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
sudo apt update
Upgrade installed packages
sudo apt upgrade -y
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
tar zxf Python-2.7.18.tgz
Navigate to the source directory
cd Python-2.7.18
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
make
Install Python 2
sudo make install
Verify installation
python2 --version
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
python2 get-pip.py
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
source ~/.bashrc
Check PIP version
pip2 --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
nano hello.py
Insert this code into the file:
print "Hello from Python 2"
Run the script
python2 hello.py
Expected output:
Hello from Python 2
Test installing a package
pip2 install lxml
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.