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!
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
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
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
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
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
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.
The post How to Install and Switch Python Versions on Ubuntu 22.04 first appeared on LinuxHostSupport.
Deploying FreePBX and Asterisk on a single Ubuntu virtual machine in a public cloud is…
Canonical and MediaTek enhance reliability, accelerate market entry and reduce Total Cost of Ownership (TCO)…
As Ubuntu 20.04 LTS (Focal Fossa) standard support ends on May 31, 2025, Azure users…
Welcome to the Ubuntu Weekly Newsletter, Issue 881 for the week of February 23 –…
Welcome back, data scientists! In my previous post, we explored how easy it is to…
In this article, we will see how to install vLLM on Linux using 4 easy…