How to Install Python on Debian 12?


Python is a versatile high-level programming language. It is used extensively in Data Analysis, Software Development, Web Development, Network Programming, Automation Scientific, and Numeric Computation. Python is compatible with Debian and a large number of Python programmers are developing on Debian systems. Additionally, Python has extensive community support which continuously contributes to making it more effective day by day.

This post illustrates different methods to install Python on Debian 12.

List of Contents:

 

How to Install Python on Debian 12?

Python can be installed via APT package manager, Debian Package, and building from the source (via tar.gz archive) as demonstrated below:

 

1. Using Default Repositories of Debian 12?

Use the below-mentioned steps to install Python via APT (Advanced Package Tool) packet manager:

Step 1: Update the System Packages List

Run the following command to update the system repositories:

sudo apt update

Step 2: Install Python

Run the following command to install Python:

sudo apt install python3

Python 3.11.2 is installed successfully.

2. Using the .deb Package

A Debian Package (.deb) is a compressed archive format that contains source packages, executable files, and binary packages used by Debian. To install Python via the Debian package, execute the following steps:

Prerequisites: Download/Install Dependencies

To install Python via a Debian package, first, download the dependencies: libpython3.12-minimal, libpython3.12-stdlib, and Python3.12-minimal by using the wget command as follows:

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

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

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

Then, install the downloaded dependencies:

sudo apt install ./libpython3.12-minimal_3.12.0-7_amd64.deb && sudo apt install ./libpython3.12-stdlib_3.12.0-7_amd64.deb && sudo apt install ./python3.12-minimal_3.12.0-7_amd64.deb

Step 1: Download the Debian Package of Python

To download the Debian package of Python 3.12, execute the following command:

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

Note: To install a previous version of Python, use the wget command along with the link to the Debian package from Debian’s FTP.

Step 2: Install Python

The command below installs the Debian package of Python, i.e., python3.12_3.12.0-7_amd64.deb via the apt package manager:

sudo apt install ./python3.12_3.12.0-7_amd64.deb

 

Step 3: Replace the Executable Path of Python

When Python is installed externally, its executable path needs to be replaced with the already installed version. To check the path of the system’s recognizable Python version, run the command below:

which python3

To check the path of newly installed Python, run the command below:

which python3.12

To change the executable path, create the symbolic link python3 of the directory of newly installed python, i.e., python3.12, by running the command below:

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

Step 4: Verify Python Installation

Use the “version” command to verify Python’s installation:

python3 --version

3. Built Python via Source?

Python can also be built from the source. All previous versions of Python are available in the form of tarball on Python’s Official Website. To install Python via building from the source, execute the following steps:

Prerequisites: Install Dependencies

First, update the system repositories by executing the below-mentioned command:

sudo apt update

Before working with tarballs, install the dependencies, by executing the commands below:

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

Step 1: Download the Compressed Archive of Python

To download the compressed archive containing Python via Python’s Official Website, execute the following command:

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

Note: To install a previous version of Python, use the wget command along with the link to the respective Compressed Archive package from Python’s Official Website.

Step 2: Extract the Compressed Archive of Python

Run the command below to extract Python-3.12.0.tgz:

tar xzf Python-3.12.0.tgz

Step 3: Compile the Source Code

Navigate to the Python-3.12.0 directory via the cd command and run the following configuration script with the additional flag: “–enable-optimizations”, which enhances the performance of the built Python interpreter (run time program) by about 10%. However, it slows down the configuration process:

./configure --enable-optimizations

Step 4: Building the Source Code

The make command is utilized to build the source code. Additionally, utilize the “j” flag with the number of CPU processors to enhance the build process. In our case, we will build Python by using 4 processors by executing the following command:

Note: The nproc command is used to check the number of processing units of the CPU. The more the number of processing units in the make command (via the j flag), the faster will be the building process.

make -j 4

Step 5: Install Python

To install Python, run the following command with altinstall, which ensures that the default Python library is not affected:

sudo make altinstall

Next, replace the executable path of the newly installed Python with the already installed Python as shown in Step 3 in the the previous section.

Step 6: Verify Python Installation

Verify Python’s installation by running the command below:

python3 --version

Note: Delete the .deb and .tgz compressed archives after the successful installation of Python to conserve memory.

 

How to Execute/Run Python Scripts?

The interactive mode enables line-by-line execution of Python script. To access Python interactive mode, run the “python3” command as follows:

python3
>>> print("Hello World")
>>> print(100+45)
>>> exit()

To run a Python script hello.py, execute the following command in the directory of the script:

python3 hello.py

Additionally, Python packages are installed via Pip. More details about installing Pip on Debian 12 and installing Python packages can be seen in this post.

 

How to Uninstall/Remove Python on Debian 12?

It is highly not recommended to uninstall Python as various system utilities rely on Python. If in case there is a requirement to uninstall Python along with all its dependencies, run the following command:

 

sudo apt autoremove python3

 

How to Re-install Python?

Removing Python will result in unexpected behavior as a lot of system utilities are using Python, for example, the Desktop environment of Debian will be uninstalled and the user will be logged in CLI mode:

To re-install Python, execute the following steps:

Step 1: Enable Internet

To enable the internet on the Debian 12 system, first reload the DHCP client by executing the following command:

sudo dhclient -r

Next, configure the DHCP client by running the command below:

sudo dhclient

Step 2: Find and Fix Packages

To find missing dependencies and fix broken packages, execute the command below:

sudo apt install -f

Step 3: Install Desktop Environment

To install the GNOME desktop environment, execute the following command:

sudo apt install task-gnome-desktop

After the installation is complete, the GNOME login menu will appear:

After logging in the system, verify the restoration of Python by the following command:

python3 --version

Bonus Tip: Pyenv | How to Manage Python Versions on Debian 12?

Pyenv is a tool that enables users to install and manage different Python versions. Execute the steps below to install Python via pyenv:

Step 1: Update the System Packages List

Update the system packages list by executing the following command:

sudo apt update

Step 2: Install Dependencies

Install the required dependencies, by executing the commands below:

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

Step 3: Install and Configure Pyenv

To download the Pyenv installer from GitHub and then install it via bash, run the following command:

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

Next, restart the terminal and add the configuration below to add pyenv to shell configuration:

echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc && echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc && echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

Step 4: Install Python

To install any version of Python, execute the following command:

pyenv install <version>

For example, to install the Python 3.12.0 environment, run the following command:

pyenv install 3.12.0

The above output verifies that Python 3.12.0 is installed successfully.

Display installed Python versions

The “*” indicates the currently active system Python, i.e., python 3.11.2.

pyenv versions

Verify Current Active System Python

python3 --version

Set the Global Version

To set a Python version to the global version, i.e., the Python version by default, enter the Python version in the command below:

pyenv global <version>

For example, to install Python 3.8.0, run the following command:

pyenv global 3.8.0

Then, execute the command below to update the path:

eval "$(pyenv init -)"

Display installed Python versions

The “*” is now at Python version 3.8.0, which indicates that the active system Python is 3.8.0.

pyenv versions

Verify Current Active System Python

pyenv version

Set the Local Version

To set a local version, i.e., a version for a particular directory, execute the command below:

pyenv local <version>

For example, to install Python 3.10.13, run the following command:

pyenv local 3.10.13

Display installed Python versions

pyenv versions

Verify Current Active System Python

pyenv version

That’s how the Python is installed/managed on Debian 12.

Conclusion

In Debian 12, installing Python via APT is the most reliable method as the packages available at APT are tested and approved by the Debian team. To install a package via the APT package manager, run the “sudo apt install python3” command. However, the APT does not offer the latest release of Python. To download the latest stable version, install via the Debian Package, and build from the Source. This article demonstrated the installation of Python and running Python scripts on Debian 12 (Bookworm) systems.

Print Friendly, PDF & Email