Python v3.6+ on Debian 9

Warning

The following is for advanced users that either want to be ahead of the curve or require the most recent solution in general.

Warning

This hereafter given method while proven to work can seriously harm proper function of your system.
Therefore:
Users without advanced knowledge about GNU/Linux in general and Debian especial are not encouraged to use this solution; neither does Ka-Ro endorse use of hereby given solution.

Warning

Only for Debian 9!

Update the system

Before beginning update all the system packages to be most recent:

sudo apt-get update && sudo apt-get upgrade

Installing Python 3.6.x from source

Note

The here given example uses Version of Python is 3.6.10 (which at the time at writing is the most current and can be found here)
The specific the version string:
Python 3.6.10 - Dec. 18, 2019

Building and installing Python 3.6.x from source, is recommended as the safest way to upgrade the system’s python, unlike adding new repositories to apt sources, espcially something like testing, etc., it doesn’t bear the risk that thereby conflicting packages may be installed.

Begin by installing the required build tools for Python 3.6.x:

Note

This step might be skipped as the package given probably already installed for proper workings of the Yocto environment.

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev
sudo apt-get install -y libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm
sudo apt-get install -y libncurses5-dev  libncursesw5-dev xz-utils tk-dev

Download the Python 3.6.10 source file using ‘wget’:

Note

Be sure to use the most recent version, see here fore more.

wget https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tgz

Unpack the Python 3.6.10 source file:

tar xvf Python-3.6.10.tgz

Enter the Python-3.6.10 directory and run ‘./configure’ to prepare the build:

cd Python-3.6.10
./configure --enable-optimizations --with-ensurepip=install

Note

The flag –with-ensurepip=install create a python install with it’s own integrated pip.

Then run the following command to build Python 3.6.10:

Note

Where ‘-j4’ equals the output of ‘nproc’ - i.e. the count of CPU cores available -, e.g. ‘-j$(nproc)

make -j4

And then run this command to install Python 3.6.10:

sudo make altinstall

Note

This creates an ‘alternative install’ and unlike the standard packages will be located under /usr/local/bin/

Now you can open the Python 3.6.10 interpreter by executing the following command:

python3.6

Set the system to default to Python 3.6

Note

Below given setup and procedure is required as defaulting to Python3 in Debian 9 will break the package management!

This requires to have installed the Debian own package python3-minimal.
This package will allow proper functioning of the Debian package management in conjunction with Python3.

$ apt-cache show python3-minimal
Package: python3-minimal
Version: 3.5.3-1
Installed-Size: 120
Maintainer: Matthias Klose <doko@debian.org>
Architecture: amd64
[...]

We are using the Debian provided py3clean (pyclean for Python 3.5+) to guarantee that the standard package manager(s) from Debian ‘dpkg’ and ‘apt’ are still capable to work.

When the system would be setup to only use the newly installed Python v3.6 it would run into incompatibility problems, as script that require Python 2.7 will fail with Python 3.+ that goes so far that Debian to this date is still running Python 2.7 as default, even when the more recent Python v3.5 from the distro’s repositories is installed.

sudo -s
update-alternatives --query python
cd /etc/alternatives
ls -Al py*
which py3clean
which pyclean
mv /usr/bin/pyclean /usr/bin/py2clean
pushd /usr/bin
ln -s /etc/alternatives/pyclean pyclean
popd
update-alternatives --install /usr/bin/pyclean pyclean /usr/bin/py2clean 1
update-alternatives --install /usr/bin/pyclean pyclean /usr/bin/py3clean 2
update-alternatives --query pyclean
update-alternatives --query python
update-alternatives --config python
ls -Al python
update-alternatives --config pyclean

This is based in excerpts on this blog entry: How to Install Python 3.6.4 on Debian 9