This tutorial is about installing and switching between different Python versions on Ubuntu 20.04.
Using multiple Python versions on your server is a very good choice especially when you have multiple Python applications and you need to test them one by one. Of course, every Python application can run on a different Python version depending on the developer’s choice. It is recommended for the developers or system administrators to use virtual environments for every newly deployed Python application. This is the main purpose of the tutorial, to have multiple Python versions on a single system and have choices before creating the virtual environments or changing the default Python version.
In the next steps, first we are going to install the default Python version in Ubuntu 20.04, and then lower Python versions. Once we install multiple Python versions, we are going to explain the process of switching them. Let’s get to work!
Table of Contents
Prerequisites
- Fresh install of Ubuntu 20.04
- User privileges: root or non-root user with sudo privileges
Update the System
Before installing anything on your server it is recommended to update the system packages to their latest versions available.
sudo apt update -y && sudo apt upgrade -y
Step 1. Install Default Python Versions
In the first step of this tutorial we are going to install the default Python2 and Python3 versions in Ubuntu 20.04.
To install the default Python2 version execute the following command:
sudo apt install python2 -y
To check if the Python2 version is installed successfully execute the command below:
python2 -V
You should get the following output:
root@vps:~# python2 -V Python 2.7.18
To install the default Python3 version execute the following command:
sudo apt install python3 -y
To check if the Python3 version is installed successfully execute the command below:
python3 -V
You should get the following output:
root@vps:~# python3 -V Python 3.8.10
Step 2. Install Multiple Python Versions
In the previous steps we installed the default Python versions in Ubuntu 20.04, Python3.8, and Python2.7. Now we are going to install lower Python3 versions such as Python3.7, Python3.6 and Python3.5
To install Python3.7, you need to add the repository first:
sudo apt install software-properties-common sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update
Once, the repo is added and the system is updated, execute the command below:
sudo apt install python3.7 -y
To check if the Python3.7 is installed successfully, run the following command:
python3.7 -V
You should receive the following output
root@vps:~# python3.7 -V Python 3.7.12
Next version for installing is the Python3.6 version. Since the repo is already added you need to execute the following command to install Python3.6
sudo apt install python3.6 -y
To check if the Python3.6 is installed successfully, run the following command:
python3.6 -V
You should receive the following output
root@vps:~# python3.6 -V Python 3.6.15
The last Python version that we are going to install is the Python3.5 version:
sudo apt install python3.5 -y
p>To check if the Python3.5 is installed successfully, run the following command:
python3.5 -V
You should receive the following output
root@vps:~# python3.5 -V Python 3.5.10
Step 3. Create Python Symlinks
Once, multiple Python versions are installed, we need to add the symbolic links for every Python version separately. We are going to create symlinks for the following Python versions respectively: Python2.7, Python3.5, Python3.6, Python3.7, and Python3.8 Next to the symlink we are going to add the group name python and the option number.
Execute the following commands one by one:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 3 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 4 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 5
After execution, you should receive the following output:
root@vps:~# sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in auto mode root@vps:~# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2 update-alternatives: using /usr/bin/python3.5 to provide /usr/bin/python (python) in auto mode root@vps:~# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 3 update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in auto mode root@vps:~# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 4 update-alternatives: using /usr/bin/python3.7 to provide /usr/bin/python (python) in auto mode root@vps:~# sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 5 update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode
Step 4. Change Python Version
Once, the symlinks are created we can list the installed python versions and switch them easily. Execute the command below:
sudo update-alternatives --config python
You should get the following output:
0 /usr/bin/python3.7 4 auto mode 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.5 2 manual mode 3 /usr/bin/python3.6 3 manual mode 4 /usr/bin/python3.7 4 manual mode * 5 /usr/bin/python3.8 0 manual mode Press to keep the current choice[*], or type selection number:4
You can enter any number and use that Python version. For example we choose the Python3.7 by entering the number 4
If you execute again the command:
sudo update-alternatives --config python
You should receive the following output with changed Python version
There are 5 choices for the alternative python (providing /usr/bin/python). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/python3.7 4 auto mode 1 /usr/bin/python2.7 1 manual mode 2 /usr/bin/python3.5 2 manual mode 3 /usr/bin/python3.6 3 manual mode * 4 /usr/bin/python3.7 4 manual mode 5 /usr/bin/python3.8 0 manual mode
Congratulations! You successfully installed multiple Python versions on your system. Besides that, you were able to switch the Python versions very easily. Of course, you do not have to do this by yourself, since our admins will help you with any aspect of using multiple Python versions. Do not hesitate to contact us anytime you want. We are available 24/7.
If you liked this post on How to Install and Switch Python Versions on Ubuntu VPS, please share it with your friends on social networks or simply leave a reply below. Thanks.
I tried the method described with Ubuntu 22.04 (LTS).
(I know, this post is written for 20.04. But, perhaps there is someone who has the same idea as I had.)
This method broke my system; couldn’t reboot anymore. No problem, I use Ubuntu on a VM and did a snapshot before.
I don’t know why this happend, but I found this discussion (https://unix.stackexchange.com/questions/410579/change-the-python3-default-version-in-ubuntu). Comments to the highest rated answer mention, that this method can breake apt. Another user reports on a break of gnome-terminal.
Alternatives to this method are also mentioned there (updating symbolic link). Didn’t try them out yet.
If you install a different Python version on your system you need to make sure that you don’t change the default python version because it may break it.