Compile & Install Python 3.12 or 3.13 in Debian 12 Bookworm

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

Python 3.12 is the latest LTS (long-term support) version of the popular programming language that was released October 2, 2023. While, Python 3.13 is the most recent release series that was released on Oct. 7, 2024.

Debian 12 Bookworm by default has Python 3.11. For developers and users who need the newer Python versions, here’s how to build them from the source tarball.

Build Python 3.12 or 3.13 in Debian 12 from source tarball

Firstly, open terminal from start menu or connect to your remote Debian Server from command console. Then, follow the steps below one by one.

1. Install Dependencies:

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

sudo apt update

Next run command to install the essential dependency libraries:

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 line: (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.x tarball:

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

Or, download the Python 3.13.x  via command:

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

The programming language keeps updating with new small releases (e.g., 3.12.9, 3.13.2). So, check the download link above in web browser, and replace version number accordingly.


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

tar -Jxvf Python-3.13.1.tar.xz

3. Build Python:

NOTE: In commands below, replace version number 3.13.1 according to which version you’re going to compile & install.

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.13.1

If you save it in Downloads folder, the command will be cd ~/Downloads/Python-3.13.1. 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.13, run python3.13 --version and pip3.13 --version instead.

Set Python 3.12/3.13 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:

Tips: Replace 3.12 in commands below with 3.13 will set the Python 3.13 as default. If you have multiple versions installed, then run the update-alternatives command with --install option multiple times for different Python version, then switch between them with --config option.

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.13/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.