11

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?

0

3 Answers 3

18

You can run python3.9 with the command

python3.9

(instead of the command python or python3)

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

14

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
2
  • 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 stackoverflow.com/questions/69594088/…
    – rome
    Aug 12, 2023 at 23:18
  • @rome For Python3.11 please see askubuntu.com/a/1443735
    – Raffa
    Aug 13, 2023 at 7:33
4

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

2
  • 1
    +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 :)
    – Raffa
    Jun 25, 2021 at 12:59
  • @Raffa Miniconda/Anaconda has a lot of scientific packages that are not otherwise available on pip. Mar 29, 2022 at 8:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .