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:
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:
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:
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:
python3 -m pip --version
Also verify that the venv module is available:
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:
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:
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:
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:
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:
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:
source .venv/bin/activate
Check the active executables:
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:
~/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:
python -m pip install requests
Inspect the installed package:
python -m pip show requests
Verify that Python can import it:
python - <<'PY' import requests print('requests version:', requests.__version__) PY
Check the package location:
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:
python -m pip freeze > requirements.txt
Inspect the file:
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:
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:
sudo apt update sudo apt install -y pipx
Add the pipx application directory to your user PATH:
pipx ensurepath
You may need to open a new shell before the PATH change is visible.
Check pipx:
pipx --version pipx list
For example, a CLI package can be installed into its own managed environment with:
pipx install httpie
Remove the example when you are finished:
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:
python3 -c 'import sys; print(sys.prefix); print(sys.base_prefix)'
If the two paths are equal, create or activate a venv:
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:
sudo apt update sudo apt install -y python3-venv
Then retry:
python3 -m venv .venv
pip: command not found
Use the interpreter-bound form first:
python3 -m pip --version
If pip is missing from the Ubuntu system Python, install the package:
sudo apt install -y python3-pip
Inside a venv, use:
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:
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:
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:
python - <<'PY' import sys print('prefix:', sys.prefix) print('base_prefix:', sys.base_prefix) print('isolated:', sys.prefix != sys.base_prefix) PY
Expected result:
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:
deactivate
Confirm that your shell has returned to the system interpreter:
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:
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:
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.
