How To Set Python2 As Default In Ubuntu

In this tutorial, we will learn how to set Python 2 as the default Python interpreter in Ubuntu. Although Ubuntu 18.04 and later versions come with Python 3 installed by default, there are still some applications that require Python 2. Follow the steps given below to switch the default version to Python 2 on your Ubuntu machine.

Step 1: Install Python 2:

If you do not have Python 2 installed already, you can install it by running the following command in your terminal:

Once the installation is complete, you can check the Python 2 version using the following command:

Step 2: Check available Python alternatives:

Now, we need to check the available Python alternatives on the system. To do this, run the following command in the terminal:

If you see an error stating “no alternatives for python”, then we need to create an alternative for Python 3 (default) and Python 2.

Step 3: Create alternatives for Python:

If your system has no alternatives for Python, you can create them using the following commands:

The above commands will set the alternatives for both Python 3 and Python 2.

Step 4: Configure Python alternatives:

Now, you can configure the Python alternatives that you have created. Run the following command:

You will see a list of available alternatives to choose from. Enter the number corresponding to Python 2 and press Enter to set it as the default version.

There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3     1         auto mode
  1            /usr/bin/python3     1         manual mode
  2            /usr/bin/python2     2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2

Now, Python 2 is set as the default Python interpreter on your Ubuntu system.

Step 5: Verify the default Python version:

Finally, verify that the default Python version is set to Python 2 by running:

The output should display the version of Python 2 that is installed on your system.

Python 2.7.17

Conclusion:

In this tutorial, we learned how to set up Python 2 as the default Python interpreter on an Ubuntu system. We also covered how to create and configure Python alternatives. Following these steps, you can easily switch between Python 2 and Python 3 whenever required.