Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Check the Python You Already Have

python3 --version
which python3

The first line prints the version, the second the path, usually /usr/bin/python3. Ubuntu 24.04 LTS ships Python 3.12 and Ubuntu 26.04 LTS ships Python 3.14. Each Ubuntu release keeps one main Python version for its whole life and receives security updates for it through apt.

Warning: do not remove this Python and do not point /usr/bin/python3 at another version. The package manager, cloud-init, and other system tools run on it, and replacing it can leave the server unable to install updates.

Install Python, pip, and venv

If python3 is missing, for example in a minimal container, or to add pip and venv:

sudo apt update
sudo apt install python3 python3-pip python3-venv

The same package names work on Debian 12 and 13. Package list updates are explained in sudo apt update.

The python Command

On Ubuntu, the command is python3; plain python gives "command not found". Scripts that call python work after you install a small package that makes it point to Python 3:

sudo apt install python-is-python3

Python 2 is no longer part of current Ubuntu releases.

The "Externally Managed Environment" Message

Running pip install outside a virtual environment stops with an error that says the environment is externally managed. This is intentional: since Ubuntu 23.04 and Debian 12, the system Python belongs to apt, and packages installed with pip could break the ones the system relies on. Three proper ways forward:

  • A virtual environment: for your own application and its requirements. This is the normal answer, shown below.
  • An apt package: for a library the system packages provide, such as sudo apt install python3-requests.
  • pipx: for a command-line tool written in Python. Install it with sudo apt install pipx, then pipx install and the tool's name; each tool gets its own environment.

Do not install packages with pip as root, and do not pass the option that overrides the check: both write into the system Python.

Create a Virtual Environment for an App

cd /var/www/app
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The prompt now starts with (.venv), and python and pip refer to the environment. Packages go into the .venv folder, not into the system. Leave it with deactivate. If python3 -m venv says that ensurepip is not available, the python3-venv package is missing.

Keep .venv out of Git with a line in .gitignore, and create it on the server rather than copying it from your computer.

Run the App from the Environment

A script, cron job, or service does not need to activate anything. Call the environment's own Python by its path:

/var/www/app/.venv/bin/python /var/www/app/main.py

In a systemd service, put the same path in ExecStart and set User to the account that owns the app folder, not root. Writing such a unit is covered in systemd unit files.

A Newer Python Version

If an application needs a newer Python than your release ships, install it next to the system one, never in its place:

  • A newer Ubuntu LTS: the cleanest option for a new server, because the version is then supported through apt.
  • The deadsnakes PPA: a third-party archive maintained outside Ubuntu, with other Python versions for Ubuntu LTS releases. Its maintainers do not promise timely security updates and leave production use at your own risk, so weigh that before adding it. It installs commands such as python3.13; create the venv with that command. Check its page first for the releases and versions it currently builds. If add-apt-repository is missing, see add-apt-repository command not found.
  • A container: the official Python images for Docker bring their own interpreter and leave the host untouched.

Whatever you choose, python3 keeps pointing at the system version, and each app's venv records which interpreter it was created with.

Was this answer helpful?
Loading...