Switching between Python 2 and 3 versions on Ubuntu 20.04

Are you missing Python 2 on your Ubuntu 20.04 system? It was dropped out completely since this Focal Fossa release, but here is how to install it and switch between version 2 and version 3 on your current Ubuntu installation.

In the recent past, there has been a bit of debate on which Python version should one learn. Python 2 or Python 3. It’s now 2020 and pretty evident that Python 3 is the way to go. However, Python 2 is still in the market, and some people, including me, still use it in development. It brings forth the necessity of how we set up and switch between the two versions on your Linux system.

Ubuntu 18.04 LTS release came with several changes, and one is that Python 2 was no longer the default Python version in the system. Welcome, Ubuntu 20.04 LTS release; canonical dropped Python 2 entirely and no longer included it among the installation packages.

See the image below. When we try to execute the Python 2 command, which is by default “python,” we get “command not found” error.

Python 2 not installed in Ubuntu 20.04
Python 2 not packaged in Ubuntu 20.04

In this post, we will show you how to:

  • Install Python 2 in Ubuntu 20.04 LTS
  • Switch between Python 3 and Python 2 versions

Installing Python 2 in Ubuntu 20.04 LTS

Step 1) Launch the Terminal and type any of the commands below. You will be required to enter your root password.

$ sudo apt install python2
$ sudo apt install python-minimal

Install Python2 in Ubuntu 20.04 LTS
Install Python2 in Ubuntu 20.04 LTS

Step 2) Once the installation complete, you can check the Python 2 version using the “–version” command.

python2 --version
python3 --version

Check the python version installed
Check the python version installed

From the image above, we can see we are running Python 2.7 and Python 3.8. Now, let’s jump on to one of the important parts of this tutorial of how to configure Python and switch between the two versions – Python 2 and Python 3.

How to switch between Python 2 and 3 versions on Ubuntu 20.04

Method 1: My recommended way by configuring

Step 1) Check all the available Python versions in your system. To do so, we will need to check the /bin directory. That is because we can have variations in Python 3. Say Python 3.7 and Python 3.8. In such situations, the –version command won’t be useful as it only lists the currently configured version.

Execute the commands below on the Terminal.

ls /usr/bin/python*

Check all the installed Python versions in the bin directory
Check all the installed Python versions in the bin directory

Step 2) Once we have listed all the versions present on the system, we need to check whether there are any Python-alternatives configured.

Execute the command below on the Terminal.

sudo update-alternatives --list python

Check for any Python alternatives configured on the system
Check for any Python alternatives configured on the system

From the image above, we see that there are no Python alternatives configured.

Step 3) Now, we will configure two Python alternatives. From the image in Step 2 above, we saw that I have Python 2.7 and Python 3.8 present on my system.

Execute the commands below on the Terminal.

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2

Configure Python Alternatives
Configure Python Alternatives

Step 4) We need to confirm the Python alternatives set and whether they are in use.
Execute the command below again.

$ sudo update-alternatives --config python

Confirm the Python Alternatives set
Confirm the Python Alternatives set

On the prompt that appears on the Terminal, enter 1 or 2 to make your selection. In this post, we want to use Python 2; therefore, we will enter option 1.

Step 5) Now, let’s check the Python version currently running on our system. Execute the command below.

python --version

Check the currently running Python Version
Check the currently running Python Version

To switch to another Python version, all you need to do is execute the command on Step 4 and select the other option. In this case, we would choose option 2 to use Python 3.

Method 2: Old-school way without configuration

This other method, we would refer to it as the manual way since we won’t perform any configurations. Follow the procedure below.

Step 1) Install Python 2 with the command below.

sudo apt install python2

Step 2) Check the Python versions present in your system by running the command below.

ls /usr/bin/python*

Check all the installed Python versions in the bin directory
Check all the installed Python versions in the bin directory

Step 3) In this post, we currently have Python 2 and Python 3 available. Now write your Python 2 or Python 3 code. 

For example, below is a Python 2 code to print the sentence “Hello, This Fosslinux.com.

Python2 Code
Python2 Code

Step 4) To execute your code using Python 2, you will need to specify the version manually. For example, the python file is called Example.py. Execute the command below.

python2 Example.py

Execute the Python 2 code
Execute the Python 2 code

If we used Python 3 to execute the code below, we would get an error, as shown below.

Error when using Python3 to execute Python2 code
Error when using Python3 to execute Python2 code

If we were writing code using Python 3, we would specify Python 3 when running the program.


Despite Python 2 being dropped in the recent Linux releases, some people still find it advantageous and use it in their development. There are vast applications built using Python 2, and the transition to Python 3 has not been put into effect fully.

These and many other reasons make Python 2 still relevant in the development community. With the methods described above, you will be able to switch from one Python version to another easily when coding.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.


  1. hello

    i’m starting with UBUNTU. i installed 20/04 LTS and get an error message when trying to install python2
    sudo apt install python2 start but
    reading > ok
    status information reading > ok
    impossible to find python2 paquet.

    i have a old school python script that need the 2 and not the 3 !
    i tried to upgrade it by changing the print syntax but now experiencing decode_encode issues.

    thanks for the help.


Please enter your comment!
Please enter your name here




Basics of Working with the SQLite Database in Python

A database is one of the most useful and popular files for storing data; they can be used to store any kind of data, including text, numbers, images, binary data, files, etc. SQLite is a relational database management system based on the SQL language. It is a C library, and it provides an API to work with other programming languages, including Python. It does not require a separate server process to be run as needed in large database engines like MySQL and Postgresql.

5 Ways to Check the Linux Version

When most people talk of Linux, they are always referring to a Linux distribution. However, this is not the case. Linux itself is a kernel which acts as a bridge between user applications and the hardware. When we talk of a Linux distribution, we refer to an operating system developed from the Linux kernel. A distribution comes with a package manager, pre-installed applications, a Desktop Environment, and several more features.

Getting Started with Linux Operating System

The Linux operating system brings forth a vibrant mix of features and security, making it the best alternative to macOS or Windows operating systems. In this post, we will give you a master guide on Getting started with Linux systems - taking you from a complete beginner to a level where you can begin testing the various Linux distributions available with much ease.

How to Create a Comprehensive Mail Server on Ubuntu

Postal is a free and open-source mail server used to send and receive emails. It comes loaded with tons of excellent features and functionalities, making it extremely popular among large organizations as well as in enterprise settings.

The 10 Best Linux Performance Monitoring Tools

Do you want to monitor the performance of your Linux system? Are you looking for some powerful performance monitoring tools to help you out? If you agree, it's your day as we have put together a detailed list of the ten best Linux performance monitoring tools.

How to Boot your Windows or Linux PC from a USB Drive

Sometime back, the process of installing an operating system required users to pop a bootable media disk into their DVD or CD drive and use it to boot the PC. But times have changed. Nowadays, the most common way of installing an OS is booting from a USB drive. The use of USB drives is further propelled by the current production of slim and lightweight laptops with no support for DVD/CD drives.


Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

Installing multiple Linux distros on a USB drive

A bootable Linux USB drive, which also referred to as a Live USB drive, is a drive containing files that enable a computer to boot into a specific Linux distro. It is most commonly used to test-drive various Linux distros before installing on the computer for use as a daily driver.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

10 Best Screen Capturing Software for Linux

Do you want to make a video tutorial for YouTube or show-off your epic victory royale to your friends? Look no further — as we have compiled the 10 Best Screen Capturing Software for Linux. Whether you need to record your desktop screen or take a simple screenshot, FOSSLinux has got your back.

6 cool and fun Linux commands you don’t want to miss

The real purpose of this article is to help Linux newbies get comfortable and confident with the Linux command-line.  While knowledge and comfort of the Linux GUI allow great power, it is the mastery of the command line, or CLI, affords the Linux user unlimited power and certifies them as a Linux power user well on their way to becoming an expert.