Log inSign up

Python 3, pip and virtual environments on your server

Every supported image already ships Python 3, so the job is adding pip and the venv module: sudo apt install python3-pip python3-venv on Ubuntu and Debian, sudo dnf install python3-pip on Rocky Linux and AlmaLinux. Install project libraries inside a virtual environment rather than system wide, use pipx for command line tools, and reach for uv, the deadsnakes archive or a source build when you need a newer interpreter than the distribution provides.

Topic: Python · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Which Python version does each image ship?

The interpreter that comes with the image is tied to the release and receives security fixes for its whole life, which makes it the safe default for most applications.

  • Ubuntu 24.04: Python 3.12.
  • Ubuntu 22.04: Python 3.10.
  • Debian 12: Python 3.11.
  • Debian 13: Python 3.13.
  • Rocky Linux 9 and AlmaLinux 9: Python 3.9 as python3, with python3.11 and python3.12 available as extra packages in AppStream.

Check what you have before changing anything:

python3 --version
which python3
apt policy python3        # Ubuntu and Debian
dnf list --available "python3.1*"   # Rocky and Alma

Never replace or remove the system python3. Package tools such as apt helpers, dnf plugins and many admin scripts depend on it, and swapping the /usr/bin/python3 link to another version is a reliable way to break a server.

How do you install pip and venv on Ubuntu and Debian?

Install three packages from the main archive: pip, the venv module and the headers needed to compile native extensions. Log in as your sudo user first (the first SSH session tutorial creates one called deploy).

sudo apt update
sudo apt install -y python3-pip python3-venv python3-dev build-essential
python3 -m pip --version
python3 -m venv --help | head -n 3

Debian and Ubuntu split venv into its own package, so python3 -m venv fails with a message about ensurepip until python3-venv is present. The build-essential and python3-dev packages matter later when a library such as psycopg or cryptography has no prebuilt wheel for your platform.

How do you install pip on Rocky Linux and AlmaLinux 9?

On the RHEL family the venv module is part of the main package, so only pip and the build tools need installing.

sudo dnf install -y python3-pip python3-devel gcc
python3 -m pip --version

Python 3.9 is old for new projects. A newer interpreter sits in AppStream and installs side by side with the default one:

sudo dnf install -y python3.12 python3.12-pip python3.12-devel
python3.12 --version
python3.12 -m venv ~/venvs/api

The command python3 keeps pointing at 3.9, which is exactly what you want. Your application refers to the newer interpreter through its virtual environment, never through the system name.

What does the externally-managed-environment error mean?

It means the distribution has claimed the system Python for its own packages and pip refuses to write into it. Ubuntu since 23.04 and Debian since 12 mark their interpreter this way (the rule comes from PEP 668), so a plain pip install requests stops with error: externally-managed-environment. Rocky and Alma 9 do not set the marker yet, but the same advice applies there.

The fix is not to force it. Choose one of these instead:

  • A library for your own code: create a virtual environment and install into it.
  • A command line tool: use pipx install, which gives each tool its own private environment.
  • A library the system already packages: install the distro version, for example sudo apt install python3-requests.

The flag --break-system-packages exists, and the name is an honest description of what it can do. Files pip writes into /usr/lib/python3 can be overwritten by the next apt upgrade, or can shadow a module that a system tool needs.

How do you create and use a virtual environment?

Create one folder per project, make the environment inside it, then activate it whenever you work on that project.

mkdir -p ~/apps/report && cd ~/apps/report
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install requests
python -c "import requests; print(requests.__version__)"
pip freeze > requirements.txt
deactivate

While the environment is active, python and pip point at the copies inside .venv, so nothing touches the system. Scripts, cron jobs and systemd units do not need activation at all: call the interpreter by its full path, such as /home/deploy/apps/report/.venv/bin/python job.py. To rebuild an environment on another server, copy the code and requirements.txt, create a fresh .venv and run pip install -r requirements.txt. Do not copy the .venv folder itself, since it contains absolute paths.

How do you install Python command line tools with pipx?

pipx installs each application into an isolated environment and links its commands into ~/.local/bin, which keeps tools like linters, httpie or Poetry out of both the system and your projects.

# Ubuntu and Debian
sudo apt install -y pipx
# Rocky and Alma (from EPEL)
sudo dnf install -y epel-release && sudo dnf install -y pipx

pipx ensurepath
exec $SHELL -l
pipx install httpie
http --version
pipx list
pipx upgrade-all

If EPEL is not wanted on a RHEL family server, python3.12 -m pip install --user pipx works too, because those systems do not block user installs.

When is uv the better choice?

uv is a fast installer and project tool written in Rust that can also download ready built Python interpreters, which makes it handy when you need a specific version without touching system packages. It installs into your home directory as a single binary.

curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.local/bin/env
uv --version
uv python list
uv python install 3.13
mkdir -p ~/apps/scraper && cd ~/apps/scraper
uv venv --python 3.13
uv pip install httpx
.venv/bin/python -c "import sys; print(sys.version)"

For a full project you can also run uv init, uv add httpx and uv run main.py, which keep a lock file in the folder. Interpreters fetched by uv live under ~/.local/share/uv/python and are updated with uv python upgrade or by installing a newer patch release.

How do you get a newer Python on Ubuntu with deadsnakes?

The deadsnakes archive on Launchpad publishes extra Python versions for Ubuntu LTS releases, packaged the same way as the system one. It is a community project, not part of Ubuntu itself, and it is not meant for Debian.

sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt update
apt-cache search --names-only "^python3\.1[0-9]$"
sudo apt install -y python3.13 python3.13-venv python3.13-dev
python3.13 -m venv ~/venvs/new
~/venvs/new/bin/python --version

The archive does not publish the version Ubuntu already ships (3.12 on 24.04), and pip comes from the venv rather than a separate package. Install the -venv package for whichever version you add, or ensurepip will be missing.

How do you build Python from source with altinstall?

Building from the release tarball works on every distribution and gives you the exact version you want. Use make altinstall so the result lands as python3.X in /usr/local/bin and never overwrites python3. Check python.org for the newest release and set PYVER to it.

# Ubuntu and Debian build dependencies
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
  libsqlite3-dev libffi-dev liblzma-dev libncursesw5-dev tk-dev uuid-dev wget
# Rocky and Alma build dependencies
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 libuuid-devel wget

PYVER=3.14.2
cd /tmp && wget https://www.python.org/ftp/python/$PYVER/Python-$PYVER.tgz
tar xzf Python-$PYVER.tgz && cd Python-$PYVER
./configure --enable-optimizations --with-lto
make -j"$(nproc)"
sudo make altinstall
python3.14 -c "import ssl, sqlite3, lzma; print('modules ok')"

The optimised build runs the test suite for profiling, so expect it to take a while on a single vCPU. If the last line reports a missing module, install the matching development package and run configure again. A source build gets no automatic security updates: repeat the steps for each patch release.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1scripts and small tools1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A1-2room to compile wheels and a source Python1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months

Questions about Install Python 3, pip and venv

Should I use sudo pip install?

No. On Ubuntu and Debian it is blocked by the externally managed marker, and everywhere else it mixes pip files with distro files. Use a virtual environment, pipx or the distro package instead.

Why is the command python not found?

Modern images only ship python3. Inside a virtual environment python exists automatically. For the whole system on Ubuntu or Debian you can install python-is-python3, which adds the link without changing the interpreter.

Can I upgrade the system Python to a new major version?

Not safely. The system version only changes when you move to a new distribution release. Add newer versions next to it with AppStream packages, deadsnakes, uv or a source build, and point each project at the one it needs.

How much server do I need for Python work?

A script or small API runs in well under 1 GB of memory. Compiling Python from source or building heavy wheels such as NumPy from scratch is happier with 2 GB or more, or with a swap file on the smallest plans.

Where should application code live?

A common layout is /srv/appname or /home/deploy/apps/appname, owned by the user that runs the service, with the .venv folder inside it. The Flask with Gunicorn guide shows a full service layout.

Is uv safe to use next to pip?

Yes. It writes standard virtual environments, so pip inside a uv created environment still works, and you can switch tools later without rebuilding the project.