How to Install Python 2 on Debian 12

Updated on 02 July, 2025
Learn to compile Python 2.7.18 with pip on Debian 12 for maintaining legacy applications.
How to Install Python 2 on Debian 12 header image

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.

Warning
Python 2 is no longer maintained and should not be used for new development. Use it only if a legacy application requires it.

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.

  1. Update the APT package index.

    console
    $ sudo apt update
    
  2. Upgrade installed packages.

    console
    $ sudo apt upgrade -y
    
  3. Install required dependencies.

    console
    $ sudo apt install libreadline-dev libbz2-dev libsqlite3-dev libssl-dev -y
    
  4. Download the Python 2.7.18 source code.

    console
    $ wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
    
  5. Extract the archive.

    console
    $ tar zxf Python-2.7.18.tgz
    
  6. Switch to the Python source folder.

    console
    $ cd Python-2.7.18
    
  7. 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.
  8. Compile the build files.

    console
    $ make
    
  9. Install Python 2 from the built source.

    console
    $ sudo make install
    
  10. 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.

  1. Download the installation script.

    console
    $ curl -O https://bootstrap.pypa.io/pip/2.7/get-pip.py
    
  2. Install PIP 2 using Python 2.

    console
    $ python2 get-pip.py
    
  3. 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.
    1. Add the directory to your PATH.

      console
      $ echo 'export PATH=$PATH:$HOME/.local/bin' >> ~/.bashrc
      
    2. Update your shell environment's PATH variable.

      console
      $ source ~/.bashrc
      
  4. 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.

  1. Create a Python script for testing purposes.

    console
    $ nano hello.py
    
  2. Add the following contents to the file:

    python
    print "Hello from Python 2"
    

    Save and exit the file.

  3. Run the script.

    console
    $ python2 hello.py
    

    Output:

    Hello from Python 2

    This validates your Python 2 installation.

  4. 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.

Comments

No comments yet.