How to Compile and Install Python 3.12 / 3.10 in Debian 11 / 12

This tutorial shows how to install and setup the Python programming language 3.10 or 3.12 in Debian 11 bullseye and 12 bookworm.

Python 3.10 is the latest LTS version of the interpreted high-level general-purpose programming language released a few days ago. It features pattern matching, writing union types as X | Y, OpenSSL 1.1.1, and more. And it has 18-month support with bug-fix updates, and 5-year support with security updates.

Unlike Fedora and Ubuntu, there’s neither official package nor PPA repository that contains Python 3.10 package for Debian users. However, compiling Python from source is easy and here’s how!

UPDATE: This tutorial is also tested and works with Python 3.12. Though, you have to change the version number in the following commands.

Build Python 3.10/3.12 in Debian 11/12:

Firstly, open terminal from start menu or connect to Debian Server command console. Next run following commands one by one to build Python 3.10 either in Debian 11/12 Desktop and/or Server.

1. Install Dependencies:

In terminal or command console, first update system package by running command:

sudo apt update

Next install essential packages for downloading the source and building the program:

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

2. Download the source tarball:

Download via web browser: Python has a FTP download page contains the tarball for all releases:

Just select download the tarball (.tar.xz) for your desired version, and extract it in your Downloads folder.


Download via command: (The download link varies depends on which version you’re doing to install).

For Debian server without GUI, you may use the command below instead to download the 3.12.0 tarball:

wget -c https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tar.xz

Or, download the Python 3.10.12 via command:

wget -c https://www.python.org/ftp/python/3.10.13/Python-3.10.13.tar.xz

After that, extract the source tarball either via file manager or using command (change release number 3.12.0 accordingly):

tar -Jxvf Python-3.12.0.tar.xz

3. Build Python:

After downloading and extracting the source tarball, navigate to the extracted folder via cd command (or right-click on extracted folder and select “Open in Terminal):

cd Python-3.12.0

If you save it in Downloads folder, the command will be cd ~/Downloads/Python-3.12.0. Also, change version number accordingly.

Then, configure with expensive, stable optimizations (PGO, etc.):

./configure --enable-optimizations

Finally, build and install Python into /usr/local/bin directory without overriding the system built-in Python installation:

sudo make -j4 && sudo make altinstall

Depends on your CPU, change -j4 (4 threads) in command to tell start how many threads for the building process.

4. Verify Python installation:

After installation, verify it via command:

python3.12 --version
pip3.12 --version

For Python 3.10, run python3.10 --version and pip3.10 --version instead.

Set Python 3.12 as default in Debian 12:

Debian 12 uses python 3.11 as default for python3 executable and leave ‘python’ un-configured. Users may link Python3.12 to python easily via update-alternatives command:

1.) First, install Python3.12 as alternative for python via command:

sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.12 1

Then, run the command below and type the number for Python 3.12:

sudo update-alternatives --config python

2.) For pip, it’s recommended to use python3.12 -m pip install command. If you insists, run commands below one by one to set it as default.

sudo update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.12 1
sudo update-alternatives --config pip


After that, run python --version and pip --version to check results.

Conclusion:

As you see, it’s quite easy to compile Python 3.10/3.12 from source tarball. Just install dependencies, download source and build it. And it installs in a few minutes in my case with no error occurred. As well, users may set them as default easily by creating symbolic links using update-alternatives command.

Hi, I'm Merilyn Ne, a computer geek working on Ubuntu Linux for many years and would like to write useful tips for beginners. Forgive me for language mistakes. I'm not a native speaker of English.