Debian

How to Install Python 3.9 on Debian 11

Python 3.9

Python is arguably one of the most popular and versatile programming languages. It’s a multi-purpose language and provides a myriad of use cases such as the development of full stack web apps, artificial intelligence, web scraping, and visualization of data in cool dashboards in data science.

Currently, Python 3.9.7 is the latest version of Python and provides quite a number of benefits including flexible variable and function notations, union operators in dict, support for IANA timezone and so many others.

In this guide, we will walk you through the installation of Python 3.9 on Debian 11.

Step 1: Install Python3 dependencies

This walkthrough will require us to compile Python from source. This allows us to install the latest version of Python and further customize build options.

To get started, update the package index.

$ sudo apt update

Next, install the dependencies and libraries required to build Python.

$ sudo apt install build-essential libncurses5-dev zlib1g-dev libnss3-dev libgdbm-dev libssl-dev libsqlite3-dev libffi-dev libreadline-dev curl libbz2-dev

This installs a host of additional packages and other dependencies.

Once the installation of Python dependencies is complete, the next step will be to download the latest Python 3.9 installation file.

Step 2: Download and install the latest Python 3.9

Next, we will download the gzipped source code file from the Official Python release and download page as shown.

So, to download the tarball file, run the command:

$ sudo wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz

Next, extract the compressed file as follows.

$ tar -xvf Python-3.9.1.tgz

After the extraction, you will now have 2 files: the tarball file and the decompressed folder ( Python-3.9.7)

To compile Python from source navigate to the uncompressed folder.

$ cd Python-3.9.1

Then run the configure script as shown.

$ /configure --enable-optimizations

This runs multiple tests and takes quite some time. In my case, this took approximately 30 minutes. Here is some sample output.

Then commence the build process.

$ make -j 2

The -j option specifies the number of CPU cores. You can check out the number of cores on your Linux system by executing the nproc command.

$ nproc

Here’s some sample output of the build process.

Afterward, run the following command to install Python binaries.

$ sudo make altinstall

This finalizes the installation of Python 3.9.7 on your system.

Step 3: Testing the installation

You can confirm the version of Python installed using the command:

$ python3.9 --version

You can access Python 3.9 shell using the command

$ python3.9

To give it a test run, we will write a simple program to find the sum of two variables, x and y.

x = 35

y = 75

z = x + y

print("Hello, the sum of x and y is", +z)

Output

Hello, the sum of x and y is 110

And there we go! Python 3.9.7 is now installed on Debian 11.

Similar Posts