How to Install Python 3.11 on Ubuntu 22.04

Published on

3 min read

Install Python on Ubuntu 22.04

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:

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.

  1. 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
  2. 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
  3. Once the download is finished, extract the archive :

    tar -xf Python-3.11.3.tgz
  4. 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.

  5. 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.

  6. 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.

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.