How To Install Python On Ubuntu 22.
This article will explain how to build Python from the source code on Ubuntu 22.04.
Python 3 comes preinstalled by default on Ubuntu 22.04. To check the Python version installed on your system, type:
The output should look something like the below:
If you need another or multiple Python versions installed on your system, you should build it from the source.
Installing Python on Ubuntu from Source
Compiling Python from the source allows you to install the latest Python version and customize the build options. However, you won’t be able to maintain your Python installation through the apt package manager.
At the time of writing this article, the most recent version of the latest major release of Python is 3.11. This version includes many speed improvements and new features such as new standards library modules, new syntax and built-in features, and more .
The following steps explain how to compile Python 3.11 from the source. If installing a newer release, change the version number in the commands below.
First, install the libraries and dependencies necessary to build Python:
Download the latest release’s source code from the Python download page using the wget command:
Once the download is finished, extract the archive :
Navigate to the Python source directory and run the configure command. This script performs a number of checks to make sure all of the dependencies are present on your system:
The –enable-optimizations option optimizes the Python binary by running multiple tests. This makes the build process slower.
Start the build process:
For faster build time, modify the -j to correspond to the number of cores in your processor. You can find the number by typing nproc.
When the build process is complete, install the Python binaries by typing:
We’re using altinstall instead of install because the later command will overwrite the default system python3 binary.
That’s it. The latest Python has been installed on your system and is ready to be used by executing python3.11. To verify it, type:
The output will show the Python version:
To use the default distro version, run python3.
We’ve shown you how to build Python from the source on your 22.04 machine. You can now start developing your Python project.