Python is a well-known high-level programming language used for web development, artificial intelligence, data analysis, and many more. It provides a lot of standard libraries and packages that have pre-written code for certain tasks on the system.
Many Linux distributions including Debian come with Python pre-installed as many systems’ packages depend on Python. However, you can also easily install and manage additional versions of Python on your system.
This article will illustrate various methods for installing Python on Debian 12 systems.
Overview
- How to Install Python on Debian 12
- How to Run Python Scripts on Debian 12
- How to Fix Debian 12 System If Python is Removed
- Final Thoughts
How to Install Python on Debian 12
Python can be installed on Debian 12, through the following methods:
Method 1: Install Python on Debian 12 From Default Debian Repository
The default Debian repository consists of a wide range of software packages including Python. However, these packages are a bit outdated. You can install Python from the Debian repository by going through the following instructions:
Step 1: Update Debian Packages
First, refresh the Debian packages list through the given command:
Step 2: Install Python on Debian 12
Then, install Python on your Debian 12 system from the Debian repository by typing out the provided command:
The below output indicates that the latest version provided by the Debian repository is already installed on the system:
Step 3: Verification
To ensure that Python is installed on the system, check its version:
Method 2: Install Python on Debian 12 From Source Code
The official Python website has various Python source packages to get its latest version or any desired version. You can download the required source package and directly install Python on your Debian 12 system from the source code. Check out the following instructions for Python installation from the source code:
Step 1: Install Required Dependencies
First, you must install the basic dependencies and build tools that are necessary for extracting and installing the source code file via the following command:
Step 2: Download Python’s Source Code File
Then, visit the official Python website, copy the most recent source package file’s link, and download it through the provided command:
Here, we are downloading the source code file of the latest Python version i.e. “3.12.2”.
Alternatively, download the latest Python source code file directly from the website:
Step 3: Extract Source Code
After that, utilize the “tar -zxf” command along with the downloaded Python source code file to unzip it:
Step 4: Start Configuration Process
Now, redirect to the extracted directory i.e. “Python-3.12.2” using the “cd” command and type out the below-listed command to start the configuration process:
Here, the “–enable-optimizations” option is used to allow additional optimization to improve the performance during the build process and enhance Python’s performance.
Step 5: Compile Source Code
Next, start the build process with the help of the “make” command. You can also utilize the “-j” flag along with the specific number to optimize the build process through parallel compilation:
Step 6: Install Python on Debian 12
Once done with the compilation process, run the given command for Python installation on your Debian 12 system:
This will install the Python’s most up-to-date version without interpreting the default system Python:
Step 7: Verify Python Installation
Finally, check the Python’s version to ensure that it is installed successfully:
Method 3: Install Python on Debian 12 Via Deb Package Files
The Deb package files are also available for installing the latest or any specific version of Python. You can install Python from the Deb package files by following the given steps:
Step 1: Download and Install Dependencies
First, you must download and install the necessary dependencies for installing Python from the deb package files. These dependencies include “Python3.12-minimal”, “libpython3.12-stdlib”, and “libpython3.12-minimal”. You can download these through the “wget” command:
For “Python3.12-minimal”:
For “libpython3.12-stdlib”:
For “libpython3.12-minimal”:
Note: Visit Debian FTP to download the deb packages of Python and its dependencies.
Now, install the downloaded Python dependencies on your system for Python’s installation:
Step 2: Download the Deb Package of Python
Then, download the latest deb package of Python on your Debian system as:
Step 3: Install Python on Debian
Next, utilize the given command to install the downloaded deb package of Python:
Step 4: Replace Python’s Executable Path
After that, you need to replace the pre-installed Python path with the newly installed Python. You can check these paths using the “which” command as seen below:
which python3.12
The output shows the paths of the system’s recognizable Python and newly installed Python:
Remove the system’s pre-installed Python path as:
Replace the default “Python3” executable with the newly installed “Python3.12” by creating the symbolic link using the below-mentioned command:
Step 5: Verification
Finally, check the Python version to verify its installation:
Method 4: Install Python on Debian 12 Via Pyenv
Pyenv enables users to manage/handle Python’s multiple versions on their system. You can install the desired Python version on your Debian system through Pyenv and easily switch between them. Here are the steps:
Step 1: Install Dependencies
First, make sure you have all the necessary dependencies installed on the Debian system. Run the below-listed command to install them:
Step 2: Install Pyenv on the System
Then, utilize the given command to download Pyenv installer and install Pyenv on your Debian system:
Step 3: Configure Pyenv on Debian 12
Now, you need to open the “.bashrc” file in the editor to configure Pyenv on your system:
In the“.bashrc” file, add the following lines to set the path for Pyenv installation:
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
Save the changes using the “CTRL + X” keys and then close the file.
After that, run the given command to implement changes:
Step 4: Install Python Using Pyenv
You can check the available Python versions using “pyenv install -l” and choose the desired version that you want to install. Finally, install the specific Python version using the “pyenv install” command followed by the required version:
Here, we have installed the “3.12.1” version of Python:
Step 5: Make Python Global
You can also make the installed Python version global to make it the default version used in your system:
Step 6: Verification
To verify whether the desired Python version has been installed or the system or not, run the given command:
How to Run Python Scripts on Debian 12
Once the desired version of Python is installed on the Debian system, you can use it by accessing the Python Shell. To initiate the Python Shell, type out the below-listed command:
It can be seen that the Python shell has been started:
Now, you can execute different Python scripts. For example, we are printing the following string:
If you want to leave the Python shell, utilize the provided command:
How to Fix Debian 12 System If Python is Removed
Removing or uninstalling Python from the Debian 12 system is NOT suggested because many system tools or packages rely on Python. However, if you still want to uninstall it, use the below-listed command to remove it:
If you have installed Python from the source code, you can remove it as:
As we know many system tools, such as package managers (apt, yum), and desktop environments including GNOME and KDE Plasma, etc rely on Python. Therefore, removing Python can cause instability and unexpected behavior in your system, such as you will also not be able to use the Desktop to access your system. If you encounter such issues, check out the following instructions to resolve them:
First, reboot your system and you will see the command-line interface:
Here, enter your Debian username and password to log in to your system:
Then, reload the DHCP client through the below-listed command to enable the internet on your system:
After that, refresh the network configuration by requesting a new IP address from the DHCP server through the given command:
Next, utilize the provided command for finding missing dependencies and fixing broken packages:
Finally, install the desktop environment on your Debian 12 system as:
Subsequently, the missing dependencies including Python will be installed and the GNOME login menu will appear as seen below:
That was all about installing and managing Python on the Debian 12 system.
Final Thoughts
In Debian systems, Python is already pre-installed on them. However, you can still install Python on Debian 12 via the official Debian repository, source code, or through the Pyenv method. The default Debian repository provides Python’s outdated version. Therefore, if you want to install the latest or a particular version of Python, you should install it through source code or Pyenv. This guide has demonstrated the efficient methods for Python installation on Debian 12 systems.
I am a technical writer with a knack for learning and exploring new technologies. As a Computer Science Graduate, my expertise lies in the fundamentals of Programming languages and Linux OS. I strive to provide users with in-depth and informative articles that can help them understand the core concepts of modern technologies.