Systemadministrator mit einer gewissen Neigung zum Chaos.

27 Jul 2021

How to install the latest Python version on Debian/Ubuntu-based systems

If you’re a Python developer using Debian Buster for example, you’ll probably have faced the problem that Buster only provides version 3.7.3 in its package sources. Which isn’t a problem if it’s only about executing Python programs, but if you want to develop programs that take advantage of the latest version of Python. Then you’ve to build and install this Python version yourself.

This tutorial shows how to build Python using version 3.9.6, but it’s easy to build another version with these instructions. No matter if the version you want is older or newer. Just change the corresponding version labels to the ones of your desired version.

Step 1: Create a clean build environment

To have a clean environment for building Python, we create a directory called Build in the home directory of the current user. When the build and installation is complete, this directory can be easily deleted so that no leftovers remain on the system.

mkdir -v ~/Build
cd ~/Build

The -v option is for verbose. For more information about the execution of the command.

Step 2: Download the Python tarball

First you need the source code of the current Python version. You can easily download it as a tarball from the download section of the website. In this tutorial we use the tool wget to download the tarball from the official FTP server and save it directly in the chosen directory.

Download the tarball via wget:

wget ""

Step 3: Install dependencies

Python and some of its modules have dependencies. To avoid problems during building and to make Python fully functional, we install these dependencies.

If sudo is not installed on your system, use su - root and execute the commands without the sudo prefix.

1. Update the package manager and upgrade the packages on your system.

sudo apt update
sudo apt upgrade

2. Installation of dependencies

sudo apt install zlib1g-dev libc6-dev libexpat1-dev libssl-dev libbz2-dev \
                 libffi-dev libdb-dev liblzma-dev libncurses-dev \
                 libncursesw5-dev libsqlite3-dev libreadline-dev uuid-dev \
                 libgdbm-dev tk-dev libbluetooth-dev build-essential

tk-dev should only be installed on a system with GUI.

Now your system should be ready for building Python!

Step 4: Build Python

1. Unpack tarball and change directory

tar -xf Python-3.9.6.tgz
cd Python-3.9.6

2. Prepare the build with ./configure

CXX="/usr/bin/g++" ./configure --prefix=/usr/local \
                               --with-system-expat \
                               --with-system-ffi \
                               --with-ensurepip=install \

CXX="/usr/bin/g++" Avoid an annoying message during configuration.

--prefix=/usr/local set the path where the Python build gets installed.

--enable-system-expat enables linking against system version of Expat.

--enable-system-ffi enables linking against system version of libffi.

--with-ensurepip=install install pip and setuptools with the Python build.

--enable-optimizations enable optimizations within Python.

3. Execute the build with make

make -s -j$(nproc)

-j option to split the building into parallel steps to speed up the process. The number should correspond to the amount of threads on your CPU.

$(nproc) uses the amount of threads automatically.

4. Install the Python build

Finally you can install the just build Python version. If you use the --prefix=/usr/local as recommended above, use altinstall to avoid overwriting the system Python. You also need to run this install as root.

If sudo is not installed on your system, use su - root and execute the commands without the sudo prefix.

sudo make altinstall

Step 5: Test installed Python build

Check if --version return the installed Python version.

python3.9 --version

The output should be Python 3.9.6

Run the Python test suite to make sure everything works as expected.

python3.9 -m test

The output should include SUCCESS


Congratulations, you have installed the latest version of Python successfully and can now start develop with this version. Your installation of Debian/Ubuntu is not affected by this Python build.