| by Arround The Web | No comments

How to Install Python on Debian 12

Python is a popular programming language and a main part of the Linux operating system including Debian 12. The reason is many applications running on the Linux system use Python to perform system related tasks, such as managing system logs, automating tasks and more. With Python, you will be able to execute Python codes, run Python applications or access several Python libraries on your system.

In this guide, you will learn:

How to Install Python Debian 12

Python is preinstalled on a Debian system; however, the installed version of Python isn’t the latest one. If you want to install the latest version of Python on Debian 12, use the following steps:

Step 1: Check Python Version on Debian

First, check the version of Python installed on your Debian system, this can be done from the following command:

python3 --version

Step 2: Download Latest Python Source File on Debian 12

Now, visit the website and download the latest version tgz source file (Gzipped source tarball) for Python. The latest version of Python at the time of writing this article is 3.12.1 whose source file can be downloaded on Debian 12 from the following wget command:

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

Step 3: Extract the Python Source File Content

You must extract the source file content on your Debian system using the tar command given below:

tar -zxvf Python-3.12.1.tgz

Note: Ensure changing the source file name if you have downloaded a different version on your system.

Step 4: Install Dependencies on Debian 12

You must also ensure installation of some dependencies on your Debian system since they are required during the installation process. To install those dependencies on Debian 12 with a single command, use:

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

Step 5: Configure Packages or Files for Python

To check for packages and configure them for installation, first navigate to the Python source directory using the cd command:

cd Python-3.12.1

Then you can use the command provided below to configure packages that are needed for Python installation on Debian 12:

CFLAGS=-Wno-error ./configure

The above command will execute and ignore the warnings that may stop the execution process. In other words, it doesn’t treat a warning as an error.

Step 6: Prepare the Installation Process

After the configuration, you must prepare the Python installation process by executing the make command given below:

sudo make -j 5

The above command will parallelize the build process by splitting the task into smaller chunks. It then executes the task concurrently on up to 5 threads to speed up the build process.

Step 7: Install Python on Debian 12

After the execution of above command, you will then be able to install Python on Debian 12 by running the following make command:

sudo make altinstall

The above command adopts a different approach compared to the make install command since it installs the packages in a different location (/usr/local/bin) instead of overwriting them. This is crucial for the users who want to deal with multiple Python versions and keep things separate from the environment.

How to Update Python on Debian 12

After completing the installation, you must update Python on Debian 12, this can be done by removing the previously installed Python and replacing it with the newly installed Python version. All this can be done using the step given below:

Step 1: Navigate to Bin Directory

First, navigate to /usr/bin location on Debian using the cd command given below:

cd /usr/bin

Step 2: Remove Python Directory from Debian 12

Then remove the previous Python directory from your system using the following command:

sudo rm python3

Step 3: Link the Latest Python Version on Debian 12

Then link the latest Python version installed on Debian 12 with the python commands, this can be done by using the following commands:

For python command, use:

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

And for python3, use:

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

Step 4: Check Python Version on Debian 12

To check the latest version of Python is updated on your Debian system, you can use the following command:

python3 --version

How to Use Python on Debian 12

To use Python on Debian 12, use the following command to open Python interpreter:

python3

Then write any python code and press Enter to execute it on your system:

print("Hello LinuxHint")

Note: You can also create a file with the .py extension on Debian, add a Python code and then execute the file using the Python interpreter.

Bonus Method: How to Install Python on Debian 12 Through pyenv

pyenv is another effective method that can be used to install Python on Debian 12 or other Linux systems. With pyenv, you can install multiple Python versions on your system and switch to any version within seconds. It will be useful for the users who are working with applications simultaneously that require different Python versions.

To install pyenv on Debian 12 and install Python through it, use the following steps:

Step 1: Install Curl and Git on Debian 12

First, you must install curl and git on your Debian system from the following command since they are required for installing pyenv:

sudo apt install curl git -y

Step 2: Install pyenv on Debian 12 from Script

Now run the following command to download the pyenv script and install the application on Debian 12:

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

Step 3: Set Environment Path

You must set the path in the .bashrc file so that the system will know where pyenv is installed. To do this, open the .bashrc file using the following command:

sudo nano ~/.bashrc

Then add the following lines inside the file:

export PYENV_ROOT="$HOME/.pyenv"

[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"

eval "$(pyenv init -)"

Save the file using CTRL+X, add Y and press Enter to exit.

Then apply the changes using source command provided below if :

source ~/.bashrc

Step 4: Check pyenv Version

To confirm pyenv is successfully installed on Debian 12, use the following command:

pyenv --version

Step 5: Check List of Pythons

To check the list of different Python versions that can be installed on Debian 12 through pyenv, you can use the below-given command:

pyenv install -l

Step 6: Install Python from pyenv

You can now install Python from pyenv using the pyenv command followed by the install keyword and the Python version you want to install on Debian:

pyenv install version_number

Step 7: Make Python Global

Make the Python installation global on Debian 12 from the following command:

pyenv global 3.13-dev

Step 8: Initializes the pyenv Shell

Now, you have to make changes to the shell by initializing the pyenv, so that it will setup the necessary environment variables, this can be done using:

eval "$(pyenv init -)"

Step 9: Check Python Version

Now confirm the Python version installed through pyenv is successfully updated on Debian 12 using the following command:

python3 –version

How to Fix Issue While Removing Python from Debian 12

It is advised and highly prohibited not to remove Python from your Linux system because most applications, services and built-in features are based on Python. Removing Python will make the system unstable, and you may not be able to use the desktop. If you have accidentally removed Python from your system, use the following steps to fix it:

Step 1: Reboot the device and login to your system user by providing username and password:

Step 2: Now, run the following command to renew your IP address from the DHCP server:

sudo dhclient -r

Step 3: To ensure your system has a valid network connection, you can use:

sudo dhclient

Step 4: Then install the necessary desktop environment on Debian 12 from the following command:

sudo apt install task-gnome-desktop -y

The above command will install the necessary dependencies on Debian 12 including Python as well and once installed, you will see the desktop interface once again:

Conclusion

Python is pre-installed in Debian 12, however to install the latest version, download the source tgz file from the official website. Then navigate to source directory and use the configure command to check for the Python configuration file. Later on, you can use the make command to prepare the files, execute them and install through make altinstall command. However, you have to update your Python version by first removing the previous Python directory and linking it with the latest one. Further you can also use pyenv to install Python on Debian 12. The complete step-by-step guide for installing Python on Debian 12 is provided in the above section of this guide. Doing this will allow you to use the latest version of Python on your Debian system.

Share Button

Source: linuxhint.com

Leave a Reply