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.comWhat is the safest way to work with Python 2 on Ubuntu 24.04?
Python2.7 on Ubuntu 23.04
Videos
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.
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.
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
Create a new conda environment using python 2.7:
conda create -n py27 python=2.7where py27 is the name of the virtual environment.Activate the py27 virtual environment.
conda activate py27The prompt changes to show py27 instead of base. Py27 environment activated.
You're all set up. Now when you run
pythoncommand in the terminal you'll be executingpython 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.
First, install some dependencies:
sudo apt-get install build-essential checkinstall
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
Then download using the following command:
version=2.7.13
cd ~/Downloads/
wget https://www.python.org/ftp/python/$version/Python-$version.tgz
Extract and go to the directory:
tar -xvf Python-$version.tgz
cd Python-$version
Now, install using the command you just tried, using checkinstall instead to make it easier to uninstall if needed:
./configure
make
sudo checkinstall
Change version to whichever version you need (version=2.7.1 or version=3.6.0, for example).
Unless you really have a burning desire to compile it yourself, the preferred way is to use the DeadSnakes PPA to install versions of Python that aren't included by default:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python2.7
Other versions, such as python2.4 or python3.6, etc. are also available.
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!
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" ]
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.
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?