Posted on July 10, 2023 by nexonhost
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:
python3 --version
The output should look something like the below:
Python 3.10.6
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:
sudo apt updatesudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
Download the latest release’s source code from the Python download page using the wget command:
wget https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tgz
Once the download is finished, extract the archive :
tar -xf Python-3.11.3.tgz
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:
cd Python-3.11.3./configure --enable-optimizations
The –enable-optimizations option optimizes the Python binary by running multiple tests. This makes the build process slower.
Start the build process:
make -j 12
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:
sudo make altinstall
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:
python3.11 --version
The output will show the Python version:
Python 3.11.3
To use the default distro version, run python3.
Conclusion
We’ve shown you how to build Python from the source on your 22.04 machine. You can now start developing your Python project.