How to Install Python on Ubuntu 22.04
3 min read
Python is one of the world’s most popular programming languages. It is a versatile language used to build various applications, from simple scripts to complex machine-learning algorithms. With its simple and easy-to-learn syntax, Python is a popular choice for beginners and experienced developers.
This article will explain how to build Python from the source code on Ubuntu 22.04.
The same steps apply to all Ubuntu-based distributions, including PopOS, Kubuntu, Linux Mint, and Elementary OS.
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
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 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
Download the latest release’s source code from the Python download page using the
Once the download is finished, extract the archive :
tar -xf Python-3.11.3.tgz
Navigate to the Python source directory and run the
configurecommand. This script performs a number of checks to make sure all of the dependencies are present on your system:
--enable-optimizationsoption 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
-jto correspond to the number of cores in your processor. You can find the number by typing
When the build process is complete, install the Python binaries by typing:
sudo make altinstall
installbecause 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:
We’ve shown you how to build Python from the source on your 22.04 machine. You can now start developing your Python project.
Next, you can read about How to Use Pip and How to Create Python Virtual Environments for different Python projects.
If you have any questions or feedback, feel free to comment below.