
Python 2 was widely used for many years before reaching its end-of-life (EOL) on January 1, 2020. While Python 2 is no longer maintained, some legacy applications still use it.
This article shows how to install Python 2.7.18 and PIP from source on a Debian 12 instance, verify the installation, and run a Python script to confirm that Python 2 works as expected.
Prerequisites
Before you begin:
- Have access to a Debian 12 based Linux instance as a non-root sudo user.
Install Python 2
Debian 12 does not include Python 2 in its default repositories. To install it, compile Python 2.7.18 from source.
Update the APT package index.
console$ sudo apt update
Upgrade installed packages.
console$ sudo apt upgrade -y
Install required dependencies.
console$ sudo apt install libreadline-dev libbz2-dev libsqlite3-dev libssl-dev -y
Download the Python 2.7.18 source code.
console$ wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
Extract the archive.
console$ tar zxf Python-2.7.18.tgz
Switch to the Python source folder.
console$ cd Python-2.7.18
Run the
configure
script to set up the build system.console$ ./configure --prefix=/usr/local --enable-optimizations
In the command above:
--prefix=/usr/local
: Installs Python in the/usr/local
directory.--enable-optimizations
: Enables performance optimizations such as LTO and PGO.
Compile the build files.
console$ make
Install Python 2 from the built source.
console$ sudo make install
Verify the installation.
console$ python2 --version
Output:
Python 2.7.18
Install PIP for Python 2
PIP is the package manager for Python. For Python 2, install PIP 2 using the get-pip.py
script.
Download the installation script.
console$ curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py
Install PIP 2 using Python 2.
console$ python2 get-pip.py
If you see the following warning in your output:
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 your
PATH
.console$ echo 'export PATH=$PATH:$HOME/.local/bin' >> ~/.bashrc
Update your shell environment's PATH variable.
console$ source ~/.bashrc
Verify the installation.
console$ pip2 --version
Example output:
pip 20.3.4 from /home/your-user/.local/lib/python2.7/site-packages/pip (python 2.7)
Test and Use Python 2 and PIP
To test your Python 2 installation, follow the steps below.
Create a Python script for testing purposes.
console$ nano hello.py
Add the following contents to the file:
pythonprint "Hello from Python 2"
Save and exit the file.
Run the script.
console$ python2 hello.py
Output:
Hello from Python 2
This validates your Python 2 installation.
Try installing a PIP package, such as the
lxml
package.console$ pip2 install lxml
If the command works and installs the package, your PIP 2 is working.
Conclusion
In this article, you installed Python 2.7.18 and PIP 2 on a Debian 12 instance from the source and tested them. This setup enables legacy Python 2 applications to run on modern systems. Avoid using Python 2 for new development and migrate to Python 3 to ensure continued support and security.
No comments yet.