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
-
Install the venv and pip packages for the system Python:
sudo apt update sudo apt install -y python3 python3-venv python3-pip python3 --version -
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 -
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 -
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 -
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-failuredaemon-reload, then start the unit. The venv’s bin path puts the right Python first withoutactivatemagic.
Operational notes
python3 -m venv --upgrade-depsmakes a new venv with currentpipandsetuptools. On older bases, the bundled pip is months behind and surfacing dependency resolver bugs that were fixed upstream.- Never commit a venv to git.
.gitignoreshould includevenv/,.venv/,/opt/venvs/. Build the venv on the host (or in CI) fromrequirements.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.txtor adoptuv/pip-toolsfor a real lockfile. The barerequirements.txtis not a lock. python3-venvis a separate package on Ubuntu — thevenvmodule fails with aensurepiperror on a minimal install that has Python but notpython3-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.