How to Install Python on Ubuntu 22.04?

How to Install Python on Ubuntu 22.04?

Python is one of the most popular and versatile programming languages in the world. It is used for web development, data analysis, machine learning, scripting, and many other applications. In this article, we will show you how to install Python on Ubuntu 22.04, the latest LTS (long-term support) release of Ubuntu.

Check the Default Python Version

Ubuntu 22.04 comes with Python 3.10 preinstalled as the default Python version. You can check the Python version by typing the following command in your terminal:

python3 --version

The output should look something like this:

Python 3.10.6

If you need another or multiple Python versions installed on your system, you can build them from the source code or use a tool like pyenv to manage them.

Install Python from the Source Code

Compiling Python from the source code 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 code. If installing a newer release, change the version number in the commands below.

Step 1: Install the Required Dependencies

Before you can build Python from the source code, you need to install some libraries and tools that are necessary for the compilation process. To do so, run the following command as root or sudo user:

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

This will install all the required dependencies on your system.

Step 2: Download and Extract the Python Source Code

Next, you need to download the source code of the Python version you want to install from the official Python download page. You can use the wget command to download it:

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

Once the download is finished, extract the archive using the tar command:

tar -xf Python-3.11.3.tgz

This will create a directory named Python-3.11.3 that contains all the source files of Python.

Step 3: Configure and Build Python

Now, you need to navigate to the Python source directory and run the configure script. This script performs a number of checks to make sure all of the dependencies are present on your system and creates a Makefile that contains all the instructions for building Python:

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 but improves the performance of Python.

After running the configure script, you can start building Python by typing:

make -j 12

The -j option specifies how many jobs (commands) to run simultaneously. For faster build time, modify it to correspond to the number of cores in your processor. You can find out how many cores your processor has by typing nproc.

The build process may take several minutes depending on your system’s specifications.

Step 4: Install Python

When the build process is complete, you can install Python by typing:

sudo make altinstall

We’re using altinstall instead of install because we don’t want to overwrite the default system python3 binary.

That’s it! You have successfully installed Python on your Ubuntu system.

Verify Your Installation

To verify that Python has been installed correctly, type:

python3.11 --version

The output should show the Python version you have installed:

Python 3.11.3

You can also run an interactive Python shell by typing:

python3.11

This will allow you to execute Python commands and scripts interactively.

To exit the shell, type exit() or press Ctrl+D.

In this article, we have shown you how to install Python on Ubuntu 22.04 from the source code. You can now use Python to develop various applications and projects on your Ubuntu system.

If you have any questions or feedback, feel free to leave a comment below.

Leoz Dawn Avatar

Leave a Reply

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