Python is one of the most popular high-level languages, focusing on high-level and object-oriented applications from simple scrips to complex machine learning algorithms. The Python language was created by Guido van Rossum and released in 1991, Python 2 in 2000, and Python 3 in 2008. Currently, the latest series in Python is 3.9 brings forward significant changes to language features, and how the language is developed along with a performance boost in new string functions, dictionary union operators, and more consistent and stable internal APIs.
Some of the features Python can do:
- Python can be used on a server to create web applications.
- Python can be used alongside software to create workflows.
- Python can connect to database systems. It can also read and modify files.
- Python can be used to handle big data and perform complex mathematics.
- Python can be used for rapid prototyping or production-ready software development.
In the following tutorial, you will learn how to install Python 3.9 on Debian 11 Bullseye, along with a quick example of how to create a virtual environment (venv).
Table of Contents
- Recommended OS: Debian 11 Bullseye
- User account: A user account with sudo privilages or root access (su command).
- Required Packages:
Updating Operating System
Update your Debian 11 operating system to make sure all existing packages are up to date:
sudo apt update && sudo apt upgrade
Root or Sudo Access
By default, when you create your account at startup with Debian compared to other distributions, it does not automatically receive sudoers status. You must either have access to the root password to use the su command or visit our tutorial on How to Add a User to Sudoers on Debian.
Method 1. Install Python 3.9 with APT
The first option to install Python 3.9 is to use the APT package manager, which is often recommended for its ease of use, stability, and, more importantly, availability of urgent security updates that can be applied in a few commands with the rest of your system updates.
By default, Python 3.9 should be installed. If missing, use the following apt command to install Python in your terminal:
sudo apt install python3.9
Next, verify the installation using the apt-cache policy command:
sudo apt-cache policy python3.9
python3.9: Installed: 3.9.2-1 Candidate: 3.9.2-1 Version table: *** 3.9.2-1 500 500 http://ftp.au.debian.org/debian bullseye/main amd64 Packages 100 /var/lib/dpkg/status
Next, confirm the version installed:
python 3.9 --version
Method 2. Install Python 3.9 from Source
The second part of the installation of the 3.9 builds is to install the packages required. To install these dependencies, run the following command in your terminal:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev -y
Next, visit the downloads page and get the latest Python 3.9 version build and use the wget command to download the latest archive:
Extract the Python archive. Remember, change the version number if you downloaded a newer one:
tar -xf Python-3.9.7.tar.xz mv Python-3.9.7 /opt
Navigate to the directory and run the ./configure –enable-optimizations command:
cd /opt/Python-3.9.7 ./configure --enable-optimizations --enable-shared
Note, The script performs several checks to make sure all of the dependencies on your system are present. The ./configure –enable-optimizations will optimize the Python binary by running multiple tests, which will make the build process slower.
Now that you have built and configured the environment, it is time to compile it with the command make.
A handy trick is to specify the -j <number of cpu> as this can significantly increase compiling speed if you have a powerful server. For example, the LinuxCapable server has 6 CPUs, and I can use all 6 or at least use 4 to 5 to increase speed.
make -j 6
Once you have finished building, install Python binaries as follows:
Note, it’s advised to use the make altinstall command NOT to overwrite the default Python 3 binary system.
Next, after the installation, you need to configure the dynamic linker run-time bindings:
sudo ldconfig /opt/Python3.9.7
Note, do not skip this, or you will face issues. You will also need to replace the path with your directory name and version.
Confirm that Python 3.9 is installed and the build version by running the following command:
Create a Virtual Environment
Python’s venv module is a virtual environment is a Python environment such that the Python interpreter, libraries, and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed on your operating system, for example, those that are installed on your Ubuntu operating system to avoid clashing and disturbing your production environments.
To make sure Python 3.9 is installed correctly and functioning, create a quick Python project as follows.
First, create the project directory and navigate to it:
mkdir ~/test_app && cd ~/test_app
Now inside the project root directory, run the following command to create a virtual environment, for the test name it test_app:
python3.9 -m venv test_app_venv
Next, activate the virtual environment as follows:
After starting the virtual environment, you will now be in the shell prompt terminal. You will notice the name of your environment will be prefixed.
(test_app_venv) [email protected]:~/test_app#
By default, pip 3.9 should be installed, which is the most used package manager for Python.
To exit the virtual environment, use the following command:
Comments and Conclusion
The tutorial has shown you how to configure and build your Python version on your Debian 11 Bullseye system using one of the two methods posted. You will need to make sure you keep updating it as with all self-compiled builds, and updates, especially around security, can be forgotten about without checking in the future, so best to subscribe to a Python RSS feed or similar to keep up with the news and updates.