Install Multiple Python Versions on Ubuntu 22.04

How to Install and Manage multiple Python versions on Ubuntu 22.04

Ubuntu 22.04 is the second LTS version of Ubuntu that drops Python2 support, and come fresh out of the box with Python 3.10.6. But what if you’ve written apps intended for a newer version of Python or you would want to run apps written for an older version of Python, or even you would want to run multiple apps along multiple versions of Python on Ubuntu 22?

You might have tried to replace your system’s default installation and destroyed your machine in the process. If nobody has warned you, I’ll do the honors: don’t do that. I will show you how to install and run your apps along multiple versions of Python on your Ubuntu 22.04 box.

Please note that we don’t need to do anything with Python 3.10 as it is already available in Ubuntu 22.04.

Install Python 2 on Ubuntu 22.04

As said, Python2 was dropped since Ububtu 20.04. However, I have recently written a toturial on how to install Python 2.7 and Pip on Ubuntu 22.04. Please check it out and follow it to have Python 2.7 and its core features such as pip, virtual environment.

Install Python 3 on Ubuntu 22.04

In this section, I will guide you through how to install Python 3.6, 3.7, 3.8, 3.9, 3.11. Please note that version 11 of Python at the time of this post was written is still in release cadidate stage.

As for versions from 3.7, we will use Deadsnakes PPA which is an actively maintained repository of Python distributions available to Ubuntu. Deadsnakes carries the burden of hosting versions of Python that have been tried and tested to work on Ubuntu.

As for Python 3.6, we will compile it from source as Deadsnakes does not support Python 3.6.

Install Python 3.6 on Ubuntu 22.04

Prerequisites

To compile and install Python 3.6, we need to install several required packages and libraries by executing the following command:

sudo apt install make build-essential libreadline-dev \
wget curl llvm libssl-dev zlib1g-dev libbz2-dev  \
libsqlite3-dev libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev libgdbm-dev \
libnss3-dev libedit-dev libc6-dev

Download Python 3.6 source

wget https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tgz
tar -xzf Python-3.6.15.tgz

Compile and Install Python 3.6 from source

cd Python-3.6.15
./configure --enable-optimizations --with-lto --with-pydebug
make -j 4  # adjust 4 for number of your CPU cores
sudo make altinstall

Verify Python version

python3.6 -V

Install Python 3.7, 3.8, 3.9, 3.11 on Ubuntu 22.04

All the versions of Python from 3.7 are available fro Deadsnakes PPA. So, adding this PPA will help us install Python version in a convenient way with apt install.

Add Deadsnakes PPA

sudo add-apt-repository ppa:deadsnakes/ppa

Install Python 3.7, 3.8. 3.9. 3.11

Thanks to the Deadsnake PPA, to install Python 3.7 and 3.8 and 3.9 and 3.10 is just simple using apt install as below:

sudo apt install python3.7 python3.8 python3.9 \
python3.11

Verify their version

As usual, you could always execute python intepreter command to enter interactive session or using option -V to see the version.

python3.7 -V # check if the version is 3.7 
python3.8 -V # check if the version is 3.8 
python3.9 -V # check if the version is 3.9 
python3.11 -V # check if the version is 3.11 

Or, enter Python interactive console

python3.7 # adjust 3.7 for other versions

Virtual Environments for versions of Python 3

sudo apt install python3.7-venv \
python3.8-venv python3.9-venv \
python3.10-venv python3.11-venv

Now you can start creating virtual environments for your versions of Python 3 as follow:

python3.6 -m venv ~/python-venv/3.6/project1 
python3.7 -m venv ~/python-venv/3.7/project2 
python3.8 -m venv ~/python-venv/3.8/project3 
python3.9 -m venv ~/python-venv/3.9/project4 
python3.10 -m venv ~/python-venv/3.10/project5 
python3.11 -m venv ~/python-venv/3.11/project6 

Leave a Reply