How to Install and Switch Python Versions on Ubuntu 22.04

August 1, 2023 / How-to Guide

In this article, we will provide a step-by-step process of installing multiple Python versions on Ubuntu 22.04.

Let’s get started and get things done!

Requirements

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

Update the System

It is important to update the packages to their latest versions for every new operating system installation.

sudo apt update -y && sudo apt upgrade -y
  1. Install Python3.10

    We will install 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 verify the installed version, run the command python3.10 -V. You should see the following output:

    root@host:~# python3.10 -V
    Python 3.10.6
  2. Install Python3.8
    Next on our list is Python 3.8 the second Python version. To install it, execute the following command:

    sudo apt install python3.8 -y

    To verify the installed version, run the command python3.8 -V. You should see the following output:

    root@host:~# python3.8 -V
    Python 3.8.16
  3. Install Python3.6
    The third Python version of our list will be Python 3.6. Let’s proceed with the following command:

    sudo apt install python3.6 -y

    As you have observed, the installation process encountered an error, displaying 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 indicates that Python 3.6 is not available in the default repository of Ubuntu 22.04. Therefore, we need to download the build from the source. To do so, 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 continuing with the installation, we must 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

    With all these preparations completed, we can now proceed to build and install it using the following commands:

    ./configure --enable-optimizations
    
    sudo make altinstall

    To verify the installed version, run the command python3.6 -V. You should see the following output:

    root@host:/opt/Python-3.6.9# python3.6 -V
    Python 3.6.9
  4. Install Python3.5
    The final Python version discussed in this blog post will be Python 3.5. We will download and install it in the same way as we did for Python 3.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 verify the installed version, run the command python3.5 -V. You should see the following output:

    root@host:/opt/Python-3.5.10# python3.5 -V
    
    Python 3.5.10
  5. Switch between Python versions
    As we conclude this tutorial, we will explain how to switch between different Python versions. It’s important to understand that 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 can be found in the /usr/local/bin directory. We need to create symbolic links for each 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 resemble 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

    The output should appear similar to 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 to select and use a specific Python version. For example, if we choose Python 3.10, we would enter the number 1.

    Execute the command sudo update-alternatives –config python once more, and you will see the updated Python 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 have successfully installed multiple Python versions on your system and learned how to switch between them. However, remember that you don’t have to handle this process alone. Our technical support team is here to assist you with any aspect of Python installation and configuration. We are available 24/7, so feel free to contact us anytime for assistance.

Spread the love