How to install Python on Ubuntu 24

To install Python on Ubuntu you usually install nothing: Python 3 comes with the system, because Ubuntu’s own tools are written in it. What you add is pip and venv, and sometimes another Python version. This guide is for Ubuntu 24.04 and 22.04 and explains the externally-managed-environment error that pip shows on 24.04.

Check the Python that Ubuntu already has

python3 --version

Ubuntu 24.04 prints Python 3.12.3, Ubuntu 22.04 Python 3.10.12. The command is python3, not python. If you want python to start Python 3 as well, install the small python-is-python3 package:

sudo apt install python-is-python3

Don’t remove or replace the system python3, and don’t point it to another version with update-alternatives. apt and other system tools run on it and break with a different version.

Install Python on Ubuntu: pip and venv

pip installs packages from PyPI, venv creates virtual environments for projects. On Ubuntu both come as separate packages:

sudo apt update sudo apt install -y python3-pip python3-venv python3 -m pip --version

How to fix error: externally-managed-environment

On Ubuntu 24.04, installing a package with pip outside a virtual environment stops with error: externally-managed-environment. This is PEP 668: the system Python belongs to apt, and pip would overwrite the packages Ubuntu itself relies on. Ubuntu 22.04 doesn’t show the error, but the advice is the same there. The fix is a virtual environment for each project:

mkdir -p ~/myproject cd ~/myproject python3 -m venv .venv source .venv/bin/activate pip install requests

While the environment is active, the prompt starts with (.venv) and python and pip work without the 3. Packages go into the project’s .venv folder. deactivate leaves the environment. Each time you come back to the project, activate it again with the same source line.

  • A package for the whole system: install it from Ubuntu if it is there, for example: sudo apt install python3-requests
  • A program written in Python (a command-line tool): use pipx, which gives each program its own environment. Install it: sudo apt install pipx Then add its folder to PATH: pipx ensurepath After that a program installs with: pipx install <name>
  • Don’t add --break-system-packages. The flag turns the protection off, and the next pip install may break apt’s own tools.

Install another Python version with uv

uv is a package and Python manager made by Astral. It downloads ready Python builds into your home folder, without sudo, and doesn’t touch the system Python. The install command is from uv’s documentation:

curl -LsSf https://astral.sh/uv/install.sh | sh

Open a new terminal so uv is on your PATH. Then install Python 3.13 and create a project environment with it:

uv python install 3.13 uv venv --python 3.13 source .venv/bin/activate python --version

uv creates .venv in the current folder with the Python you asked for. pyenv does a similar job by compiling Python from source. Its README lists the build packages Ubuntu needs for that.

Install another Python version from deadsnakes

deadsnakes is a PPA on Launchpad run by volunteers, not by Ubuntu or the Python developers. It carries Python versions that your Ubuntu release doesn’t ship, as apt packages next to the system one. Its page says outright that security updates may come late, so production servers use it at their own risk.

sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update sudo apt install -y python3.13 python3.13-venv python3.13 --version

The new version starts as python3.13, python3 stays the system one. Create environments with it the usual way:

python3.13 -m venv .venv

If Python or pip doesn’t work

  • Command 'python' not found, did you mean:: Ubuntu has only python3. Type python3, or install python-is-python3.
  • The virtual environment was not created successfully because ensurepip is not available.: venv isn’t installed. Install it: sudo apt install python3-venv For a deadsnakes version, install its own package, such as python3.13-venv.
  • pip: command not found: install python3-pip, or use pip inside an active environment. Where pip alone doesn’t work, start it through Python: python3 -m pip
  • ModuleNotFoundError: No module named for a package you installed: it went into another environment. Activate the project’s .venv and check that this path points inside it: which python

If you also work on a Mac

MacWashing, System Junk: Xcode junk, user caches, installers and logs found, 22.83 GB ready to be cleaned

System Junk in MacWashing finds the caches of every app it knows, including those hidden inside app data, plus logs, Xcode junk and leftovers of deleted apps. It shows how much each takes and clears them in one go. Anything debatable stays unchecked until you decide.

Free trial · macOS 14 or later

Questions and answers

How do I install Python on Ubuntu?

Python 3 is already there. Check it: python3 --version Add pip and venv: sudo apt install python3-pip python3-venv Install project packages inside a virtual environment.

Can I just use sudo pip install?

No. It writes into the system Python that apt manages, and on Ubuntu 24.04 pip refuses anyway. Use a venv for projects, apt for system packages and pipx for programs.

Which way to get a newer Python is better, uv or deadsnakes?

uv works in your home folder, needs no sudo and has every current version. deadsnakes gives apt packages for the whole system, but it is a third-party PPA with no promise of quick security fixes.