Quickly Install|Build Python from Source on Linux Ubuntu

Posted April 9, 2024
Quickly Install|Build Python from Source on Linux Ubuntu

There are many ways to install and build Python on your Linux machine. Is it Possible to Install and Build Python from Source on Ubuntu? You will answer these questions will all the steps you need to perfectly install and let you build Python from its Source on a Linux Ubuntu machine.

Building Python from source involves compiling the Python interpreter directly from its source code. This way you are not using pre-built binaries provided by package managers like apt or yum. Here you will Download the source code of Python and tailor the Python interpreter to your Ubuntu Linux machine.

This guide teaches you the how and about of Installing Python from the source. Ready? Buckle up and Quickly Install|Build Python from Source on Linux Ubuntu like a Pro.

Requisites to Install|Build Python from Source

Before diving into this step-by-step guide, ensure:

  • You have a specific Python version that you want to install from the Python Source.
  • Have internet connections.
  • You might need a terminal with sudo privileges.
  • Ensure your Linux Ubuntu package indexes are up to date.
sudo apt update
sudo apt upgrade -y

How to Install|Build Python from Source on Linux Ubuntu

Python has all versions and its source code available. This source code is typically distributed in compressed archive formats like .tar.gz/.tar.xz. This means you will select the version you want to install and download its related source code in a compressed directory.

A quick look at Python Source Release docs, you will have Python versions and their source code as follows:

Install|Build Python from Source on Linux Ubuntu

In each version, you will have:

  • Gzipped source tarball
  • XZ compressed source tarball

Here is where you get the source code for the Ubuntu version you want to run on your Linux Ubuntu machine. Let’s say I want to install and build Python 3.12.0 on my Ubuntu system. I will follow these steps.

Preparing to Install|Build Python from Source

To get your installation process right, first ensure you have the necessary development tools installed on your system:

sudo apt install -y \
    build-essential \
    libbz2-dev \
    libffi-dev \
    libgdbm-dev \
    liblzma-dev \
    libncurses5-dev \
    libnss3-dev \
    libreadline-dev \
    libsqlite3-dev \
    libssl-dev \
    tk-dev \
    uuid-dev \
    wget \
    zlib1g-dev

Once ready, Grap your Python version from the source and use wget to download the compressed files:

wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Install|Build Python from Source on Linux Ubuntu

You should have a Python-3.12.0.tgz file based on the path you execute the above wget command. An ls command should confirm so:

ubuntu@:~$ ls
Python-3.12.0.tgz

To get the download source code to install and build Python, extract Python-3.12.0.tgz:

sudo tar -xvf Python-3.12.0.tgz

Now, you have a directory containing the Python source code for version 3.12.0:

ubuntu@:~$ ls
Python-3.12.0 Python-3.12.0.tgz

A closer look at this directory should contain Python source files:

cd Python-3.12.0
ls

Install|Build Python from Source on Linux Ubuntu

Building Python From Source on Ubuntu

To Build this source code, Ubuntu will first need to configure the script for Makefile. Ubuntu will use Makefiles to install Python from the source.

By default, Ubuntu will install source builds on /usr/local. Now in your Python-3.12.0 path, use the following command to compile the Python source code into executable binaries (Makefiles):

sudo ./configure --enable-optimizations

Once the above command is completed, do an ls to check if you have the completed installation Makefiles:

Install|Build Python from Source on Linux Ubuntu

Now, Python is configured and you can build Python using make. The make reads instructions from a Makefile generated during the configuration step to orchestrate the compilation process.

compiling source code takes a lot of time. In this case, ran an -j$(nproc) flag to use multiple CPU cores for parallel compilation and speed up the process:

make -j$(nproc)

Installing Python From Source on Ubuntu

If the above build process is done, here is how to install Python From Source on Ubuntu.

You need to know if to use make altinstall or make install.

Now, the make altinstall command

  • Installs Python to the specified directory
  • It ensures source Python doesn’t overwrite or interfere with the system’s default Python.
  • The altinstall target differentiates Source from the install target.

You already know Ubuntu has a default Python. The make altinstall prevents overwriting the system in Python. This way, the default Python remains stable.

The default Python files are saved on your /usr/bin path. For example, if I ls the default Python path I will have:

ubuntu@:~$ ls /usr/bin/python*
/usr/bin/python3 /usr/bin/python3.10

Go ahead and install the source Python version to the /usr/local path (note /usr/bin), and here is where the make altinstall command comes in:

sudo make altinstall

Install|Build Python from Source on Linux Ubuntu

Python 3.12.0 will be installed from the source and Ubuntu should also install pip compatible with this version:

Install|Build Python from Source on Linux Ubuntu

Confirming If Python is Installed|Build from Source on Linux Ubuntu

To make sure you have installed Python from the source correctly, check the installed version:

python3 --version

You might get the default Python version. In this case, the Python version is checked on /usr/bin. Now, check the version based on the installed source path /usr/local/bin:

ubuntu@:~/$ /usr/local/bin/python3.12 --version
## Expected output
Python 3.12.0

Symbolic links allow you to use the installed Python from the Source version as the default Python interpreter on your system.

This step is optional. To add the links, use Python commands without specifying the full path to the installed interpreter binary as follows:

But first, ls to the installed source python to get the whole paths examples:

ls /usr/local/bin/python3*

Here, I have:

/usr/local/bin/python3 /usr/local/bin/python3.12 /usr/local/bin/python3.12-config

Now, my Python source Symbolic links will be:

sudo ln -s /usr/local/bin/python3.12 /usr/local/bin/python
sudo ln -s /usr/local/bin/python3.12 /usr/local/bin/python3

You successfully installed/built Python from Source on Linux Ubuntu. Clean up and remove the source code directory and any downloaded archives (Python installation remains on your system):

cd ..
sudo rm -rf Python-3.12.0
sudo rm Python-3.12.0.tgz

Conclusion

You’ve successfully built and installed Python from source on Linux Ubuntu. I hope you can install any Python3 version while using the Python source code and install the Python version tailored to your needs.

Quickly Install|Build Python from Source on Linux Ubuntu

Written By:

Joseph Chege