There are several options to install Python interpreters without messing up the system. If you're comfortable with Docker, the best option would probably be to use a Python2 Docker container. A Docker container is somewhat similar to a VM, it's an isolated environment on your system, but much more lightweight. When starting the container, you can mount the path of your code into the container and then work from within it without affecting your system. Example (using the Docker image suggested by Artur Meinild in the comments):

docker run --rm -itd -v/path/to/code:/code -w /code --name my-python2-env esolang/python2
docker exec -it my-python2-env bash  # this will start a shell within the container
docker stop my-python2-env  # stop the container when you don't need it anymore

Another method would be to use anaconda or miniconda. This software allows you to install and manage several Python environments distinct from your system's interpreter. It's like Pip, but much more capable, e.g., it can also install interpreters and even non-Python libraries like CUDA with a simple conda install command. For instructions on how to install Conda, please refer to the official documentation.

You could also install Python2 from source into your home directory (or some other non-system directory). But this method is probably more complicated than the other two solutions and would require you to also install other dependencies from source into a non-system directory as well.

Answer from Green ็ปฟ่‰ฒ on askubuntu.com
๐ŸŒ
GitHub
github.com โ€บ ctch3ng โ€บ Installing-Python-2.7-and-pip-on-Ubuntu-24.04-Noble-LTS
GitHub - ctch3ng/Installing-Python-2.7-and-pip-on-Ubuntu-24.04-Noble-LTS ยท GitHub
./configure --prefix=/usr/local/python2.7 make sudo make install ยท Option 2: Optimized Build (For potential performance gains) The --enable-optimizations flag enables Profile-Guided Optimization (PGO), which can result in a slightly faster ...
Starred by 31 users
Forked by 6 users
๐ŸŒ
Medium
medium.com โ€บ @kallagoutham33 โ€บ how-to-install-python-2-on-ubuntu-24-04-12c0819278ba
How to install Python 2 on Ubuntu 24.04 | by Goutham Kalla | Medium
October 6, 2025 - curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py sudo python2.7 get-pip.py pip2 install virtualenv ... Now any python or pip command will point to Python 2.7 inside this environment.
Discussions

What is the safest way to work with Python 2 on Ubuntu 24.04?
Containerisation if left up to me More on reddit.com
๐ŸŒ r/learnpython
18
12
August 7, 2024
Python2.7 on Ubuntu 23.04
Python 2.7 is pretty old, I have transfered all my codes to Python 3, it has a higher performance, compatability etc. In your case, I would recommend to use conda (simply miniconda) and create a virtual environment with python=2.7, see conda instructions . I think it always good to isolate the environment from the global one from ubuntu, in order to not fuck up the latter More on reddit.com
๐ŸŒ r/Ubuntu
6
2
July 13, 2023
๐ŸŒ
LinuxShout
linux.how2shout.com โ€บ home โ€บ how to install python 2.7 on ubuntu 24.04 noble lts linux
How to install Python 2.7 on Ubuntu 24.04 Noble LTS Linux - LinuxShout
June 18, 2024 - Open a command terminalStep 2. Install DependenciesStep 3: Download the Python 2.7 Source CodeStep 4: Compile and Install Python 2.7 on Ubuntu 24.04Step 5: Verify the InstallationStep 6: Setting Up pip for Python 2.7Step 7: Change the Default ...
Top answer
1 of 2
2

There are several options to install Python interpreters without messing up the system. If you're comfortable with Docker, the best option would probably be to use a Python2 Docker container. A Docker container is somewhat similar to a VM, it's an isolated environment on your system, but much more lightweight. When starting the container, you can mount the path of your code into the container and then work from within it without affecting your system. Example (using the Docker image suggested by Artur Meinild in the comments):

docker run --rm -itd -v/path/to/code:/code -w /code --name my-python2-env esolang/python2
docker exec -it my-python2-env bash  # this will start a shell within the container
docker stop my-python2-env  # stop the container when you don't need it anymore

Another method would be to use anaconda or miniconda. This software allows you to install and manage several Python environments distinct from your system's interpreter. It's like Pip, but much more capable, e.g., it can also install interpreters and even non-Python libraries like CUDA with a simple conda install command. For instructions on how to install Conda, please refer to the official documentation.

You could also install Python2 from source into your home directory (or some other non-system directory). But this method is probably more complicated than the other two solutions and would require you to also install other dependencies from source into a non-system directory as well.

2 of 2
1

I used miniconda as suggested by Green and here's the breakdown of the steps I took to setup a conda environment using python 2.7.

  1. Install miniconda:open this link, select Linux and follow the instructions. Once you install miniconda (or anaconda) your prompt changes to show which conda environment is active (base by default) CLI prompt with base conda environment active

  2. Create a new conda environment using python 2.7:

    conda create -n py27 python=2.7 where py27 is the name of the virtual environment.

  3. Activate the py27 virtual environment.

    conda activate py27

    The prompt changes to show py27 instead of base. Py27 environment activated.

    You're all set up. Now when you run python command in the terminal you'll be executing python 2.7.x. Python 2.7.16 in my case.

Note: whenever you open a new terminal, conda environment will be set to base. If you want to remove the py27 environment, run conda env remove --name py27.

๐ŸŒ
TecAdmin
tecadmin.net โ€บ install-python-2-7-on-ubuntu-and-linuxmint
How to Install Python 2.7 on Ubuntu, Debian & Linux Mint
April 26, 2025 - Learn to install Python 2.7 on Ubuntu, Debian & Linux Mint. Our guide covers the full process, ensuring a secure setup for legacy applications.
๐ŸŒ
LinuxConfig
linuxconfig.org โ€บ home โ€บ how to install python2 on ubuntu 26.04
How to Install Python2 on Ubuntu 26.04
March 22, 2026 - Ubuntu completely removed Python ... version 24.04. The python2 package no longer exists in the official repositories or the deadsnakes PPA, so you must compile Python 2 from source. What causes the โ€œcannot use keyword โ€˜falseโ€™ as enumeration constantโ€ error? Ubuntu 26.04โ€™s GCC compiler defaults to the C23 standard, where false, true, and bool are reserved keywords. Python 2.7.18 uses these ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ what is the safest way to work with python 2 on ubuntu 24.04?
r/learnpython on Reddit: What is the safest way to work with Python 2 on Ubuntu 24.04?
August 7, 2024 -

Hi all! I just started a new job that is having me analyze/write code in Python 2. The reason it is Python 2 and not Python 3 is because we are supporting older systems that require it, and our code must be backwards compatible with these systems.

With that said, the workstation they've supplied me is running Ubuntu 24.04, and I'm at wits end as to how I can SAFELY run Python 2 code on it. From what I've gathered, recent versions of Ubuntu depend on Python 3 packages so it isn't as simple as adding a legacy apt repo and installing Python 2 - it would break my OS.

So, the question remains: How can I safely work with Python 2 without bricking my OS? Do I need to containerize or use virtualization? Thank you!

๐ŸŒ
Greenwebpage
greenwebpage.com โ€บ home โ€บ blog โ€บ how to install python 2 on ubuntu 24.04: 3 quick methods
How to Install Python 2 on Ubuntu 24.04: 3 Quick Methods
April 3, 2025 - Method 1: Compiling Python 2 from Source Method 2: Set Up pip for Python 2.7 Method 3: Use Virtual Environments Final Words How to Install Python 2 on Ubuntu 24.04? There is no single approach for installing Python 2 on Ubuntu 24.04, and you may choose from several available optionsโ€ฆ
๐ŸŒ
Linux Genie
linuxgenie.net โ€บ home โ€บ how to install python in ubuntu 24.04
How to Install Python in Ubuntu 24.04 - Linux Genie
April 24, 2024 - To install Python in Ubuntu 24.04 you can use the APT package manager. You can also install Python using its source code file.
๐ŸŒ
Super User
superuser.com โ€บ questions โ€บ 1933056 โ€บ how-to-install-python2-zlib-on-ubuntu-24
python - How to install Python2 zlib on Ubuntu 24? - Super User
December 29, 2025 - Every package maintainer is, 2 is end of life. Reportedly 22.04 python2 binaries still work on 24.04. For a limited time, you could download python2.7-minimal packages and its dependencies from jammy, and install that.
๐ŸŒ
LinuxVox
linuxvox.com โ€บ blog โ€บ install-python2-ubuntu-2404
Installing Python 2 on Ubuntu 24.04 โ€” linuxvox.com
However, you can install it using the following steps: Add the deadsnakes PPA (Personal Package Archive). This PPA provides older versions of Python, including Python 2. ... You may need to enter your password when prompted.
Top answer
1 of 3
11

This version is no longer available in canonical mirrors.

It has been released in 2013.

As a result, having both python and pip working together since then is challenging.

Python 2.7.5 + PIP on centos7

It may be the simplest way if ubuntu is not a requirement.

CopyARG CENTOS_VERSION=7
FROM centos:$CENTOS_VERSION

# Python 2.7.5 is installed with centos7 image
# Add repository for PIP
RUN yum install -y epel-release

# Install pip
RUN yum install -y python-pip

RUN python --version

ENTRYPOINT [ "python" ]

Python 2.7.5 on ubuntu

I've been able to install it from source

It has not been a success to install pip :

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

CopyARG UBUNTU_VERSION=18.04
FROM ubuntu:$UBUNTU_VERSION

ARG PYTHON_VERSION=2.7.5

# Install dependencies
# PIP - openssl version > 1.1 may be an issue (try older ubuntu images)
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build Python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

RUN python --version

ENTRYPOINT [ "python" ]

Python 2.7.6 + pip on ubuntu

Ubuntu 14.04 still has mirrors working (how long ???).

Python packages are really close to your expectations.

You may try to run your scripts with that one.

CopyARG UBUNTU_VERSION=14.04
FROM ubuntu:$UBUNTU_VERSION

RUN apt-get update \
  && apt-get install -y python python-pip \
  && apt-get clean

RUN python --version

ENTRYPOINT [ "python" ]

Python 2.7.5 + pip, not working but could on ubuntu

Here is what I've tried with no success.

CopyARG UBUNTU_VERSION=16.04
FROM ubuntu:$UBUNTU_VERSION


# Install dependencies
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

# Build pip from source
RUN wget https://bootstrap.pypa.io/pip/2.7/get-pip.py \
    && python get-pip.py

RUN python --version

ENTRYPOINT [ "python" ]

Python 2.7.9 with pip - as requested in comment

You can use this dockerfile, building python includes pip.

CopyARG UBUNTU_VERSION=16.04
FROM ubuntu:$UBUNTU_VERSION

ARG PYTHON_VERSION=2.7.9

# Install dependencies
RUN apt-get update \
  && apt-get install -y wget gcc make openssl libffi-dev libgdbm-dev libsqlite3-dev libssl-dev zlib1g-dev \
  && apt-get clean

WORKDIR /tmp/

# Build Python from source
RUN wget https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz \
  && tar --extract -f Python-$PYTHON_VERSION.tgz \
  && cd ./Python-$PYTHON_VERSION/ \
  && ./configure --with-ensurepip=install --enable-optimizations --prefix=/usr/local \
  && make && make install \
  && cd ../ \
  && rm -r ./Python-$PYTHON_VERSION*

RUN python --version \
  && pip --version

ENTRYPOINT [ "python" ]
2 of 3
1

The simplest possible solution:

Copysudo apt-get install libssl-dev openssl
wget https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz
tar xzvf Python-2.7.5.tgz
cd Python-2.7.5
./configure
make
sudo make install

After installation completed set installed python as default one.

๐ŸŒ
Reddit
reddit.com โ€บ r/ubuntu โ€บ python2.7 on ubuntu 23.04
r/Ubuntu on Reddit: Python2.7 on Ubuntu 23.04
July 13, 2023 -

HI! I recently change my system from Ubuntu 22.04 to Ubuntu 23.04 because I built a new computer and had some trouble getting my system to run on the older kernel. Last week i tried to work on a project from work and i notice python 2 is not installed out of the box, tried to install it from the official Ubuntu repo with apt but found out it is not available anymore. Also tried to compile it from source with no luck either since tons of decencies are missing. Has anyone here manage to install Python2.7 on their machine? If so, could you help me?

๐ŸŒ
LinuxConfig
linuxconfig.org โ€บ home โ€บ install python 2 on ubuntu 22.04 jammy jellyfish linux
Install Python 2 on Ubuntu 22.04 Jammy Jellyfish Linux
January 17, 2022 - This tutorial will show how to install Python 2 for Ubuntu 22.04 Jammy Jellyfish. Python 2 has not been the default installed version on Ubuntu versions for a few years, but itโ€™s still possible to install Python 2 and to install Python 2.7 on Ubuntu 22.04.
๐ŸŒ
LinuxShout
linux.how2shout.com โ€บ home โ€บ how to install python 2.7 on ubuntu 20.04 lts
How to install Python 2.7 on Ubuntu 20.04 LTS - LinuxShout
June 18, 2024 - sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
๐ŸŒ
RoseHosting
rosehosting.com โ€บ home โ€บ how to install python on ubuntu 24.04
How to Install Python on Ubuntu 24.04 | RoseHosting
July 1, 2024 - Learn how to install Python on Ubuntu 24.04 using our step-by-step guide, or let us do the installation for you with any of our managed plans.