how to install and switch python versions on ubuntu 22.04

How to Install and Switch Python Versions on Ubuntu 22.04

In this tutorial, we are going to explain in step-by-step detail how to install multiple Python versions on Ubuntu 22.04

Python is a high-level, general-purpose programming language compatible with different operating systems such as MacOS, Windows, Linux and etc.

In this blog post, we will focus on installing Python in the Linux distributions. Also, sometimes on one Linux server, it is necessary to be installed multiple Python versions. This tutorial will teach you about installation and changing between different versions.

Installing multiple Python versions on Ubuntu 22.04 is a very easy and straightforward process that may take up to 15 minutes. Let’s get things done!

Prerequisites

  • Fresh install of Ubuntu 20.04
  • User privileges: root or non-root user with sudo privileges

Update the System

Every fresh installation of the operating system needs the packages to be updated to the latest versions available.

sudo apt update -y && sudo apt upgrade -y

Step 1. Install Python 3.10

We will start with the latest version available in the default repository of Ubuntu 22.04. To install python3.10 execute the following command:

sudo apt install python3.10 -y

To check the installed version, execute the command python3.10 -V. You should receive the following output:

root@host:~# python3.10 -V
Python 3.10.6

Step 2. Install Python 3.8

The second Python version on our list will be Python 3.8. To install it, execute the following command:

sudo apt install python3.8 -y

To check the installed version, execute the command python3.8 -V. You should receive the following output:

root@host:~# python3.8 -V
Python 3.8.16

Step 3. Install Python 3.6

The third Python version will be the Python 3.6 version. Let’s execute the following command:

sudo apt install python3.6 -y

As you noticed, the installation process failed with the following message:

root@host:~# sudo apt install python3.6 -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package python3.6
E: Couldn't find any package by glob 'python3.6'

This means that Python3.6 is not in the default repository of Ubuntu 22.04. We will need to download the build from the source. To do that, execute the following command:

cd /opt

wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz

tar xfv Python-3.6.9.tgz

cd /Python-3.6.9

Before we proceed with installation, we need to install the prerequisites for the older Python versions:

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

After all these preparations, we can finally build and install it with the following commands:

./configure --enable-optimizations

sudo make altinstall

To check the installed version, execute the command python3.6 -V. You should receive the following output:

root@host:/opt/Python-3.6.9# python3.6 -V
Python 3.6.9

Step 4. Install Python 3.5

The last version of Python in this blog post will be the Python3.5 version. We will download and install the same way as the Python3.6

cd /opt

wget https://www.python.org/ftp/python/3.5.10/Python-3.5.10.tgz

tar xfv Python-3.5.10.tgz

cd Python-3.5.10/

./configure --enable-optimizations

sudo make altinstall

To check the installed version, execute the command python3.5 -V. You should receive the following output:

root@host:/opt/Python-3.5.10# python3.5 -V
Python 3.5.10

Step 5. Switch between Python versions

In the last step of this tutorial, we are going to explain how you can switch between different Python versions. First, you need to know that the newer Python versions installed from the default repository are located in the /usr/bin directory on the server, while the older versions built from the source are located in /usr/local/bin directory.

We need to create symbolic links for every installed Python version, including the path of the installed Python version. Execute the following commands one by one:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.6 3
sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.5 4

Your command line should look like this:

root@host:/# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 1
update-alternatives: using /usr/bin/python3.10 to provide /usr/bin/python (python) in auto mode
root@host:/opt/Python-3.6.9# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode
root@host:/opt/Python-3.6.9# sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.6 3
update-alternatives: renaming python link from /usr/bin/python to /usr/local/bin/python
update-alternatives: using /usr/local/bin/python3.6 to provide /usr/local/bin/python (python) in auto mode
root@host:/opt/Python-3.6.9# sudo update-alternatives --install /usr/local/bin/python python /usr/local/bin/python3.5 4
update-alternatives: using /usr/local/bin/python3.5 to provide /usr/local/bin/python (python) in auto mode

To list and choose which Python version should be active, execute the following command:

sudo update-alternatives --config python

The output should look like this:

root@host:/# sudo update-alternatives --config python
There are 4 choices for the alternative python (providing /usr/local/bin/python).

  Selection    Path                      Priority   Status
------------------------------------------------------------
* 0            /usr/local/bin/python3.5   4         auto mode
  1            /usr/bin/python3.10        1         manual mode
  2            /usr/bin/python3.8         2         manual mode
  3            /usr/local/bin/python3.5   4         manual mode
  4            /usr/local/bin/python3.6   3         manual mode

You can enter any number and use that Python version. For example, we choose the Python3.10 by entering the number 1

Execute the command sudo update-alternatives –config python again, and you will notice the changed version:

root@host:/# sudo update-alternatives --config python
There are 4 choices for the alternative python (providing /usr/local/bin/python).

  Selection    Path                      Priority   Status
------------------------------------------------------------
  0            /usr/local/bin/python3.5   4         auto mode
* 1            /usr/bin/python3.10        1         manual mode
  2            /usr/bin/python3.8         2         manual mode
  3            /usr/local/bin/python3.5   4         manual mode
  4            /usr/local/bin/python3.6   3         manual mode

Congratulations! You successfully installed multiple Python versions on your system and learned how to switch them. Of course, you do not have to do this by yourself. You can contact our technical support, and the admins will help you with any aspect of the installation and configuration of Python versions. We are available 24/7.

If you liked this post on how to Install and Switch Python Versions in Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below. Thanks.

8 thoughts on “How to Install and Switch Python Versions on Ubuntu 22.04

    1. Are you facing any errors? The commands are the same, but repositories for certain versions of Python must be added manually.

  1. Hi,

    this is great – I searched for such a instruction quiet some time. Thank you!

    It seems everything worked for me, but when I tried to install pip for python3.6 I got a segmentation fault. Do you have any idea how I could install pip?

    Thanky you,
    best,
    Nadine

  2. Hi thanks for the tutorials, it works well. Thank you.

    Is there any guide how to have different pip versions ? I already installed python 3.10, 3.6 and 3.5 however the pip only point out to python3.10.

    Regards,
    Iwan

    1. You can use the following command to install a package on a specific Python version: python3.6 -m pip install “package”

      Remember to change the version number on the Python command to install the package in the version you want.

    1. Please run the following commands as root and provide us with the output;

      apt clean
      apt update
      apt install python3.6
      free -m

Leave a Reply

Your email address will not be published. Required fields are marked *