Installing Python 3.11 on Debian 11

The default Python version on Debian 11 is 3.9.1 which might not suite evryone. I for one, use Python 3.11.x and needed to upgrade my Python version to 3.11. This quick tutorial will explain how to replace your existing Python 3.9 to Python 3.11 on Debian 11.

Install Python using apt

$ apt info python3
Package: python3
Version: 3.9.2-3
Priority: optional
Section: python
Source: python3-defaults
Maintainer: Matthias Klose <doko@debian.org>
Installed-Size: 91.1 kB
Provides: python3-profiler
Pre-Depends: python3-minimal (= 3.9.2-3)
Depends: python3.9 (>= 3.9.2-0~), libpython3-stdlib (= 3.9.2-3)
Suggests: python3-doc (>= 3.9.2-3), python3-tk (>= 3.9.2-0~), python3-venv (>= 3.9.2-3)
Replaces: python3-minimal (<< 3.1.2-2)
Homepage: https://www.python.org/
Tag: devel::interpreter, devel::lang:python, devel::library,
 implemented-in::c, implemented-in::python, role::devel-lib,
 role::program, role::shared-lib
Cnf-Extra-Commands: python
Cnf-Priority-Bonus: 5
Download-Size: 37.9 kB
APT-Manual-Installed: no
APT-Sources: http://deb.debian.org/debian bullseye/main amd64 Packages
Description: interactive high-level object-oriented language (default python3 version)
 Python, the high-level, interactive object oriented language,
 includes an extensive class library with lots of goodies for
 network programming, system administration, sounds and graphics.
 .
 This package is a dependency package, which depends on Debian's default
 Python 3 version (currently v3.9).

If you want to upgrade to 3.11.1, read on.

Download Python 3.11.1 source

The latest Python source code is available here.

Download and extract the Python source code.

cd /tmp/
wget https://www.python.org/ftp/python/3.11.1/Python-3.11.1.tgz
tar -xzvf Python-3.11.1.tgz
cd Python-3.11.1/

Install the build tools

Now, install the build tools. The build tools includes gcc, make, zlib, ssl libraries and other libraries.

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

If you are prompted to install other dependencies, select yes.

Configure, make and make install

./configure --enable-optimizations

Now run make. You can make the build using nproc, which returns the number of CPUs.

make -j `nproc`

Make install

The default Python installation is /usr/bin. If you want to install Python under /usr/local/bin instead of overwriting the default, do this:

sudo make altinstall

This will install Python at /usr/local/bin/python3.11. To test the version, run this:

python3.11 -V

You will get this output:

Python 3.11.1

Make Python 3.11.1 the default version

To make the default version of Python 3.11.1, run this:

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

This creates a bunch of softlinks and links the latest Python to /usr/local/bin.

Test whether Python 3.11.1 is the default version:

$ ls -al /usr/local/bin/python
lrwxrwxrwx 1 root root 24 Jan 17 22:29 /usr/local/bin/python -> /etc/alternatives/python

$ ls -al /etc/alternatives/python
lrwxrwxrwx 1 root root 25 Jan 17 22:27 /etc/alternatives/python -> /usr/local/bin/python3.11

$ /usr/local/bin/python3.11 -V
Python 3.11.1
python -V

The output will be:

Python 3.11.1

At this point, Python 3.11.1 has been set as the default version of Python.

Share this content: