Debian

How to Install Python on Debian 12 Bookworm

how to install python on debian 12 bookworm

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

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:

sudo apt update

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:

sudo apt install python3

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:

python3 --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:

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

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:

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

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:

tar -zxf Python-3.12.2.tgz

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:

./configure --enable-optimizations

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:

make -j5

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:

sudo make altinstall

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:

python3.12 --version

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”:

wget http://ftp.de.debian.org/debian/pool/main/p/python3.12/python3.12-minimal_3.12.2-1_amd64.deb

For “libpython3.12-stdlib”:

wget http://ftp.de.debian.org/debian/pool/main/p/python3.12/libpython3.12-stdlib_3.12.2-1_amd64.deb

For “libpython3.12-minimal”:

wget http://ftp.de.debian.org/debian/pool/main/p/python3.12/libpython3.12-minimal_3.12.2-1_amd64.deb

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:

sudo apt install ./libpython3.12-minimal_3.12.2-1_amd64.deb && sudo apt install ./libpython3.12-stdlib_3.12.2-1_amd64.deb && sudo apt install ./python3.12-minimal_3.12.2-1_amd64.deb

Step 2: Download the Deb Package of Python

Then, download the latest deb package of Python on your Debian system as:

wget http://ftp.de.debian.org/debian/pool/main/p/python3.12/python3.12_3.12.2-1_amd64.deb

Step 3: Install Python on Debian

Next, utilize the given command to install the downloaded deb package of Python:

sudo apt install ./python3.12_3.12.2-1_amd64.deb

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
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:

sudo rm /usr/bin/python3

Replace the default “Python3” executable with the newly installed “Python3.12” by creating the symbolic link using the below-mentioned command:

sudo ln -s /usr/bin/python3.12 /usr/bin/python3

Step 5: Verification

Finally, check the Python version to verify its installation:

python3 --version

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:

sudo apt install git libssl-dev libreadline-dev libsqlite3-dev llvm libncursesw5-dev tk-dev libffi-dev xz-utils zlib1g-dev libbz2-dev liblzma-dev libncurses5-dev -y

Step 2: Install Pyenv on the System

Then, utilize the given command to download Pyenv installer and install Pyenv on your Debian system:

curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

Step 3: Configure Pyenv on Debian 12

Now, you need to open the “.bashrc” file in the editor to configure Pyenv on your system:

nano ~/.bashrc

In the“.bashrc” file, add the following lines to set the path for Pyenv installation:

export PYENV_ROOT="$HOME/.pyenv"
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:

source ~/.bashrc

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:

pyenv install 3.12.1

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:

pyenv global 3.12.1

Step 6: Verification

To verify whether the desired Python version has been installed or the system or not, run the given command:

python --version

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:

python3

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:

print("Hello LinuxWays")

If you want to leave the Python shell, utilize the provided command:

exit()

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:

sudo apt autoremove python3* -y

If you have installed Python from the source code, you can remove it as:

sudo find /usr/local -depth -iname 'python*' -exec rm -rf {} \;

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:

sudo dhclient -r

After that, refresh the network configuration by requesting a new IP address from the DHCP server through the given command:

sudo dhclient

Next, utilize the provided command for finding missing dependencies and fixing broken packages:

sudo apt install -f

Finally, install the desktop environment on your Debian 12 system as:

sudo apt install task-gnome-desktop -y

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.

Similar Posts