Debian is known for its stable version, which unfortunately also means outdated software packages. This means that when the stable version is only updated every two years, you end up with outdated software packages. For example, the current Debian still runs with a Python version that no longer meets the current standard. At the same time, other software packages depend on this exact Python version, even if you want to benefit from the new features in Python programming. The solution could be a system upgrade to "Testing" or even “sid/Unstable”. However, this is not recommended; it's not for nothing that these releases are called Testing and Unstable.
A more elegant approach is to install Python 3.12.3—and any subsequent version—from source code alongside the system's native Python version. The system remains stable because the default Python version isn't replaced, and you can still work with the latest Python version by explicitly calling python3.12 or setting it as the interpreter in your IDE.
~# apt update
~# apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev curl libbz2-dev libsqlite3-dev wget tk-dev liblzma-dev
First, the package sources are updated to include the latest available package information. Next, various development libraries and compiler tools are installed, which are necessary for compiling Python from source code. These libraries ensure that Python supports many features (e.g., SSL, database access). Next, change to the /usr/src
directory and download the current or desired Python source code.
~# cd /usr/src
~# wget www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
The /usr/src
directory is chosen as the working directory, as source code for system software is typically stored there. The Python source code itself is downloaded to the current directory as a compressed archive.
~# tar -xzf Python-3.12.3.tgz
~# cd Python-3.12.3
The archive is unpacked, creating the Python-3.12.3 folder, which is then navigated to in order to prepare for compilation.
~# ./configure --enable-optimizations
The configure
script checks the system environment and prepares the makefiles for the compilation process. The --enable-optimizations
option enables additional optimizations during compilation, such as enabling Profile-Guided Optimization (PGO). This improves Python's runtime performance, but the compilation process takes slightly longer.
~# make -j$(nproc)
The make
command starts the compilation process. The -j$(nproc)
option ensures that as many parallel processes as there are CPU cores are used. This significantly speeds up compilation.
~# make altinstall
The make altinstall
command installs Python without overwriting the existing system Python version. This is important because Debian internally binds many system tools to the default Python version, and overwriting it can cause problems. For example, altinstall
creates the command python3.12
instead of python3
, allowing both versions to be used in parallel.
~# python3.12 --version
With this command you can now check whether Python 3.12 has been installed correctly and is available.