Install Python 2.7 and Pip on Ubuntu 22.04 Jammy Jellyfish

The problem

The Python Software Foundation declared Python 2 End of Life (EOL) on January 1, 2020, and ended support. Python 2.7.18 was the final release of Python 2, released in April 2020. No version of Python 2 will receive updates, not even for critical security vulnerabilities. Thus, Python 2 has not been the default installed version on Ubuntu versions for a few years so far. Ubuntu 20.04 is the first LTS version of Ubuntu to drop Python2, coming fresh out of the box with Python 3.8.5.

However, as Python developers, we might still have Python 2 related projects (eg. converting a python2 to python3 project) and that’s point where installation Python2 on Ubuntu 22.04 comes. This tutorial will show how to install Python 2 for Ubuntu 22.04 Jammy Jellyfish.

Install Python 2.7

As usual, you would need to update the system anytime you install something in Ubuntu by executing the following command on command-line terminal (press Ctrl+Alt+T to open the terminal).

sudo apt update; sudo apt dist-upgrade

To install Python 2 version on Ubuntu 22.04, please enter the following commands:

sudo apt install python2 -y

The above command will install the following packages for you

libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib python2-minimal python2.7 python2.7-minimal

To verify the installation is successful, please execute the following command:

python2 -V

You should see `Python 2.7.18` as output. Things should look like below:

your_name@your_computer:~# python2 -V
Python 2.7.18

Install Pip for Python 2.7

Please enter the following command to get pip installation script using curl and save it into `get-pip.py`

curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output get-pip.py

Run the following command to install pip for Python 2.7:

python2 get-pip.py

As the result, you should see something like below

Install Pip 2 on Ubuntu 22.04

Create and Manage Virtual Environments

Virtual environments can be described as isolated installation directories. This isolation allows you to localized the installation of your project’s dependencies, without forcing you to install them system-wide.

To create virtual enviroments, your firstly need to install virtualenv using pip as follow:

pip2 install virtualenv

Now, you can start creating your virtual environments for your Python 2 projects using command as follow:

virtualenv ~/python-venv/2.7/project1
virtualenv ~/python-venv/2.7/project2

The two commands above will:

  • Create the directory python-venv inside your home directory if it does not exist
  • Create the directory 2.7 inside the python-venv mentioned above if it does not exist
  • Create the directory project1 inside the 2.7 mentioned above and deploy Python2 into it
  • Create the directory project2 inside the 2.7 mentioned above and deploy Python2 into it

Now, verify the creation of the first environment by issuing two commands below:

# activate the project1 environment
source ~/python-venv/2.7/project1/bin/activate
# run Python2 code in an interactive session
python

You are expected to see output as below

your_name@your_computer:~# source ~/python-venv/2.7/project1/bin/activate
(project1) your_name@your_computer:~# python
Python 2.7.18 (default, Jul  1 2022, 10:30:50) 
[GCC 11.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Please make sure that:

  1. The Python version is 2.7.x
  2. (project1) is prefixed your comand line prompt

Then you are done for the project1 environment at ~/python-venv/2.7/project1/.

To exit the virtual environment, just execute the two commands as follow:

exit() # to exit the Python2 interactive session
deactivate # to exit the project1 virtual environment

You can can now start doing the same for the project2 environment.

Conclusions

In this guide, we saw how to install Python 2.7.18 and Pip 20.3.4 on Ubuntu 22.04, which are the last release of Python 2 and the corresponding Pip, from April 2020. Although this version is now deprecated, it can still be installed and used for legacy purposes on Ubuntu and other Linux systems.

So, for now, you will have two Python versions which are Python 2.7 that you’ve just installed and Python 3.10 that came built-in with Ubuntu 22.04. You could run Python 2.7 by using command python2 while python3 will be for Python 3.10

You could also create virtual environments for your Python2 projects in Ubuntu 22.04 now.

You may want to check out Install Multiple versions of Python on Ubuntu 22.04 if you wish to have other versions of Python on the same manchine.

Leave a Reply