How to Install pip on Ubuntu 24.04 and Use Python venv

Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer
BeginnerUpdated Nov 17, 202614 min read~20 minutes total
PythonpipvenvpipxUbuntu 24.04DevOpsLinux
Last reviewed:
Tutorial
How to Install pip on Ubuntu 24.04 and Use Python venv
rafftechnologies.com/learn
On this page

Prerequisites

  • A Raff Linux VM running Ubuntu 24.04 with SSH access and a non-root user with sudo privileges

Build and run your Python projects on your own Ubuntu 24.04 server.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month

  1. 1Pick a Linux imageUbuntu, Debian and more, with your SSH key added.
  2. 2Choose a sizeFrom $4.99 a month. Resize when your app grows.
  3. 3Connect over SSHYour server is up in about 55 seconds.

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

Ubuntu 24.04 uses Python 3.12 by default, but application dependencies should not be installed into the operating system's Python environment with a normal global pip install. Ubuntu follows the externally managed environment model defined by Python packaging standards, so the safe default is to install python3-pip and python3-venv with APT, create a project-specific virtual environment, and run pip inside that environment.

On a Raff Linux VM running Ubuntu 24.04, this keeps your project packages separate from files managed by the operating system. It also makes deployments easier to reproduce: each application can have its own .venv, dependency file, and package versions without changing Python packages required by system tools.

This tutorial shows how to install pip on Ubuntu 24.04, create and verify a Python virtual environment, install packages safely, work with requirements files, use pipx for standalone Python command-line applications, troubleshoot externally-managed-environment, and clean up the test environment. It uses python -m pip inside the virtual environment so the pip command is tied to the Python interpreter you actually intend to use.

Prerequisites:

  • A Raff Linux VM running Ubuntu 24.04
  • SSH access with a non-root user that has sudo privileges
  • No public application port or firewall change is required for this tutorial

Step 1 — Verify the Ubuntu 24.04 Python Version

Check the default Python 3 interpreter:

Bash
python3 --version

Ubuntu 24.04 defaults to Python 3.12. The exact patch version can change through Ubuntu updates, so do not depend on a hard-coded 3.12.x patch number.

Check which executable is being used:

Bash
command -v python3 python3 -c 'import sys; print(sys.executable)'

On a standard Ubuntu installation, the system interpreter is normally under /usr/bin/.

Verify: python3 --version should report Python 3.12.x and sys.executable should point to the intended system Python before you create a virtual environment.

Step 2 — Install pip and venv with APT

Refresh package metadata and install the Ubuntu packages for pip and virtual environments:

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

Check pip through the Python interpreter rather than relying only on a standalone pip3 command:

Bash
python3 -m pip --version

Also verify that the venv module is available:

Bash
python3 -m venv --help >/dev/null && echo 'venv available'

Using APT here matters because Ubuntu manages the system Python installation. Avoid using get-pip.py to replace the distro-managed pip on a normal Ubuntu server.

Verify: python3 -m pip --version should succeed and the venv check should print venv available.

Step 3 — Understand the externally-managed-environment Error

Ubuntu 24.04 marks its system Python as externally managed. Python packaging tools use this marker to avoid modifying packages owned by the operating system.

Locate the marker without changing the system:

Bash
python3 - <<'PY' import os import sysconfig path = os.path.join(sysconfig.get_path('stdlib'), 'EXTERNALLY-MANAGED') print(path) print('exists:', os.path.exists(path)) PY

On Ubuntu 24.04, a normal system-level command such as:

Bash
python3 -m pip install requests

is expected to be rejected outside a virtual environment with an externally-managed-environment message.

pip provides --break-system-packages as an explicit override, but that bypasses the protection. Do not use it as the normal application-dependency workflow. Use a virtual environment instead.

Verify: The marker check should report that the externally managed marker exists on the distro-managed Python installation.

Step 4 — Create a Python Virtual Environment on Ubuntu

Create a project directory and a virtual environment named .venv:

Bash
mkdir -p ~/my-python-project cd ~/my-python-project python3 -m venv .venv

Python's standard venv module creates an isolated Python environment for the project. Unless --without-pip is specified, venv bootstraps pip into the environment.

Starting with Python 3.12, setuptools is no longer installed as a core venv dependency by default. Install it only when your project or build process actually requires it.

Inspect the environment:

Bash
ls -la .venv .venv/bin/python --version .venv/bin/python -m pip --version

Add the environment directory to Git ignore rules if this is a version-controlled project:

Bash
printf '.venv/\n' >> .gitignore

Verify: .venv/bin/python and .venv/bin/python -m pip should both run successfully, and .venv/ should be excluded from version control.

Step 5 — Activate the venv and Verify Which Python and pip You Are Using

Activate the environment in Bash:

Bash
source .venv/bin/activate

Check the active executables:

Bash
which python python --version python -m pip --version

The paths should now point inside ~/my-python-project/.venv/.

Using python -m pip is useful because it makes the relationship between the interpreter and pip explicit. It avoids accidentally invoking a pip executable from another Python installation on systems with multiple interpreters.

You do not have to activate a venv to use it. Automation and systemd units can call its interpreter directly:

Bash
~/my-python-project/.venv/bin/python -m pip --version

Verify: which python and the pip output should both reference the .venv directory.

Step 6 — Install and Verify a Python Package with pip

With the virtual environment active, install a small test dependency:

Bash
python -m pip install requests

Inspect the installed package:

Bash
python -m pip show requests

Verify that Python can import it:

Bash
python - <<'PY' import requests print('requests version:', requests.__version__) PY

Check the package location:

Bash
python -c 'import requests; print(requests.__file__)'

The path should be inside .venv, not the system Python directories.

This tutorial intentionally does not start a Flask development server or open a public firewall port. Installing pip and verifying a package does not require exposing an application to the internet.

Verify: requests should import successfully and its file path should be inside your project's .venv.

Step 7 — Record and Reinstall Dependencies with a Requirements File

For a simple pip-based project, you can record the current environment:

Bash
python -m pip freeze > requirements.txt

Inspect the file:

Bash
cat requirements.txt

A requirements file is one common way to reproduce an environment, but it is not the only dependency-management format in Python. Some projects use pyproject.toml, lock files, or another dependency tool. Commit the dependency files your project actually uses rather than treating requirements.txt as mandatory for every Python project.

Test that pip can parse the file:

Bash
python -m pip install -r requirements.txt

For deployment, review dependency changes before upgrading packages. Avoid a blind pip install --upgrade across every production dependency without testing the result.

Verify: requirements.txt should contain the installed dependency set and python -m pip install -r requirements.txt should complete without changing the system Python.

Step 8 — Use pipx for Standalone Python Command-Line Applications

A virtual environment is appropriate for libraries used by a project. For standalone Python command-line applications that you want available from your shell, pipx is usually a better fit because it creates a separate environment for each application.

Install pipx from Ubuntu's package manager:

Bash
sudo apt update sudo apt install -y pipx

Add the pipx application directory to your user PATH:

Bash
pipx ensurepath

You may need to open a new shell before the PATH change is visible.

Check pipx:

Bash
pipx --version pipx list

For example, a CLI package can be installed into its own managed environment with:

Bash
pipx install httpie

Remove the example when you are finished:

Bash
pipx uninstall httpie

Do not use pipx for libraries that your application imports. Those belong in the application's venv.

Verify: pipx --version should succeed, and pipx list should show only the standalone applications you intentionally installed.

Step 9 — Troubleshoot pip and venv on Ubuntu 24.04

externally-managed-environment

If pip shows this error, confirm that you are outside a virtual environment:

Bash
python3 -c 'import sys; print(sys.prefix); print(sys.base_prefix)'

If the two paths are equal, create or activate a venv:

Bash
python3 -m venv .venv source .venv/bin/activate python -m pip install <package>

Do not reach for sudo pip install or --break-system-packages as the default fix.

No module named venv or venv creation fails

Install the Ubuntu venv package:

Bash
sudo apt update sudo apt install -y python3-venv

Then retry:

Bash
python3 -m venv .venv

pip: command not found

Use the interpreter-bound form first:

Bash
python3 -m pip --version

If pip is missing from the Ubuntu system Python, install the package:

Bash
sudo apt install -y python3-pip

Inside a venv, use:

Bash
python -m pip --version

A package fails to build from source

pip prefers a compatible wheel when one exists. If no wheel is available, a source build may require a compiler or development headers. Read the package's build documentation and install only the system dependencies it actually requires instead of installing a large build-tool bundle preemptively.

The wrong Python receives the package

Compare the interpreter and package location:

Bash
which python python -c 'import sys; print(sys.executable)' python -m pip --version

All three should refer to the same virtual environment when it is active.

Verify: After troubleshooting, python -m pip --version and an import test should both resolve inside the intended .venv.

Step 10 — Run the Final End-to-End Verification

From the project directory with the environment active, run:

Bash
python --version python -m pip --version python -c 'import requests; print(requests.__version__)' python -c 'import requests; print(requests.__file__)' python -m pip check

pip check verifies whether installed packages have compatible declared dependencies.

Confirm that the environment is isolated from the system interpreter:

Bash
python - <<'PY' import sys print('prefix:', sys.prefix) print('base_prefix:', sys.base_prefix) print('isolated:', sys.prefix != sys.base_prefix) PY

Expected result:

Text
isolated: True

You now have the core Ubuntu 24.04 Python workflow: APT manages the system Python packages, venv isolates application dependencies, and pip installs packages inside that venv.

Verify: The import should succeed, pip check should report no broken requirements, and isolated should be True.

Step 11 — Deactivate or Clean Up the Test Environment

Leave the active virtual environment:

Bash
deactivate

Confirm that your shell has returned to the system interpreter:

Bash
command -v python3 python3 --version

If this was only a disposable test project, remove the entire project directory rather than trying to uninstall each virtual-environment package individually:

Bash
rm -rf ~/my-python-project

Only run that command if the directory contains no source code or files you need to keep.

If you want to rebuild a real project's venv, keep the source and dependency files, remove only .venv, and recreate it:

Bash
rm -rf .venv python3 -m venv .venv source .venv/bin/activate python -m pip install -r requirements.txt

Virtual environments are designed to be disposable and recreated rather than copied between machines.

Verify: The test .venv should be gone when cleanup is intended, while the Ubuntu system Python should continue to work normally.

Conclusion

You installed pip on Ubuntu 24.04 with python3-pip, created a Python 3.12 virtual environment with venv, installed packages without modifying the externally managed system Python, verified package isolation, recorded dependencies, and used pipx for standalone command-line applications.

The key rule is simple: let APT manage the operating system's Python environment and let project-specific virtual environments manage application dependencies. If you see externally-managed-environment, treat it as a signal to check whether you should be using a venv rather than as an error to bypass automatically.

Installing pip and creating a virtual environment prepares dependencies, but does not start a production web service. For a hosted application, run the framework under an application service and configure HTTPS at the reverse proxy.

For production Python deployments, continue with Deploy Django with Gunicorn and Nginx, Deploy FastAPI with Nginx and systemd, or Install Nginx on Ubuntu 24.04.

Deploy a Raff Ubuntu VM for your Python application, install packages in a venv, and follow the Django or FastAPI tutorial for service deployment.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month · 14-day money-back guarantee

Deploy Your Server
Was this article helpful?
Aybars Altınyay
Aybars Altınyay
Platform Engineer & Technical Writer

Aybars is a Platform Engineer & Technical Writer at Raff Technologies. He works on Linux VM templates, self-hosted application workflows, and security-focused deployment validation, turning tested infrastructure procedures into clear, practical tutorials for developers and teams.

Build and run your Python projects on your own Ubuntu 24.04 server.

Tested on 1 vCPU and 2 GB RAM: $8.99 a month

Ubuntu, Debian and other Linux distributions with full root and SSH access, up and running in about 55 seconds.

14-day money-back guarantee

Frequently asked questions

How do I install pip on Ubuntu 24.04?
Install Ubuntu's pip package with `sudo apt update && sudo apt install -y python3-pip`, then verify it with `python3 -m pip --version`. For application dependencies, create a virtual environment before using pip to install packages.
Why do I get externally-managed-environment on Ubuntu 24.04?
Ubuntu marks its system Python as externally managed so pip does not overwrite packages owned by APT. Create a venv with `python3 -m venv .venv`, activate it, and install application packages there.
How do I create a Python venv on Ubuntu 24.04?
Install `python3-venv`, run `python3 -m venv .venv`, then activate it in Bash with `source .venv/bin/activate`. Verify `which python` points inside `.venv`.
Should I use sudo pip install on Ubuntu?
Not for normal application dependencies. Let APT manage the system Python and use pip inside a virtual environment. `sudo pip` or `--break-system-packages` can bypass distro protections and should not be the default workflow.
How do I check if pip is installed on Ubuntu?
Run `python3 -m pip --version` for the system interpreter or `python -m pip --version` inside an activated virtual environment.
What is the default Python version in Ubuntu 24.04?
Ubuntu 24.04 defaults to Python 3.12. The exact patch release can change through Ubuntu updates, so scripts should avoid depending on one hard-coded 3.12.x patch version.
What is the difference between pip and pipx?
Use pip inside a project's virtual environment to install libraries that the project imports. Use pipx for standalone Python command-line applications because pipx gives each application its own managed environment while exposing its command on your PATH.
Does Python 3.12 venv install setuptools automatically?
No. Starting with Python 3.12, setuptools is no longer a core venv dependency. Install setuptools only when your project or build process actually requires it.