Logic Encoder Blog

Install Python 3, pip and venv on Ubuntu 22.04 and 24.04

Sep 11, 2025 Logic Encoder 4 min read

Install Python 3, pip and venv on Ubuntu

Ubuntu already includes Python 3, and parts of the system depend on it. In most cases you do not install a new interpreter. You add the tools for virtual environments and install your project’s packages inside one.

In this guide
sudo apt update
sudo apt install python3-venv python3-pip
python3 -m venv .venv
source .venv/bin/activate
python -m pip install requests

Which Python version you get

Ubuntu releaseDefault python3Upstream status of that version (November 2026)
22.04 LTS (jammy)3.10End of life since 1 October 2026
24.04 LTS (noble)3.12Security fixes only, until October 2028

Sources: Python versions in Ubuntu and the CPython version status page. The end-of-life date is the Python project’s. Ubuntu maintains its own packages on its own support schedule, but new third-party packages will gradually stop supporting 3.10.

Check your own system:

. /etc/os-release; echo "$PRETTY_NAME"
python3 --version
command -v python3

Install the packages

python3-venv provides virtual environments and python3-pip provides pip for the distribution’s Python. Ubuntu’s Python setup guide also offers python3-full, a single package with the complete standard library, venv and IDLE:

sudo apt install python3-full

Add python3-dev and a compiler (build-essential) only when a package has to be built from source. Installing them does not by itself fix every build error; read which header or library the error names.

Create and use a virtual environment

mkdir -p ~/projects/demo
cd ~/projects/demo
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install requests
python -c "import requests; print(requests.__file__)"
deactivate

The printed path should lie inside .venv. Activation only changes your current shell; you can also call .venv/bin/python directly, which is the safer form in cron jobs and services.

On Ubuntu 22.04.5 a new environment started with pip 22.0.2 in our check, so the upgrade line matters if you need a newer pip option. Add .venv to .gitignore. An environment is not meant to be moved or copied to another location; delete it and create it again from your dependency files.

error: externally-managed-environment

Ubuntu 24.04 marks its Python as externally managed: the standard library package ships a file named EXTERNALLY-MANAGED (file list). pip then refuses to install into that interpreter outside a virtual environment and reports error: externally-managed-environment. This protects packages that apt manages. Our 22.04.5 system had no such marker file.

Pick the option that matches what you are installing:

You needUse
A library for your own projectA virtual environment, then python -m pip install
A library for system scriptsThe distribution package, usually sudo apt install python3-NAME
A command-line application written in Pythonsudo apt install pipx, then pipx install NAME and pipx ensurepath

pip has a --break-system-packages option that overrides the protection. The specification behind it intends it to be treated as risky, and the name is accurate. Do not combine it with sudo pip to silence the message.

ensurepip is not available

If python3 -m venv stops with a message that ensurepip is not available, the venv package is missing:

sudo apt install python3-venv
rm -rf .venv
python3 -m venv .venv

Remove only the half-created environment directory, not other files in your project.

The python command is not found

Ubuntu’s command is python3. Inside an activated virtual environment, python works. If you want it system-wide as well, Ubuntu provides a package that creates the link:

sudo apt install python-is-python3

Do not repoint /usr/bin/python3 to another version and do not remove the python3 package. Ubuntu warns that removing the default installation breaks system tools.

You need a different Python version

Leave the system interpreter in place and add the other version beside it. pyenv, which Ubuntu’s guide mentions, builds and switches versions in your home directory; uv can also install interpreters for a project. Before using an extra version from the Ubuntu archive or a third-party repository, check the exact release it contains with apt-cache policy and who maintains it.

Check that everything works

cd ~/projects/demo
.venv/bin/python --version
.venv/bin/python -m pip --version
.venv/bin/python -m pip check

pip check confirms that the installed packages’ declared dependencies are compatible. It does not prove that your program works, so run it as well.

Related guides

If a module cannot be imported after installation, see checking the interpreter and environment behind Python import errors. On Windows, you can run Ubuntu through WSL2.

The commands follow the venv documentation and the Ubuntu pages linked above. The 22.04 observations come from a 22.04.5 system; we did not run the 24.04 steps on a 24.04 machine.

Share this post

Previous WSL 2 Advantages and Limits: How It Differs from WSL 1 Next WSL2 Backup and Restore: Export Safely and Verify Recovery

Leave a comment

Your email address will not be published. Required fields are marked *