Ubuntu comes with Python 3 preinstalled — many system tools depend on it. The challenge isn’t installing Python, it’s getting the version your project needs without disturbing the one Ubuntu relies on. Here are the four options, from simplest to most flexible.
Check what you have
python3 --version
which python3
Ubuntu 24.04 ships Python 3.12; 22.04 ships 3.10. The command is python3; there’s no python command unless you install python-is-python3.
Golden rule: never remove, replace or pip install into the system Python. Tools like apt and cloud-init depend on it; breaking it can break the server.
Option 1: use the system Python (often enough)
If your project supports the version Ubuntu ships, install the extras you’ll need:
sudo apt update
sudo apt install -y python3-pip python3-venv python3-dev build-essential
Then work inside a virtual environment — see Python virtual environments:
python3 -m venv ~/myapp/.venv
source ~/myapp/.venv/bin/activate
pip install -r requirements.txt
On Ubuntu 23.04 and later, running pip install outside a virtual environment fails with externally-managed-environment — that’s intentional protection. See fixing pip’s externally-managed-environment error.
Option 2: deadsnakes PPA (other versions via apt)
The deadsnakes PPA packages additional Python versions for Ubuntu LTS releases. They install alongside the system Python without replacing it:
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y python3.13 python3.13-venv python3.13-dev
python3.13 --version
Use the versioned command to create environments: python3.13 -m venv .venv.
Notes:
- deadsnakes is a community-maintained PPA, not an official Ubuntu repository. It’s widely used, but security updates depend on its maintainers.
- It typically doesn’t package the version your Ubuntu release already ships.
- Don’t change
/usr/bin/python3withupdate-alternativesto point at the new version.
Option 3: pyenv (any version, per user)
pyenv builds Python from source in your home directory and lets you switch versions per project:
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev \
libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev curl git
curl -fsSL https://pyenv.run | bash
Add the init lines the installer prints to ~/.bashrc, restart the shell, then:
pyenv install 3.13
pyenv global 3.13 # default for your user
cd ~/myapp && pyenv local 3.12 # writes .python-version for this project
python --version
Compiling takes a few minutes per version. Missing -dev libraries lead to Pythons without ssl or sqlite3 support — install the build dependencies first.
Option 4: uv (fast, modern)
uv is a fast Python package and project manager that can also download standalone Python builds:
curl -LsSf https://astral.sh/uv/install.sh | sh
source ~/.bashrc
uv python install 3.13
uv venv --python 3.13
uv pip install -r requirements.txt
For new projects, uv init and uv add <package> manage pyproject.toml and a lock file. uv’s Python builds are prebuilt (no compiling), and installing packages is dramatically faster than pip.
Which should you choose?
| Situation | Choice |
|---|---|
| Project works with Ubuntu’s version | System Python + venv |
| Server app needing one other version, managed with apt | deadsnakes |
| Developer machine with many projects | pyenv or uv |
| Fast, reproducible installs and CI | uv |
| Containerised app | Official python:3.x-slim Docker image |
Running Python apps in production
Whatever the installation method, production apps should:
- live in a virtual environment owned by a non-root user;
- run under systemd (or Docker), not in an SSH session — see running a Python app with systemd;
- sit behind Nginx as a reverse proxy — see Django with Gunicorn and Nginx, Flask with Gunicorn or FastAPI with Uvicorn.
With pyenv or uv, reference the full interpreter path in systemd units (/home/app/myapp/.venv/bin/python), because the service doesn’t load your shell’s PATH.
Troubleshooting
python: command not found— usepython3, orsudo apt install python-is-python3.No module named venv/ensurepip is not available— installpython3-venv(orpython3.X-venvfor deadsnakes versions).ModuleNotFoundError: No module named '_ssl'after a pyenv build — installlibssl-devand rebuild.error: externally-managed-environment— use a virtual environment (orpipxfor CLI tools).
Python projects of every size run on our Ubuntu VPS plans with full root access.