How to Install Python 3.12.5 on Debian 11 bullseye

Published December 10, 2024

How to install Python 3.12.5 on Debian 11 How to install Python 3.12.5 on Debian 11

The latest version of Python is 3.12.5 and is the fifth maintenance release, with more than 250 bugfixes, build improvements and documentation changes since 3.12.4.

I use Debian 11 and 12 for a lot of my work , and would like to have the latest Python 3.12.5. Unfortunately, Debian 11 stable includes a much older version. This tutorial will help you install Python 3.12.5 on Debian 11 by building it from source.

Linode

I use Linode for my web hosting. It is an awesome hosting company. I use the unmanaged plan, so that means I get to install Nginx, configure the system and install the latest and greatest version of Python, which is 3.12.5.

Go get your Linode account with $100 free credits for 60 days.

Install Python using apt

The default version of Python on Debian 11 is Python 3.9.2. If you are happy with it, good.

$ 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.12.5, read on.

Previous versions of Python

If you are using any previous version of Python, such as 3.12.1 or 3.11, this article is applicable.

Download Python 3.12.5 source

Python 3.12.5 was released on August 6, 2024. 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.12.5/Python-3.12.5.tgz
tar -xzvf Python-3.12.5.tgz
cd Python-3.12.5/

OUTPUT:

$ cd /tmp/
$ wget https://www.python.org/ftp/python/3.12.5/Python-3.12.5.tgz
--2024-08-07 21:53:44--  https://www.python.org/ftp/python/3.12.5/Python-3.12.5.tgz
Resolving www.python.org (www.python.org)... 146.75.28.223
Connecting to www.python.org (www.python.org)|146.75.28.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26978998 (26M) [application/octet-stream]
Saving to: โ€˜Python-3.12.5.tgzโ€™

Python-3.12.5.tgz          100%[=====================================>]  25.73M  37.3MB/s    in 0.7s    

2024-08-07 21:53:44 (37.3 MB/s) - โ€˜Python-3.12.5.tgzโ€™ saved [26978998/26978998]

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 libsqlite3-dev

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

Configure, make and make install

Run the configure script and enable Profile guided optimization (PGO) and Link Time Optimization (LTO). This can boost up speed up to 20%.

./configure --enable-optimizations

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

make -j `nproc`

This step can take several minutes. The more processors you have in your computer, the faster will be the build. With a single-core processor, this step can take hours!

Why does the make command take so long to complete?

The make -j command can be faster when there more than a single processor because it uses all the available CPUs, so the build is done in parallel with each CPU contributing to it.

The slowest is when your computer has a single processor. In that case, these two are equivalent.

make
make -j 1

How to speed up the make command execution time

If you are building Python from scratch in a VM (virtual machine), before you start, increase the number of cores to 4 or more. Then start your VM and follow the steps. By doing this, the make command will take much lesser time.

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.12. To test the version, run this:

python3.12 -V

You will get this output:

Python 3.12.5

Make Python 3.12.5 the default version

If you get Python 3.12.5 as the default version, you can skip this step. If you still get a lower version of Python, then read on.

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

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

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

ln: failed to create symbolic link '/usr/local/bin/python': File exists

If you get this error:

ln: failed to create symbolic link '/usr/local/bin/python': File exists

that means that you already have a softlink at that location. Delete the softlink.

$ sudo rm /usr/local/bin/python

Then, repeat the softlink create command:

$ sudo ln -s /usr/local/bin/python3.12 /usr/local/bin/python

Now, verify it:

$ ls -al /usr/local/bin/python
lrwxrwxrwx 1 root root 25 Aug 6 13:08 /usr/local/bin/python -> /usr/local/bin/python3.12

Test if Python 3.12.5 is the default version

Test whether Python 3.12.5 is the default version:

$ ls -al /usr/local/bin/python
lrwxrwxrwx 1 root root 25 Dec 26  2023 /usr/local/bin/python -> /usr/local/bin/python3.12

Since I did not change this softlink to /usr/local/bin/python, it shows the date it was created, which is December 26, 2023.

Now, check if Python 3.12 is pointing to the 3.12.5 that we just installed.

$ ls -al /usr/local/bin/python3.12
-rwxr-xr-x 1 root root 36345608 Aug  8 08:58 /usr/local/bin/python3.12


$ /usr/local/bin/python3.12 -V
Python 3.12.5

Test the verbose output.

python -VV

The output will be:

Python 3.12.5 (main, Aug  8 2024, 08:56:29) [GCC 10.2.1 20210110]

So, at this point, Python 3.12.5 has been set as the default version of Python.

Hope this worked for you. If you have any issues installing Python on your Debian 11 system, feel free to comment below or email me.

Related Posts

If you have any questions, please contact me at arulsutilities@gmail.com. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: December 10, 2024.     This post was originally written on January 26, 2023.

Comments

gravatar

Your email address will not be published. All fields are required.


gravatar
Arul John wrote on 2024-05-17 03:30:17
Hi Gunter, thank you for your comment! I will include pkg-config to the list of build tools. I have not yet updated this blog post to include Bookworm, but I'll do it this weekend. Thanks.
gravatar
Gunter Baumann wrote on 2024-05-16 16:46:46
Hello, could you please add pkg-config to the list of build tools? That one seems missing on a plain vanilla bookworm installation. Thanks a lot for your work, was very helpfull for me!
gravatar
Arul John wrote on 2024-04-09 15:41:12
Hello Tony, thank you for your comment! I will update this blog post for Debian 12 Bookworm.
gravatar
Tony Pemberton wrote on 2024-04-09 11:17:25
Thanks for a full tutorial which is ideal for my HomeAssistant servers. This installed sucessfully on Debian 12 (Bookworm).The only differences I had to make were the symbolic links in that my default python-3.11 and python-3.11-config are located in /usr/bin whilst python3.12 and python3.12-config are located in /usr/local/bin as per altinstall. Thanks again
gravatar
Arul John wrote on 2024-03-02 15:30:29
Hello E. Y Moetbe, unfortunately, there don't seem to be any official pre-compiled packages for the latest Python version on Debian. If installing Python by building from source is not working for you, you could try using package managers like Miniconda or Miniforge. They generally have the latest Python versions.
gravatar
E.Y Moetbe wrote on 2024-03-01 22:21:42
This is not installation, but compilation from source. There must be a way to do an apt install a pre-compiled package rather than compiling from scratch. I ain't got 4 hours watching gcc and make failing with some ridiculous error ...
gravatar
Arul John wrote on 2024-02-27 20:02:26
Joseph, if you want to reinstall Python 3.12.2, just redo the installation procedure. It will overwrite the current 3.12.2 installation.
gravatar
Arul John wrote on 2024-02-27 19:56:51
Hello Joseph, the correct command for make -j `nproc` uses backticks and not single quotes. Yes, the installation is done in the directory /tmp/Python-3.12.2 Can you close the Terminal and reopen it for Python 3.12.2 to take precedence over 3.9? If that doesn't work, try this: echo $PATH Let me know what you see. It is possible that /usr/local/bin shows up later than /usr/bin in the $PATH variable and that can be adjusted in ~/.bash_profile or ~/.bashrc
gravatar
Joseph Attard wrote on 2024-02-27 19:45:42
Hi again, I just remembered that make -j 'nproc' gave me an error, is there a way that i uninstall the newer version of python and re-install it again? thanks in advance
gravatar
Joseph Attard wrote on 2024-02-27 19:37:18
Hi, i followed all the steps in this tutorial however i still have version 3.9 as my default Python. I am using the Radxa version of Linux, Debian GNU/Linux 11. If I am understanding correctly all the installation is done in /tmp/Python-3.12.2 and all the instructions are carried out while the terminal is in folder Python3.12.2, right?
gravatar
Arul John wrote on 2024-02-21 23:00:11
Affan Ali, yes, you have the correct one. If you don't get that message, it means the Python binary has not been pointed to /usr/local/bin/python. From your second comment, looks like your installation was good.
gravatar
Affan Ali wrote on 2024-02-21 17:47:37
so do i have the correct one? mine says this as the last output: Python 3.12.2 (main, Feb 21 2024, 12:34:50) [GCC 12.2.0] after I entered python -VV
gravatar
Affan Ali wrote on 2024-02-21 17:41:41
what if you dont get this message: ln: failed to create symbolic link '/usr/local/bin/python': File exists
gravatar
Arul John wrote on 2024-02-08 23:11:39
Hello Vicky, yes, the instructions are pretty much the same, and I just updated the blog post for 3.12.2. For moving from 3.12 to 3.12.2 you do not have to update the softlinks towards the end. Let me know how that goes.
gravatar
Vicky karthi wrote on 2024-02-08 04:48:05
Bro i am using python 3.12 if i want to update it. I need to download 3.12.1 version then install it or any other work around.
gravatar
Arul John wrote on 2024-01-26 17:00:40
Hi Ivo, thanks for your comments! I appreciate it. I updated the blog post with libsqlite3-dev in the build tools install section.
gravatar
Ivo Raisr wrote on 2024-01-26 12:08:44
Thanks for a very nice blog! Please add another package which shall be installed as a dev-dependency in section "Install the build tools": libsqlite3-dev Without this dev package, _sqlite3 library is not built and sqlite3 module cannot find the associated library: Python 3.12.1 (main, Jan 26 2024, 11:07:49) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sqlite3 Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.12/sqlite3/__init__.py", line 57, in from sqlite3.dbapi2 import * File "/usr/local/lib/python3.12/sqlite3/dbapi2.py", line 27, in from _sqlite3 import * ModuleNotFoundError: No module named '_sqlite3' With this dev package installed, the _sqlite3 library is built and sqlite3 module works: Python 3.12.1 (main, Jan 26 2024, 12:00:18) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sqlite3 >>>