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.
Matching infrastructure at centron
Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →
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
sudo apt updateUpgrade installed packages
sudo apt upgrade -yInstall required build dependencies
sudo apt install libreadline-dev libbz2-dev libsqlite3-dev libssl-dev -yDownload Python 2.7.18 source
wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgzExtract the archive
tar zxf Python-2.7.18.tgzNavigate to the source directory
cd Python-2.7.18Configure the build
This sets up the compilation environment.
./configure --prefix=/usr/local --enable-optimizations--prefix=/usr/local: Installs Python into the/usr/localdirectory.--enable-optimizations: Enables performance features such as LTO and PGO.
Compile the source
makeInstall Python 2
sudo make installVerify installation
python2 --versionExpected 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.pyRun the installer
python2 get-pip.pyIf 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' >> ~/.bashrcReload shell configuration
source ~/.bashrcCheck PIP version
pip2 --versionExample 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
nano hello.pyInsert this code into the file:
print "Hello from Python 2"Run the script
python2 hello.pyExpected output:
Hello from Python 2
Test installing a package
pip2 install lxmlIf 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.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.