to select ↑↓ to navigate
Developer Guides

Developer Guides

Open in ChatGPT
Ask ChatGPT about this page
Open in Claude
Ask Claude about this page

How to Install Multiple Python Versions

Install Multiple Python Versions

Install recent Python versions via PPA

Add PPA & Install certain Python edition

Run the command below to add the PPA:

$ sudo add-apt-repository ppa:deadsnakes/ppa

After adding the PPA, simply run the command below to install certain python version as you want.

$ sudo apt install python3.11 python3.11-venv

Replace version number in command, e.g., python3.9 and python3.9-distutils for Python 3.9

Verify

To verify Python versions, use commands python3.x --version:

$ python3.9 --version

$ python3.11 --version

Use Pip for specific Python version

It does not install pip separately for each version. But, you may simply use the command below to install a package via Pip (replace 3.11 to yours Python version):

$ python3.11 -m pip install PACKAGE

Or, add a shortcut via command:

alias pip3.11="python3.11 -m pip"

Then you may use command below to use pip install:

pip3.11 install PACKAGE

Set your Python as default

The system default Python 3 is Python 3.9 on Ubuntu 20.04, Python 3.10 on Ubuntu 22.04. It’s not recommended to change it because some core apps depend on it.

However, you may set default python which is not in use by default. Firstly, run the commands below to add your Python as alternatives (run the command below accordingly):

#Set python3 alternatives: python3.9, python3.11 and python3.10 (auto mode set to python3.10 as largest priority number)

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1 $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2 $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 3

#Set python2 alternatives: only python2.7 for now

$ sudo update-alternatives --install /usr/bin/python2 python2 /usr/bin/python2.7 1

#Set python alternatives: python2 and python3 (python3 is auto mode)

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1 $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Run the previous commands if you even has other versions installed. And, finally use the command below choose one as default:

$ sudo update-alternatives --config python3

$ sudo update-alternatives --config python2

$ sudo update-alternatives --config python

Optionally, you can install the following extras.

Install development headers for building C extensions:

$sudo apt install python3.9-dev -y

Install the standard library (venv) module:

$sudo apt install python3.9-venv -y

Install the standard library (distutils) module:

$ sudo apt install python3.9-distutils -y

Install the (2to3.8) utility as well as the standard library (lib2to3) module:

$ sudo apt install python3.9-lib2to3 -y

Install the standard library (dbm.gnu) module:

$ sudo apt install python3.9-gdbm -y

Install the standard library (tkinter) module:

$ sudo apt install python3.9-tk -y

Last updated 2 months ago
Was this helpful?
Thanks!