Although the latest version on Ubuntu 20.04 of Python is 3.x, however, we still can install Python 2.7 from its official repository. Here we will show you that along with steps to update-alternatives for selecting the default Python version on your Linux.
With the entry-level and user-friendly programming language Python, you can code almost any imaginable application. But it’s just a piece of software like any other – installation and management can sometimes be complex. Well, not for Ubuntu. The syntax of Python is simple and understandable so that beginners and those switching can quickly find their way around. Object orientation is fully supported in Python, but no object-oriented programming is enforced. Python also supports various programming styles, such as imperative, structural, functional, or aspect-oriented programming.
- Let’s see how to install Python 2.7 version on Ubuntu 20.04 LTS
As the packages to install this programming language are already on the official repository, thus simply type-
sudo apt update sudo add-apt-repository universe
sudo apt install python2.7
- You can also use the below command to install Python-2:
sudo apt install python
To check version-
- Install Pip2
Pip2 is not available in the Ubuntu 20.04 repository to install, therefore we need to download it manually-
sudo apt install curl
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py
- Now, install the downloaded script–
sudo python2 get-pip.py
- To check version-
- As we know the latest version of Python on Ubuntu is 3.x, thus, in case you want to install that as well.
sudo apt install python3
- Set Default Python Version
Although we can install multiple versions, however, the problem how to set the default one for the system. I mean if you type-
on your command terminal of Linux, which version should call 2.7, 3.8, or any other. Well, to set the default one, we need to assign priority to different versions of Python language available on our Ubuntu system.
For that first set alternative versions for Python. For example, I have two versions on my systems, one is 2.7, and the other is 3.8. Thus, I am setting 2.7 as the default version by giving it priority 1 and 2 to 3.8 one.
update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 update-alternatives --install /usr/bin/python python /usr/bin/python3.8 2
In the same way, you can set the priority to alternate versions. Once that is done, let’s check what are versions are now available in update alternatives.
update-alternatives --list python
The above command will list all the versions available on your system.
Now, if you type –
pythonon your terminal, this will call the 2.7 installed version, not 3.8.
To switch Python version:
But, what will happen, if in future you want to set Python 3.8 as the default version, then for that you can use a simple command-
update-alternatives --config python
The above command will give a text-based selection option to set priority to the Python versions. Simply type the Selection number assign to a particular version and hit the Enter key, this will change the Priority of Python as well as the switch version default version set for your system.