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
- Install Python using apt
- Previous versions of Python
- Download Python 3.12.5 source
- Install the build tools
- Configure, make and make install
- Why does the make command take so long to complete?
- How to speed up the make command execution time
- Make install
- Make Python 3.12.5 the default version
- Test if Python 3.12.5 is the default version
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 arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.