Author: Dametto LucaLast Updated: Tue, Jun 29, 2021
Debian distributes the stable versions of Python 3 in their repositories, but sometimes you need to install a newer version of Python. Installing from source allows you to install the latest version of Python 3 on Debian 10.
Before proceeding with the Python3 build, install the development tools.
$ sudo apt install build-essential make
Select the latest python version from the Python website and copy the download link.
$ cd /tmp $ wget https://www.python.org/ftp/python/<VERSION>/Python-<VERSION>.tgz
After the download, extract the files.
$ tar -xvzf Python-<VERSION>.tgz $ cd Python-<VERSION>
You must install some dependencies to enable the recommended Python modules.
$ sudo apt install libncurses-dev libgdbm-dev libz-dev tk-dev libsqlite3-dev libreadline-dev liblzma-dev libffi-dev libssl-dev libbz2-dev libncursesw5-dev uuid-dev zlib1g-dev libncurses5-dev xz-utils llvm libgdbm-compat-dev
Before configuring the Python makefile, it is good to understand what the parameters will do. All the options are listed in the official documentation. Here are short explanations of the most important ones.
This is the recommended configuration for a good Python installation.
$ ./configure \ --enable-optimizations \ --with-ensurepip=install \ --prefix=/usr/local
To speed up compilation, use
$(nproc -all) to let the compiler use all the available cores.
$ make -j $(nproc --all)
Is it possible to install python also by using
install instead of
altinstall, but that will replace the current installed version and may cause issues, so, it's highly recommended to use
altinstall, as it will install it without interfering with other versions.
$ sudo make altinstall
Test the installation.
$ python3.9 -V Python 3.9.5 $ pip3.9 -V pip 21.1.2 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)