Python is renowned as one of the most widely used programming languages worldwide. It offers versatility, enabling the development of applications ranging from basic scripts to intricate machine-learning algorithms. Its intuitive syntax makes it a preferred choice for both novices and seasoned developers.

In this guide, we will explore the process of building Python from its source code specifically for Ubuntu 22.04. These instructions are applicable to other Ubuntu-based distributions as well, such as PopOS, Kubuntu, Linux Mint, and Elementary OS.

By default, Ubuntu 22.04 already includes Python 3. To check the version of Python installed on your system, simply type:

$ python3 --version

The resulting output should resemble the example below:

Output
$ Python 3.10.6

If you require additional Python versions installed on your system, or if you need to install multiple versions, it is recommended to build them from the source.

Installing Python on Ubuntu from Source

By compiling Python from the source, you gain the ability to install the latest version of Python and customize the build options. However, it’s important to note that this method won’t allow you to manage your Python installation through the apt package manager.

As of the time of writing this article, Python 3.11 represents the latest major release, offering significant speed improvements and introducing new features such as additional standard library modules, syntax enhancements, built-in features, and more.

The following steps outline the process of compiling Python 3.11 from the source. If you’re installing a newer release, make sure to modify the version number in the commands provided.

1. To begin, let’s install the necessary libraries and dependencies required for building Python:

$ 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. To obtain the source code of the latest release from the Python download page, you can use the wget command. Execute the following command to initiate the download:

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

3. Once the download is finished, extract the archive :

$ tar -xf Python-3.11.3.tgz

4. Access the Python source directory and execute the configure command, which performs various checks to ensure that all dependencies are available on your system:

$ cd Python-3.11.3
$ ./configure --enable-optimizations

By utilizing the –enable-optimizations option, the Python binary undergoes optimization through multiple tests during the build process. It’s important to note that enabling this option will increase the build time.

5. Initiate the build process by executing the following command:

$ make -j 12

To accelerate the build time, you can adjust the value of the -j flag to match the number of cores in your processor. To determine the number of cores, you can use the command “nproc”. Modify the -j flag as shown below to align it with the number of cores:

6. After the completion of the build process, you can install the Python binaries by entering the following command:

$ sudo make altinstall

To avoid overwriting the default system python3 binary, we are using “altinstall” instead of “install” in the command.

That’s all! The latest version of Python has been successfully installed on your system and is now accessible by executing “python3.11”. To confirm the installation, type the following command:

$ python3.11 --version

The output will display the version of Python installed on your system:

Output
$ Python 3.11.3
To use the default distro version, run python3.

Conclusion

In conclusion, you have learned how to build Python from the source on your Ubuntu 22.04 machine. With this knowledge, you can now proceed with developing your Python projects. Happy coding!

Comments to: How to Install Python on Ubuntu 22.04

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