Skip to content

Installing Python 3 and managing venvs on Ubuntu/Debian

Pick the right Python on Ubuntu, install pip and venv, and structure per-project virtual environments so a system update never breaks a running service.

Python is installed everywhere already, which is part of the problem. Ubuntu’s python3 is a tool the operating system itself uses for apt, cloud-init, and a half-dozen subsystems — running pip install against it is a foot-gun. This article installs a clean Python and lays out per-project venvs so deployments and OS updates stay independent.

How to verify

Check what’s on the host before touching anything:

which python3 && python3 --version
ls /usr/bin/python3* /usr/local/bin/python3* 2>/dev/null
dpkg -l | grep -E "^ii\s+python3(\.[0-9]+|-venv|-pip)" | head
apt-mark showmanual | grep -i python
test -d /opt/venvs && ls /opt/venvs

If dpkg -l | grep python3-pip is empty but pip3 --version works, somebody bypassed apt — likely a get-pip.py run. That installs into /usr/local/lib/python3.X/dist-packages and creates a divergence the next apt upgrade will not see.

What’s happening

On Ubuntu 22.04 the system Python is 3.10; on 24.04 it is 3.12. The packages python3-venv and python3-pip are not installed by default on the minimal images. A venv is just a directory containing a pyvenv.cfg, a small bin tree with a Python symlink, and an empty site-packages — the Python interpreter, when launched from the venv’s bin/python, inserts the venv’s site-packages into sys.path and isolates installs from the system.

For production hosts the rule is straightforward: never pip install against /usr/bin/python3. Every service gets its own venv under a stable path like /opt/venvs/<service>/. The systemd unit invokes /opt/venvs/<service>/bin/python -m <module>, never the bare python3. When the OS upgrades 3.10 to 3.12, your venv keeps pointing at 3.10 binaries (because the venv links to a specific interpreter) until you intentionally rebuild.

The procedure

  1. Install the venv and pip packages for the system Python:

    sudo apt update
    sudo apt install -y python3 python3-venv python3-pip
    python3 --version
  2. If the app needs a Python the distro does not ship (3.13 on 22.04, for example), add the deadsnakes PPA and install side-by-side:

    sudo add-apt-repository -y ppa:deadsnakes/ppa
    sudo apt update
    sudo apt install -y python3.13 python3.13-venv
    python3.13 --version
  3. Create a venv under a stable per-service path. Do not put it inside the project’s git tree on production hosts — separating the venv from the code makes pulls atomic.

    sudo mkdir -p /opt/venvs
    sudo chown deploy:deploy /opt/venvs
    sudo -iu deploy
    python3.13 -m venv /opt/venvs/api
  4. Activate, upgrade pip, and install dependencies:

    source /opt/venvs/api/bin/activate
    pip install --upgrade pip setuptools wheel
    pip install -r /var/www/app/requirements.txt
    deactivate
  5. Reference the venv’s interpreter directly from systemd — do not source activate scripts inside units:

    # /etc/systemd/system/api.service
    [Service]
    User=deploy
    WorkingDirectory=/var/www/app
    Environment=PYTHONUNBUFFERED=1
    ExecStart=/opt/venvs/api/bin/python -m gunicorn app:app -b 127.0.0.1:8000
    Restart=on-failure

    daemon-reload, then start the unit. The venv’s bin path puts the right Python first without activate magic.

Operational notes

  • python3 -m venv --upgrade-deps makes a new venv with current pip and setuptools. On older bases, the bundled pip is months behind and surfacing dependency resolver bugs that were fixed upstream.
  • Never commit a venv to git. .gitignore should include venv/, .venv/, /opt/venvs/. Build the venv on the host (or in CI) from requirements.txt.
  • After an OS upgrade that moves the system Python (3.10 → 3.12), existing venvs still work because they link to the old binary, but they are uninstallable replacements. Plan a venv rebuild as part of the OS upgrade ticket.
  • For locked dependencies use pip install -r requirements.txt && pip freeze > requirements-lock.txt or adopt uv / pip-tools for a real lockfile. The bare requirements.txt is not a lock.
  • python3-venv is a separate package on Ubuntu — the venv module fails with a ensurepip error on a minimal install that has Python but not python3-venv. Install both together.

For deploying these venvs behind a WSGI server, the next article is Gunicorn deployment for Python. Stack Harbor’s managed operations practice keeps a venv per service under /opt/venvs/ on every host we run, with apt-pinned Pythons that we upgrade on our schedule, not the distro’s.