Score:5

How to install latest version of python 3.9.5 on Ubuntu 20.04?

us flag

I updated my system using:

sudo apt-get update && sudo apt-get upgrade

And then ran the following to install python3.9:

sudo apt-get install python3.9

Which yields output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
python3.9 is already the newest version (3.9.5-3~20.04.1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

However, when I run python3 -V it still tells me I am on 3.8.5. How can I fix this?

Score:11
us flag

You can run python3.9 with the command

python3.9

(instead of the command python or python3)

It is a good idea not to change the default version of python3 to python3.9, as that may break your Ubuntu installation. Instead, manually call it with python3.9.

Score:10
jp flag

In addition to explicitly specifying the python3.9 and working with it globally as explained in Archisman Panigrahi answer ... You can create an isolated virtual environment where python3 -V will report Python 3.9.

This feature can be installed for Python3 like so:

sudo apt install python3-venv

To make a Python3.9 virtual environment, you would first create a directory and cd to it like so:

mkdir my_env && cd my_env

Then, create a new Python3.9 virtual environment inside the directory like so:

python3.9 -m venv env

To use this environment, activate it like so:

source env/bin/activate

Your shell prompt will show (env) like so:

(env) $

During this, python3 -V will report Python 3.9 and commands, module installs or modifications will be contained locally in this virtual environment.

When you are done, deactivate this Python3.9 virtual environment like so:

deactivate
rome avatar
my flag
i have installed python3.11, but python3.11 -m venv env failed with Error: Command '['/home/<user>/python3.11-venv/env/bin/python3.11', '-m', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1. to fix it, i needed to install the corresponding venv package 'sudo apt install python3.11-venv'. See also this question https://stackoverflow.com/questions/69594088/error-when-creating-venv-error-command-im-ensurepip-upgrade-def
Raffa avatar
jp flag
@rome For Python3.11 please see https://askubuntu.com/a/1443735
Score:4
it flag

Consider installing Python with Anaconda or Miniconda (I recommend Miniconda). Conda lets you manage different Python versions easily with virtual environments.

sudo wget -c https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

sudo chmod +x Miniconda3-latest-Linux-x86_64.sh

./Miniconda3-latest-Linux-x86_64.sh

Press enter until it asks for "yes" or "no", then type "yes" to accept the terms of use.

If you're using a shell other than bash, type:

conda init <SHELL_NAME> (Supported shells include: fish, tcsh, xonsh, zsh)

Close and open the terminal. Type conda activate to activate the (base) conda environment.

Create a Python 3.9.5 environment, and name it whatever you like:

conda create -n myenv python=3.9.5

Once created, you can activate and use that Python environment:

conda activate myenv

When finished, you can deactivate your environment with:

conda deactivate

Any packages you install with pip or conda will be local to whatever environment you're using, so you don't have to worry about package conflicts. Just don't install everything in your (base) environment, because anything installed in (base) will get copied to new conda environments you create.

Sources:

Setting up Miniconda on Ubuntu

Conda Cheat Sheet

Anaconda Docs

Raffa avatar
jp flag
**+1** for the concept of isolation and using a virtual environment. I however added an answer of what I think a simpler way of doing it on Ubuntu :)
us flag
@Raffa Miniconda/Anaconda has a lot of scientific packages that are not otherwise available on pip.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.