On this page
- Is it safe to run Python 2 today?
- Which systems still package Python 2?
- How do you run Python 2 code in a container?
- How do you build Python 2.7 with pyenv?
- How do you isolate dependencies for a Python 2 project?
- How do you run the legacy program as a service?
- How do you start porting the code to Python 3?
- Questions
Is it safe to run Python 2 today?
Only with care, because nothing in the Python 2 stack receives security fixes any more. The interpreter, its bundled ssl wrappers, and almost every third party library stopped updating years ago, and newer versions of pip, setuptools and most packages refuse to install under 2.7.
Reasonable rules for a legacy workload:
- Do not expose a Python 2 web process directly to the internet. Put it on
127.0.0.1behind a maintained reverse proxy such as Nginx that handles TLS. - Pin every dependency to the last version that supported 2.7 and keep the wheel files in your own archive, since old downloads can disappear.
- Run it as an unprivileged user or inside a container, never as root.
- Write down a target date for the port. A bridge that has no end date tends to become permanent.
Which systems still package Python 2?
Of the images we offer, only Ubuntu 22.04 still has a Python 2 package, in its universe component. Ubuntu 24.04, Debian 12 and 13, and Rocky Linux and AlmaLinux 9 dropped it entirely, so on those you need a container or a compiled interpreter.
# Ubuntu 22.04 only
sudo apt update
sudo apt install -y python2
python2 --version
curl -fsSL https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
python2 get-pip.py --user
python2 -m pip --versionThe special /pip/2.7/ path serves the last pip release that runs on 2.7 (pip 20.3.4). Avoid the plain get-pip.py, which only works on current Python 3. Keep in mind that standard support for Ubuntu 22.04 ends in 2027, so this route has a fixed horizon.
How do you run Python 2 code in a container?
A container is the cleanest option because the whole old userland travels with the code and the host stays current. Install Docker Engine first, following the Docker Engine tutorial, or deploy the Docker app image. The official python:2.7 tags are no longer rebuilt but can still be pulled.
mkdir -p ~/legacy && cd ~/legacy
cat > Dockerfile <<'EOF'
FROM python:2.7.18-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
USER nobody
CMD ["python", "main.py"]
EOF
docker build -t legacy-app:1 .
docker run --rm legacy-app:1 python --versionThe base image is built on Debian 10, whose package mirrors moved to the Debian archive, so apt-get inside it fails unless you point it at archive.debian.org. Install system libraries once, then save the built image with docker save legacy-app:1 | gzip > legacy-app-1.tar.gz so you never depend on that rebuild working again.
How do you build Python 2.7 with pyenv?
pyenv compiles interpreters into ~/.pyenv without touching system files, and it still carries the build recipe for 2.7.18. Install the build dependencies first.
# Ubuntu and Debian
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
libsqlite3-dev libffi-dev liblzma-dev libncursesw5-dev tk-dev git curl
# Rocky and Alma
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y openssl-devel bzip2-devel libffi-devel zlib-devel readline-devel \
sqlite-devel xz-devel ncurses-devel tk-devel git
curl -fsSL https://pyenv.run | bash
cat >> ~/.bashrc <<'EOF'
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
EOF
exec $SHELL -l
pyenv install 2.7.18
cd ~/legacy && pyenv local 2.7.18
python --version
python -c "import ssl; print(ssl.OPENSSL_VERSION)"The last line matters. Python 2.7 predates OpenSSL 3, which current distributions ship, so the build may finish without a working ssl module. If the import fails, any code that talks HTTPS will not work, and the container route above is the more practical answer.
How do you isolate dependencies for a Python 2 project?
The built in venv module does not exist in 2.7, so use virtualenv. Recent virtualenv releases can no longer create Python 2 environments; versions below 20.22 still can. Run that older virtualenv from a Python 3 environment so it does not pollute anything.
python3 -m venv ~/tools/venvtool
~/tools/venvtool/bin/pip install "virtualenv<20.22"
~/tools/venvtool/bin/virtualenv -p "$(pyenv which python2.7)" ~/legacy/.venv2
~/legacy/.venv2/bin/python --version
~/legacy/.venv2/bin/pip install -r ~/legacy/requirements.txtOn Ubuntu 22.04 with the distro package, pass -p /usr/bin/python2 instead. Keep a frozen requirements.txt with exact versions, and store copies of the wheels with pip download -r requirements.txt -d wheels/.
How do you run the legacy program as a service?
A systemd unit keeps the program running across reboots and limits what it can touch. This example runs the pyenv build as the deploy user, bound to localhost only.
sudo tee /etc/systemd/system/legacy.service >/dev/null <<'EOF'
[Unit]
Description=Legacy Python 2 app
After=network.target
[Service]
User=deploy
WorkingDirectory=/home/deploy/legacy
ExecStart=/home/deploy/legacy/.venv2/bin/python main.py --host 127.0.0.1 --port 8100
Restart=on-failure
NoNewPrivileges=true
ProtectSystem=full
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now legacy
sudo systemctl status legacy --no-pager
ss -ltnp | grep 8100For a container, docker run -d --restart unless-stopped -p 127.0.0.1:8100:8100 legacy-app:1 gives the same effect. Either way, do not open port 8100 in the firewall; only the proxy should reach it.
How do you start porting the code to Python 3?
Port in small, tested steps rather than one big rewrite. The futurize command from the future package rewrites code so it runs on both versions during the transition. It builds on lib2to3, which Python 3.13 removed together with the classic 2to3 tool, so give it a 3.12 interpreter (uv can fetch one on any of our images).
uv python install 3.12
uv venv --python 3.12 ~/tools/port
uv pip install --python ~/tools/port/bin/python future
cd ~/legacy
~/tools/port/bin/futurize --stage1 -w .
~/legacy/.venv2/bin/python -m unittest discover # still passes on 2.7?
~/tools/port/bin/futurize --stage2 -w . # second pass, review the diff before committingThe usual trouble spots are text versus bytes (every str that touches a socket or file), integer division, print statements, dictionary methods that now return views, and libraries that never got a Python 3 release. Once tests pass on both interpreters, switch the service to a Python 3 environment as described in installing Python 3, pip and venv, and retire the old build.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Run legacy Python 2 code
When did Python 2 reach end of life?
Support ended on 1 January 2020. The final release, 2.7.18, appeared in April 2020 and no further fixes, including security fixes, have been published since.
Can I install python2 with apt on Ubuntu 24.04 or Debian 12?
No. Both removed the package. Use a container, a pyenv build, or an Ubuntu 22.04 server while that release is still supported.
Why does pip fail with syntax errors under Python 2?
Current pip uses Python 3 syntax. Install the last compatible release from https://bootstrap.pypa.io/pip/2.7/get-pip.py, or use the pip already inside the python:2.7 image.
Is the official python:2.7 Docker image still maintained?
No. The tags remain downloadable but are not rebuilt, so their system packages are frozen. Build your image once, save it as a file, and keep it away from public ports.
Which plan suits a legacy Python 2 app?
Old Python 2 programs are usually small. The smallest plans run them well; choose 2 GB if you also compile 2.7 with pyenv or build container images on the same server.
More in Python
- Install Python 3, pip and venv
- Manage Python projects with Poetry
- Miniconda and Anaconda on a server
- Deploy Flask with Gunicorn and Nginx
- Deploy FastAPI with Uvicorn workers
- Deploy Django with Gunicorn and Nginx
- Background tasks with Celery and Redis
- Host a Python Discord bot
- Containerise a Python web app
- JupyterLab on a server
- Host a Python Telegram bot