How to Install Python 3.9 on Debian 11

Python is a free, open-source programming language that was originally published in 1991 and has since gained widespread adoption. It’s used for general-purpose programming which makes it perfect to create apps on your phone or computer.
In this article, we’ll cover all you need to know and do to successfully install Python 3.9 on Debian 11.

Prerequisites

In order to install python 3.9 on your Debian 11 the server must meet the following prerequisites:

  • Acess to root
  • A fresh Debian 11 server installed and updated to the latest packages.

Updating your system and installing dependencies.
The first step is to update your system if it’s not updated yet.

apt update -y
apt upgrade -y
apt dist-upgrade -y

Once the update is done, you can proceed with the dependencies installation. So you can run this command to install all the required dependencies:

apt install wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev -y

Installing Python 3.9 on Debian 11

First of all, we need to download the source from python so we can proceed with the manual installation:

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

When the download finishes, you can extract the files and enter them on the extracted directory:

tar xvf Python-3.9.9.tgz
cd Python-3.9.9/

And once you are inside the Directory, you need to run the configure command and the make:

./configure --enable-optimizations
make altinstall

After running the make command, the installation will proceed and once it’s finished you can check the version installed running the following command:

python3 --version

Output will be as follows:

If you don’t get any errors while installing Python 3.9, the command should work without any problem and print the latest Python version installed on your system.
To install Python modules, you need to run the following command inside your Python 3.9 directory.

python3.9 -m pip install <module>

Where: <module> is the name of a module to be installed, for example sphinx, PyMySQL etc. You can get a list of all Python modules available for installation on its official website.
For example, let’s install the sphinx module:

python3.9 -m pip install sphinx

Testing the Python Installation

Now that Python 3.9 is installed, let’s run a simple hello world with the Python Interpreter and see that it works
You can launch the Python Interpreter from the bin directory of your Python installation with the following command.

python3

This will launch the Python interpreter and you’ll see a >>> prompt to enter Python commands.
In the interpreter, you can enter your Python code and see the result of its execution in real-time. Let’s run a simple hello world example. Enter the following print statement in the interpreter.

print("Howtofore, Hello World")

Once you enter the command and hit Enter, you’ll see that it prints the Howtofore, Hello World message on your terminal.

To exit the Python Interpreter, you can enter the following command.

exit()

Congratulations! You have successfully installed Python 3.9 on your Debian 11. You can now start writing Python code and executing it with the interpreter.