How to Install Python on Debian 12

Python is a well-known programming language that is used in website development, ai applications, and automation, also it is popular because of its simplicity. It has different versions which can be installed on the new release of Debian known as Debian 12. 

This post will demonstrate different installation methods of Python on Debian 12 through the step-by-step guide. 

How to Check the Version of Installed Python on Debian 12?

First, find out whether Python has already been installed on Debian 12 or not. It can be checked by finding out the version of installed Python using the command:

$ python3 --version

The output confirms that Python has not been installed on Debian 12.

How to Install Python on Debian 12?

There are two different methods of installing Python on Debian 12:

  • Using the official repository
  • Building from source code

Both these methods are explained in detail in the next sections. 

Method 1: Using the Official Repository

The most convenient method of installing the packages on Debian 12 is by using the official repository. Follow the below-mentioned steps to install Python on Debian 12 by using the apt package manager:

Step 1: Update the Packages

Ensure all the packages are up to date by running the command:

$ sudo apt update

After making sure that all the packages are up to date, proceed with the installation of Python. 

Step 2: Install Python on Debian

Now install the Python package from the official repository of Debian 12 by using the apt package manager:

$ sudo apt install python3 -y

After the complete execution of the command, Python has been installed on Debian 12. 

Step 3: Verify the Installed Python 

To verify the installation of Python on Debian 12, display the version of installed Python:

$ python3 --version

The 3.11.2 version of Python has been installed on Debian 12. 

How to Uninstall Python on Debian 12

To uninstall Python with all its dependencies on Debian 12, execute the below-mentioned command:

$ sudo apt purge python3* -y

The Python package has been removed from Debian 12. 

Method 2: Building from Source Code

Another method to install Python on Debian 12 is by using the source code. This method will assure that the most recent version of Python has been installed on Debian 12. 

Follow the below-mentioned steps to install Python by building from its source code. 

Step 1: Create and Navigate to the Directory

First create the directory by using the “mkdir” command to build the source code of Python and also navigate to the newly created directory using the “cd” command:

$ mkdir ./Python && cd ./Python

Step 2: Download the Latest Version Source Code

Using the wget command, download the latest version of “Python” from its official website by running the command:

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

Step 3: Extract the tgz Package

Now extract the downloaded package of Python by using the tar command:

$ tar -xf Python-3.11.3.tgz

Step 4: Run the Optimization Test

Navigate to the extracted Python package using the “cd” command and run the optimization test using the “configure” command:

$ cd Python-3.11.3 && ./configure --enable-optimizations

Step 5: Build the Python Package

Now after the optimization test has been completed, build the Python source code:

$ make -j 12

When the process is completed, then install Python binaries by executing the command:

$ sudo make altinstall

Step 6: Display the Version of Python

Now after building the source code of Python, display the installed version of Python:

$ python3.11 --version

The 3.11.3 release of Python has been installed on Debian 12. 

Note: To remove the Python package from Debian 12, we can use the purge option of the apt command utility as explained in method 1. 

How to Test the Python Installation on Debian 12?

To test the Python installation on Debian 12, run the “Python” environment:

$ python3

Use the print script of “Python” to print a string:

print("Welcome! ItsLinuxFoss")

The string has been printed on the screen. 

Conclusion

To install Python on Debian 12, either use the package available in the default repository of Debian. Another method is to build the source code for installing Python. Both methods are easy and explained step-by-step in this post.