Installing Python 2.7.18 and PIP on Debian 12 from Source

Python 2 was a popular choice for many years until it reached end-of-life (EOL) on January 1, 2020. While it is no longer maintained, some legacy software still depends on it.

This guide explains how to compile and install Python 2.7.18 and PIP on a Debian 12 system, verify the setup, and run a test script to confirm everything works as expected.

Important Notice

Python 2 is no longer supported. Avoid using it for new projects—only use it when maintaining or running legacy applications that explicitly require it.

Prerequisites

  • Access to a Debian 12 Linux instance
  • A non-root user account with sudo privileges

Step 1 – Installing Python 2

Since Debian 12 no longer includes Python 2 in its repositories, you need to compile Python 2.7.18 from source.

Update the package index

Upgrade installed packages

Install required build dependencies

sudo apt install libreadline-dev libbz2-dev libsqlite3-dev libssl-dev -y

Download Python 2.7.18 source

wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz

Extract the archive

Navigate to the source directory

Configure the build

This sets up the compilation environment.

./configure --prefix=/usr/local --enable-optimizations

  • --prefix=/usr/local: Installs Python into the /usr/local directory.
  • --enable-optimizations: Enables performance features such as LTO and PGO.

Compile the source

Install Python 2

Verify installation

Expected output:

Python 2.7.18

Step 2 – Installing PIP for Python 2

PIP is Python’s package installer. For Python 2, you can install it using the get-pip.py script.

Download the installer script

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

Run the installer

If you see a warning like:

WARNING: The scripts pip, pip2, and pip2.7 are installed in ‘/home/your-user/.local/bin’, which is not on PATH.

Add the directory to PATH

echo 'export PATH=$PATH:$HOME/.local/bin' >> ~/.bashrc

Reload shell configuration

Check PIP version

Example output:

pip 20.3.4 from /home/your-user/.local/lib/python2.7/site-packages/pip (python 2.7)

Step 3 – Testing Python 2 and PIP

To verify that Python 2 works properly, create a simple script.

Create a test script

Insert this code into the file:

print "Hello from Python 2"

Run the script

Expected output:

Hello from Python 2

Test installing a package

If the installation succeeds, PIP is functioning correctly.

Conclusion

In this guide, you compiled and installed Python 2.7.18 along with PIP 2 on Debian 12 from source, then tested them to ensure proper functionality. This setup allows legacy Python 2 applications to run on modern systems. For all new development, use Python 3 to ensure continued updates, security patches, and support.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: