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-essentialand development libraries. Download the.tgzfile from python.org, extract it, and run./configure --enable-optimizations. Finally, runmakeandsudo make altinstallto prevent overwriting your system’s default Python version.

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-optimizationsflag 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
| Feature | Package Manager (apt) | Source Build (make) |
| Speed of Setup | Seconds | 10–30 Minutes |
| Control | Low | High |
| Performance | Standard | Optimized (PGO/LTO) |
| Updates | Automatic | Manual 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-dev2. 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.03. 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-optimizations4. 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 altinstallNote:
make altinstallprevents the new version from replacing the/usr/bin/pythonbinary, 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-ltoHandling 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-devorlibdb-dev. - Fix: Install the missing
-devpackage viaaptand run./configureagain.
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
./configurewithout 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/activateSecurity and Governance
When building from source, you are responsible for security updates.
- Monitor Releases: Subscribe to the Python Announce mailing list.
- Rebuild Regularly: When a security patch (e.g., 3.13.1) drops, you must repeat the download and
make altinstallprocess 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.

Recent Comments