How to Install Python from Source on Ubuntu (2026 Guide)

Standard package managers like apt are great for quick setups, but they often lag behind the latest releases. If you need a specific version of Python for a project or want to squeeze out extra performance using hardware-specific optimizations, building from source is the professional standard. In our testing, source builds allow for granular control over libraries and security flags that pre-packaged binaries often omit.

This guide walks you through the manual build process for Python on Linux systems. We focus on version 3.13.x and newer, ensuring your environment is ready for modern development workflows.

Reader Safety Note: Compiling software requires root access (sudo). Always verify scripts and source URLs before running them. A failed build can take up several gigabytes of disk space; ensure you have at least 5GB free before starting.

Quick Answer To install Python from source, update your system and install build-essential and development libraries. Download the .tgz file from python.org, extract it, and run ./configure --enable-optimizations. Finally, run make and sudo make altinstall to prevent overwriting your system’s default Python version.

python logo

Why Build Python from Source?

Most users should stick to apt or dnf. However, three specific scenarios make a source build necessary:

  • Bleeding Edge Access: Get new features the day they release, rather than waiting months for repository maintainers.
  • Custom Performance: Using the --enable-optimizations flag triggers Profile Guided Optimization (PGO), which can make Python run 10–20% faster for certain tasks.
  • Isolation: You can install Python into /usr/local/ without breaking the system Python that your OS relies on for basic tasks like networking or package management.

Build Tools Comparison

FeaturePackage Manager (apt)Source Build (make)
Speed of SetupSeconds10–30 Minutes
ControlLowHigh
PerformanceStandardOptimized (PGO/LTO)
UpdatesAutomaticManual Rebuild Required

Step-by-Step Installation

Follow these steps to build a reliable, high-performance Python environment.

1. Update and Install Dependencies

Your system needs a C compiler and several header files to build Python’s standard library modules (like SSL for security or SQLite for databases).

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev \
libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev \
wget libbz2-dev

2. Fetch the Source Code

Navigate to your /tmp or ~/Downloads folder. Use wget to pull the specific version you need. For this guide, we use Python 3.13.0.

wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tgz
tar -xzf Python-3.13.0.tgz
cd Python-3.13.0

3. Configure the Environment

The configure script checks your hardware and sets up the build rules. Using --enable-optimizations is highly recommended for production environments.

./configure --enable-optimizations

4. Compile and Install

The make command starts the compilation. This is the longest part of the process. If you have a multi-core processor, you can speed this up by using the -j flag followed by your number of cores (e.g., make -j 4).

Crucial: Use altinstall instead of install.

make
sudo make altinstall

Note: make altinstall prevents the new version from replacing the /usr/bin/python binary, which would likely break your operating system.

Advanced & Edge Cases

Using Link Time Optimization (LTO)

For maximum speed, you can add the --with-lto flag during configuration. This improves performance by optimizing the binary across different source files.

./configure --enable-optimizations --with-lto

Handling Missing Modules

If you see “The necessary bits to build these optional modules were not found” after running make, you are missing a system library.

  • Cause: Missing liblzma-dev or libdb-dev.
  • Fix: Install the missing -dev package via apt and run ./configure again.

Building for Low-Memory Systems

Compiling with --enable-optimizations runs a series of tests that consume significant RAM. If your build crashes on a small VPS:

  • Fix: Run ./configure without the optimizations flag or add a swap file to provide extra virtual memory.

Integration & Workflow

Virtual Environments

Even with a source build, never install packages globally. Use the built-in venv module to keep your projects isolated.

python3.13 -m venv my_project_env
source my_project_env/bin/activate

Security and Governance

When building from source, you are responsible for security updates.

  1. Monitor Releases: Subscribe to the Python Announce mailing list.
  2. Rebuild Regularly: When a security patch (e.g., 3.13.1) drops, you must repeat the download and make altinstall process to stay safe.

FAQs

Can I delete the source folder after installation? Yes. Once sudo make altinstall finishes, the binaries are moved to /usr/local/bin. You can safely delete the .tgz file and the extracted folder to save space.

Why does python --version still show the old version? This is intentional. Because you used altinstall, you must call the specific version (e.g., python3.13). If you want it to be the default, use a virtual environment or an alias.

Do I need to install pip separately? No. Modern Python source builds include ensurepip by default. You can use pip3.13 immediately after the installation.

Elsewhere On TurboGeek:  Arcserve Backup & Disaster Recovery: Bare Metal Restore Made Easy

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

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

Translate »