Installing Python 2 on Ubuntu 22.04

Python 2 reached its official end of life on January 1, 2020, and is no longer actively maintained. Although deprecated, certain older applications and scripts still rely on it. While it is not advised for new projects, Python 2 can still be installed on Ubuntu 22.04 using packages from the official universe repository.

This guide explains how to install Python 2 on Ubuntu 22.04, configure pip for Python 2, and confirm the installation. If you are using another operating system, you may want to review our guides for installing Python and Pip on Ubuntu 24.04 or setting up Python 2 on FreeBSD 14.0.

Prerequisites

  • Access to an Ubuntu 22.04 system with a non-root user who has sudo privileges.

Enable the Python Repository

Python 2 is not part of Ubuntu’s default setup, but it can still be obtained from the official universe repository. The following steps prepare your system to locate and install older Python releases.

Update the package index

Install repository management tools

$ sudo apt install -y software-properties-common

Check the available Python 2 version

Sample output:

python2:
  Installed: (none)
  Candidate: 2.7.18-3
  Version table:
     2.7.18-3 500
        500 http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages

For Ubuntu 22.04, Python 2.7.18 is the most recent version available.

Install Python 2

Install Python 2 from the official Ubuntu repository and verify the installation.

Install Python 2

$ sudo apt install -y python2

Check the Python version

Sample output:

Install pip for Python 2

pip is Python’s standard package manager, allowing installation and management of packages from the Python Package Index (PyPI). Since Python 2 does not come with pip by default, it must be installed manually.

Download the get-pip.py script

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

Install pip for Python 2

$ sudo python2.7 get-pip.py

Verify pip installation

Sample output:

pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

Test and Use Python 2

After installation, test the Python 2 interpreter to ensure it runs properly.

Start the Python 2 shell

Run a simple print statement

>>> print('Hello, World!')

Sample output:

Exit the Python shell

Conclusion

Python 2 is now installed alongside the default Python 3 environment on Ubuntu 22.04. This configuration lets you maintain legacy applications that depend on Python 2 while still using modern Python 3 tools.

For more detailed information on maintaining Python 2 projects, consult the official Python 2.7 documentation.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: