Website Vidya
DevelopmentStep-by-Step Guide

How to Install Python and Pip on Ubuntu 24.04 LTS

Learn how to install python and pip on ubuntu 24.04 LTS, handle PEP 668 externally managed environment errors, use pipx, and set up venv environments.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Install Python and Pip on Ubuntu 24.04 LTS
AI Executive SummaryCORE CIS AI

5 min read

Learn how to install python and pip on ubuntu 24.04 LTS, handle PEP 668 externally managed environment errors, use pipx, and set up venv environments.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Checking Python 3 on Ubuntu 24.04
  • Step 2: Installing Pip and Virtual Environment Modules via APT
Grounded in full engineering runbookCORE CIS Ai Engine

Python is the dominant programming language for machine learning, data engineering, web backends, and cloud automation scripts.

To leverage the vast Python ecosystem—including PyPI (the Python Package Index) with hundreds of thousands of third-party libraries—developers rely on pip as the standard package installer.

Starting with modern Ubuntu distributions, Canonical and Debian implemented PEP 668, marking system-level Python installations as 'externally managed' to protect core operating system utilities from conflicting library installations.

In this guide, we show you how to install python and pip on ubuntu 24.04 LTS cleanly, navigate modern packaging standards, create project virtual environments, and install global tools with pipx.

Advertisement

Prerequisites and Supported Releases

This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.

Before proceeding, ensure you have the following requirements satisfied:

  • A server or desktop system running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • A user account with sudo administrative privileges.
  • Access to a command-line terminal shell.

Step 1: Checking Python 3 on Ubuntu 24.04

Ubuntu 24.04 comes with Python 3.12 pre-installed out of the box.

Verify that the system Python interpreter is installed and operational before proceeding:

Advertisement

Querying the version and path confirms that the standard Python binary is located in /usr/bin/:

# Check installed Python version:
python3 --version
# Expected output confirms: Python 3.12.3

# Confirm path to Python executable:
which python3
# Output confirms: /usr/bin/python3

Step 2: Installing Pip and Virtual Environment Modules via APT

While the base Python interpreter is included with Ubuntu, the pip package installer and virtual environment modules must be installed manually via APT.

Update your package repository index and install python3-pip and python3-venv:

APT downloads the official Debian/Ubuntu packaged version of pip and links the pip3 binary:

# Update APT repository index:
sudo apt update

# Install pip and venv support packages:
sudo apt install -y python3-pip python3-venv build-essential

# Verify installed pip version:
pip3 --version
# Expected output confirms:
# pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)

Step 3: Understanding PEP 668 and Externally Managed Environments

If you attempt to run pip3 install package_name globally in your shell on Ubuntu 24.04, you will encounter the following error:

Advertisement

error: externally-managed-environment

This safeguard was introduced in PEP 668 to stop pip from overwriting or corrupting system packages that Ubuntu's APT package manager relies on.

To respect this architectural safeguard, modern Python development offers two primary solutions: virtual environments for project dependencies, and pipx for standalone command-line tools.

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider.

Step 4: Creating and Managing Python Virtual Environments

Virtual environments create an isolated sandbox for your specific project, allowing you to install custom package versions without impacting the host system or other projects.

Create a new directory for your project and initialize a virtual environment:

Activate the environment using the source command and install packages freely using pip:

# Create a project workspace:
mkdir -p ~/my_project && cd ~/my_project

# Create a virtual environment named .venv:
python3 -m venv .venv

# Activate the virtual environment:
source .venv/bin/activate
# Notice your shell prompt now shows: (.venv) user@host:~/my_project$

# Inside the active environment, pip works without restriction:
pip install fastapi uvicorn requests

# Verify installed packages in this environment:
pip list

# Exit the virtual environment when done:
deactivate

Step 5: Installing Standalone CLI Tools with pipx

For standalone Python CLI tools that you want to execute from anywhere (such as black, flake8, poetry, or ansible), pipx is the official modern recommendation.

pipx creates an isolated virtual environment behind the scenes for each application while automatically exposing its command binary in your user's ~/.local/bin PATH.

Install pipx via APT and configure shell path integration:

# Install pipx from Ubuntu repositories:
sudo apt install -y pipx

# Configure PATH so pipx binaries are globally accessible:
pipx ensurepath

# Example: Install a CLI tool (e.g., black code formatter):
pipx install black

# Run the installed CLI tool directly from any shell:
black --version

# List all CLI applications managed by pipx:
pipx list

# Upgrade an application:
pipx upgrade black

Step 6: Managing Requirements and Package Freezing

To ensure reproducible builds across development, staging, and production environments, export your project's installed packages to a requirements.txt manifest file.

Inside your activated virtual environment, generate the dependency list using pip freeze:

Team members or deployment servers can then install the exact same package versions with a single command:

# Activate your project environment:
source ~/my_project/.venv/bin/activate

# Export pinned dependencies to requirements.txt:
pip freeze > requirements.txt

# Display the generated file:
cat requirements.txt

# In a new environment, install all listed packages:
pip install -r requirements.txt

Troubleshooting and Common Errors

When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:

error: externally-managed-environment when executing pip3 install

Cause: PEP 668 protects the host system against unversioned global pip installations that conflict with APT.

Resolution: Create an isolated virtual environment (python3 -m venv .venv && source .venv/bin/activate) or use pipx for CLI tools.

python3 -m venv .venv && source .venv/bin/activate

pip3: command not found after installing python3

Cause: The python3-pip package is not bundled by default with Ubuntu's minimal python3 metapackage.

Resolution: Install the package via APT with sudo apt install -y python3-pip.

sudo apt install -y python3-pip

pipx: command not found after running pipx install

Cause: ~/.local/bin is not yet present in your active shell's $PATH environment variable.

Resolution: Run pipx ensurepath and restart your shell session or run source ~/.bashrc.

pipx ensurepath && source ~/.bashrc

Security Best Practices and Hardening

Never execute sudo pip install --break-system-packages, as this will lead to dependency conflicts and potential failure of system utilities.

Use virtual environments for every project to avoid dependency pollution and library version collisions.

Regularly audit your project dependencies for known security vulnerabilities using tools like pip-audit.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
While the --break-system-packages flag exists, using it is strongly discouraged because it can overwrite Ubuntu core system libraries and corrupt package managers like apt.
On modern Ubuntu systems, pip3 specifically targets the Python 3 interpreter. pip is typically a symlink to pip3 once python-is-python3 is installed, but using pip3 or running python3 -m pip guarantees invocation of the intended Python 3 binary.
Use pipx when you want to install and use a standalone command-line application (such as Black, Ansible, or HTTPie) globally across your system. Use a venv when you are developing a project or writing code that imports Python packages as dependencies.
Activate the virtual environment with source .venv/bin/activate, then execute python -m pip install --upgrade pip safely without affecting the system Python installation.
RR
Written byVerified Author

Rajesh Ray

I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Uncategorized•

How to Redirect HTTP to HTTPS in Nginx

If you’re running a website today and it’s still accessible over plain HTTP, you’re already behind. Modern browsers flag non-HTTPS sites as “Not Secure,” search engines favor encrypted websites, and users are far more cautious about where t...

RR
Rajesh Rai
Read More
Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout