Website Vidya
DevelopmentStep-by-Step Guide

How to Install Python on Ubuntu 24.04 LTS

Learn how to install python on ubuntu 24.04 LTS using default APT repositories, Deadsnakes PPA for specific versions, and compiling from source.

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

5 min read

Learn how to install python on ubuntu 24.04 LTS using default APT repositories, Deadsnakes PPA for specific versions, and compiling from source.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Checking the Default Pre-Installed Python Version
  • Step 2: Installing Python Development Headers and Standard Libraries
Grounded in full engineering runbookCORE CIS Ai Engine

Python is one of the world's most versatile, expressive, and widely utilized high-level programming languages, powering everything from machine learning models and web backends to system automation scripts and data analysis pipelines.

On Ubuntu systems, Python is not merely an optional developer runtime—it is an indispensable component of the core operating system, heavily leveraged by critical system tools such as apt, software-properties-common, and cloud initialization utilities.

Ubuntu 24.04 LTS (Noble Numbat) ships with Python 3.12 pre-installed out of the box in the main software repository.

In this guide, we walk through how to install python on ubuntu 24.04 across multiple installation strategies: using standard APT repositories, installing custom versions via the Deadsnakes PPA, building from source code, and creating PEP 668-compliant virtual environments.

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 computer or server running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
  • A non-root user account with sudo administrative privileges.
  • Terminal shell access.
  • Basic familiarity with command-line package management.

Step 1: Checking the Default Pre-Installed Python Version

Ubuntu 24.04 LTS includes Python 3.12 natively in the base installation image.

Verify the default installed Python 3 interpreter and its binary path using your terminal:

Advertisement

If the command returns Python 3.12.x, the core interpreter is already present and ready for development:

# Check installed Python 3 version:
python3 --version
# Expected output on Ubuntu 24.04:
# Python 3.12.3

# Locate the Python 3 binary executable path:
which python3
# Output confirms: /usr/bin/python3

Step 2: Installing Python Development Headers and Standard Libraries

While the base Python interpreter is pre-installed, developer libraries, C extension compilation headers, and the virtual environment module are packaged separately.

Install python3-dev and python3-venv from the official Ubuntu repositories:

These packages are essential for compiling native C-based Python packages and establishing isolated project environments:

# Update APT package lists:
sudo apt update

# Install Python 3 development headers and virtual environment support:
sudo apt install -y python3-dev python3-venv build-essential

# Verify virtual environment module availability:
python3 -m venv --help

Step 3: Installing Additional Python Versions via Deadsnakes PPA

Developers frequently require specific or legacy Python releases (such as Python 3.11, 3.10, or newer beta versions) to maintain compatibility with specific frameworks.

Advertisement

The trusted deadsnakes Personal Package Archive (PPA) provides pre-compiled Python packages for Ubuntu.

Add the repository and install your desired Python version:

Verify the newly installed secondary Python interpreter:

# Install repository management utility:
sudo apt install -y software-properties-common

# Add the official Deadsnakes PPA:
sudo add-apt-repository -y ppa:deadsnakes/ppa
sudo apt update

# Install a specific version (e.g., Python 3.11 with dev headers and venv):
sudo apt install -y python3.11 python3.11-venv python3.11-dev

# Verify installed Python 3.11 release:
python3.11 --version
# Expected output: Python 3.11.9

Step 4: Compiling Python from Source Code (Alternative Method)

Building Python directly from official source code allows you to enable compiler optimizations like Profile-Guided Optimization (PGO) and Link-Time Optimization (LTO).

First, install the necessary build toolchains and development compression libraries:

Download the source tarball, extract it, and execute make altinstall to avoid overwriting the default system Python binary:

# Install source compilation build dependencies:
sudo apt update
sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev \
  libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

# Download and extract desired Python source release:
cd /tmp
wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tar.xz
tar -xf Python-3.12.3.tar.xz
cd Python-3.12.3

# Configure build with optimizations:
./configure --enable-optimizations

# Compile and install using altinstall (CRITICAL: never run 'make install'):
sudo make -j $(nproc) altinstall

# Verify custom compiled binary:
/usr/local/bin/python3.12 --version

Step 5: Managing Multiple Python Versions with update-alternatives

If you maintain multiple Python versions on your Ubuntu workstation, use update-alternatives to set the default python command.

Register each installed interpreter with an associated priority weight:

You can interactively switch between installed interpreters whenever required:

# Register Python 3.12 and Python 3.11 in update-alternatives:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1

# Interactively configure the active default python3 binary:
sudo update-alternatives --config python3
# Select the number corresponding to your preferred default version.

Step 6: Creating and Activating Python Virtual Environments

Under PEP 668, Ubuntu 24.04 marks the system Python environment as 'externally managed' to prevent accidental system breakage from global pip install commands.

Always establish isolated project virtual environments to install third-party packages safely:

Deactivating the virtual environment returns your shell session to the global system scope:

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

# Create an isolated virtual environment named .venv:
python3 -m venv .venv

# Activate the virtual environment:
source .venv/bin/activate

# Verify isolated pip and python paths:
which python
# Output confirms: /home/username/my_project/.venv/bin/python

# Install project packages safely:
pip install requests flask

# Deactivate the environment when finished:
deactivate

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 running pip install

Cause: Ubuntu 24.04 enforces PEP 668 to prevent pip from overwriting OS-managed Python libraries.

Resolution: Create and activate a virtual environment (python3 -m venv .venv && source .venv/bin/activate) or use pipx.

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

No module named 'venv' when running python3 -m venv

Cause: The standard library venv module is packaged separately on Debian/Ubuntu systems.

Resolution: Install the python3-venv package using APT.

sudo apt install -y python3-venv

ModuleNotFoundError: No module named '_sqlite3' or '_ssl' after source build

Cause: Python was compiled from source without necessary development headers installed on the system.

Resolution: Install libsqlite3-dev and libssl-dev, then re-run ./configure and make altinstall.

sudo apt install -y libsqlite3-dev libssl-dev && cd /tmp/Python-3.12.* && ./configure && sudo make altinstall

Security Best Practices and Hardening

Never execute sudo pip install to install Python packages globally, as this can corrupt Ubuntu system administration utilities.

Always use make altinstall rather than make install when compiling custom Python versions to avoid replacing the default /usr/bin/python3 executable.

Keep the Deadsnakes PPA packages updated regularly via sudo apt update && sudo apt upgrade to receive upstream Python security patches.

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
Ubuntu 24.04 enforces PEP 668 ('externally managed environment') to prevent pip from overwriting or conflicting with system-level packages installed by APT, which could break core operating system tools.
No. Removing the default Python 3 package will break essential system tools like apt, software-properties-common, and the desktop environment.
make install overwrites or symlinks /usr/local/bin/python3, potentially overriding the system interpreter. make altinstall installs only the versioned binary (e.g., python3.12), leaving system defaults untouched.
Use pipx (installed via sudo apt install -y pipx && pipx ensurepath), which automatically isolates CLI tools into dedicated virtual environments while exposing their binaries globally.
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