How to Install Python 3 in Debian 11 / 10

How to install Python 3 in Debian 11/10. This article introduces Python, it’s key features and after, we move to installation phase. Let’s get started. 

What is Python?

Python is a high level and object oriented programming language that is maintained by the Python Software Foundation. Interestingly, a non profit organization with the general purpose programming language that is intuitive and easy to learn with a simple syntax. In turn, this makes it an ideal choice for beginners in programming and also for intermediate to advanced developers.

Being a dynamic programming language, Python features in a myriad of use cases including data science, machine learning, AI (Artificial Intelligence)
, creating web applications, automating redundant tasks and many more. In this article, we explore how you install Python3 in Debian 11 ( codenamed Bullseye ) or Debian 10 ( Buster).

Worth mentioning, that at the time of writing this article, the latest Python version is Python 3.11

Python 3.11 Key Features

All in all, the latest version of Python provides numerous features and improvements. In this section we highlight some of the salient features:

  • Speed improvements: Python 3.11 is 1.25 times faster than Python 3.10
  • Enhanced error information. Python 3.11 provides detailed feedback about what part of an expression yielded an error.
  • Dataclass transforms.
  • Atomic grouping and speedups for regex.
  • Variadic generics.
  • TOML read-only support in stdlib.
  • Arbitrary string literal type.

We have arrived at the main part of the article How to install Python 3 in Debian 11/10.

How to install Python 3 in Debian 11/10

Prerequisites

Before you get started out, ensure that you have the following:

  • An instance of Debian 11 / 10 server with SSH access.
  • A sudo user configured on the instance.

Step 1: Update Package Repositories

To get off the ground, log into your Debian server and update the local package index and repositories as follows.

				
					$ sudo apt update
				
			

Once the system is up to date, head over to the next step.

Step 2: Install Dependencies

A couple of dependencies and tools are required for the installation  of Python3 to be successfully installed. Therefore, install the dependencies as shown:

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

				
			

Step 3: Install Python3 on Debian 11/10

At the time of writing this guide, the latest Python release is Python 3.11 which was released on December 6, 2022. However, the latest version that is currently provided by Debian repositories is Python 3.9.

Depending on your requirements, you choose to either install the version provided by default in the repository or install the latest version provided the Python.

To install Python from Debian’s APT repository, run the command:

				
					$ sudo apt install python3 -y
				
			

As earlier mentioned, this method does not provide the latest release.  In order to install the latest release, you need to manually download the latest binary file from the official Python download page. The page provides source files which are in Gzipped or XZ compressed in form of tarball files. 

Well, to download the latest Python binary file, we use the wget command as follows.  The version of Python is likely to change:

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

Once the download is complete, extract the tarball file as shown.

				
					$ tar -xvf Python-3.11.1.tgz

				
			

Once the extraction of the tarball is complete, navigate into the uncompressed Python3 folder.

				
					$ cd Python-3.11.1
				
			

Then run the following command to check if all dependencies to install Python3.11 have been satisfied.

				
					$ sudo ./configure --enable-optimizations
				
			

Next, The -–enable-optimizations option runs multiple tests and optimizes the Python binary. Then run the make command to compile the Python  source code. This takes quite a while, so you might consider taking a break at this point.

				
					$ sudo make -j 2
				
			

Once the build process is complete, install the Python binaries as shown.

				
					$ sudo make altinstall
				
			

At this point, Python is successfully installed.

Step 4: Verify Python3.11 Installation

To check if Python3 has been installed, run the following  command

				
					$ python3.11 -V
				
			

From the output, you can see that we have successfully installed Python3.11 on Debian.

Step 5: Install PIP ( Package Manager )

In Python, PIP , an acronym for  Preferred Installer Program, is a package manager that allows you to install packages and libraries that are not part of the standard Python library. It’s the most widely used package management tool for Python.

To install PIP, run the following command.

				
					$ sudo apt install python3-pip
				
			

You confirm the version of PIP installed by running the command:

				
					$ pip3 -V
				
			

Alternatively, you run:

				
					$ pip3 --version
				
			

The output below confirms that we have installed the latest version of pip, which as of this guide, is version 20.3.4

Step 6: Set up A Virtual Environment ( Optional )

After all, a virtual environment is a Python folder structure that is created on top of the current Python installation. In addition, it provides an isolated environment that helps to keep dependencies needed by different Python projects separate. Hence, a virtual environment comes in handy when you are working on different projects, each with different versions of package versions and dependencies.

By default, all projects residing on your system use the same directories to store and retrieve third party libraries or site packages. Suppose you are working with different versions of Django ( v 4.1.5 and v 4.0 ). Python does not have a way of distinguishing between these versions in the ‘site-package’ directory. What happens is that both versions of Django are placed in the same directory under the same name.  In turn, this creates conflict and is a huge problem for developers. 

And this is where creating a virtual environment is beneficial. Setting up isolated environments for each project provides autonomy and greater control over how different versions of packages are managed. You set up or create as many virtual environments as you want depending on the number of projects at hand.

As earlier mentioned, a virtual environment is a folder or directory with a few scripts to enable it work as an environment.

Install Venv Package

To create a virtual environment, you need to install the venv package as shown.

				
					$ sudo apt install python3-venv -y
				
			

Once installed, you can now create a virtual environments for your projects. First , you need to create a directory for your shiny new environment using the mkdir command followed by the path to the directory.  In this case, we are creating a new directory in our home directory.

				
					$ mkdir test_directory
$ cd test_directory
				
			

Once you are in the directory that you want to create the isolated environment, proceed and use the following syntax to create the virtual environment.

				
					

python3 -m venv /path/to/new/virtual/environment
				
			

In our case, we will createa virtual environment called my_environment

				
					$ python3 -m venv my_environment
				
			

The command simply creates a new directory called my_environment which contains  a virtual environment configuration file and  directories that contain scripts and essential files which work together to provide an isolated environment from the rest of the operating system.

The directories in the virtual environment include  bin, include, lib, lib64 and share.

In order to start using your virtual environment, run the following command to activate it.

				
					source my_environment/bin/activate
				
			

The command prompt will now be prefixed by the name of the environment, in this case, my_environment.

Step 7: Create a Simple Python Program

Now that we have created our virtual environment, we are going to create a simple Python program that prompts for two numbers and  adds them the output of which is printed on the terminal. This helps us to know if our virtual environment is working as intended

Using the nano text editor, we create a python file as shown.

				
					nano simple_program.py
				
			

Add the following lines

				
					num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
total = str(num1 + num2)
print("The sum of two numbers is: " + total)
				
			

Save the changes and exit. Then run the python file as shown.

				
					python  simple_program.py
				
			

The program you just created  prompts you to enter two numbers and later print out the sum total of the numbers provided.

To exit the virtual environment, simply run the deactivate  command:

				
					$ deactivate
				
			

Thank you for reading How to install Python 3 in Debian 11/10. We shall conclude. 

How to install Python 3 in Debian 11/10 Conclusion

Congratulations for coming this far. You have learned how to install Python3 on Debian 11/10. We have also demonstrated how to set up a virtual environment for running your python projects in isolation from other projects on your host system.

Avatar for James Kiarie
James Kiarie

Hello everyone! My name is James, a certified Linux Administrator, and a tech enthusiast with over 5 years of experience in penning down high-quality guides on Linux and Cloud technologies. Outside work hours I enjoy working out, swimming, listening to music, and reading fiction novels.

4 6 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x