Today I bring you an easy method to install python 3.6 or higher on Debian 9, and incidentally solve a couple of problems that arose during this installation.
These steps have been tested only in Debian 9, but they will surely be equally compatible with other OS such as Ubuntu or similar. In the case of using another type of distribution that is not based on Debian, it would be necessary to rename the packages to be installed, and I also cannot ensure that it works.
In addition to installing Python 3.6, the installation of Python 3.7 has been tested using the same method, and all worked without problem.
Source code download
The first thing to do is install the requirements for compilation.
$ sudo apt-get update $ sudo apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget $ sudo apt-get install libsqlite3-0
I have separated the sqlite3 library in another line, because it is not mandatory for the installation of Python 3.6, but it is advisable if we need its use or we plan to need it, since to do so later we would have to recompile python 3.6 again.
Once all the packages necessary for compilation are installed, we will proceed to download the source code. For this we will visit the Python page and download the version we need.
$ wget https://www.python.org/ftp/python/3.6.10/Python-3.6.10.tar.xz
Once we have downloaded it, we will proceed to unzip it and enter the unzipped folder.
$ tar xvf Python-3.6.10.tar.xz $ cd Python-3.6.10
Being already in the source code folder, we will proceed to configure the compilation with the options –enable-optimizations and –enable-loadable-sqlite-extensions. This last option is to enable the compilation of SQLite libraries, and like the library, it is optional. Of course, for it to work it is necessary to have previously installed the library.
$ ./configure --enable-optimizations --enable-loadable-sqlite-extensions
Once the compilation configuration is finished, we will proceed to execute the make command to generate the binaries.
$ make -j 8
NOTE: The -j Indicates the number of threads to use, so it is recommended to configure it according to the number of processors we have. In the event that we do not want to use the full power of the machine so as not to affect other services, it may be lower.
Once the compilation process is finished (which can take 15-20 minutes depending on the power of the machine), we can proceed to install python 3.6 on Debian 9.
$ sudo make altinstall
With this we will have finished installing python version 3.6 in our Debian. Now we must verify that it has been installed correctly.
$ python3.6 --version Python 3.6.10
With this we would have already managed to install Python 3.6 on Debian and therefore we can use it through the python3.6 and pip3.6 commands, but if what you want is to do Make it the system default, continue reading the article.
Install as default
The process followed above is to install the version of python that we need, but this does not make it the system default. To be able to set it as default we will have to proceed to configure it as an alternative for python3.
NOTE: This process causes some OS functions to be affected, so we will then proceed to solve these problems. The problem is not complex, since in case of needing it we can always go back with two commands.
Before continuing, it is important to know what versions of python3 we have installed, so we can proceed to configure them all as an alternative and it will be easier for us to change from one to another.
$ ls -1 /usr/bin/python3* /usr/bin/python3 /usr/bin/python3.5 /usr/bin/python3.5-config /usr/bin/python3.5m /usr/bin/python3.5m-config /usr/bin/python3-config /usr/bin/python3-jsondiff /usr/bin/python3-jsonpatch /usr/bin/python3-jsonpointer /usr/bin/python3m /usr/bin/python3m-config
Since the system in which I did the test is clean, we only have version 3.5 which is the default. Here we will not see the version of python3 that we just installed, since it is installed in another folder.
Once we know the versions of python3 that we have, we will proceed to configure them all as alternatives for python3.
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1 $ sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.6 2
It will also be necessary that we do the same process with pip to avoid problems when installing libraries.
$ sudo mv /usr/bin/pip3 /usr/bin/pip3.5 $ sudo update-alternatives --install /usr/bin/pip3 pip3 /usr/bin/pip3.5 1 $ sudo update-alternatives --install /usr/bin/pip3 pip3 /usr/local/bin/pip3.6 2
NOTE: The first command is to rename the binary of pip3.5 and so we have no problems installing the alternatives. This is because when the python3-pip package is installed, it is installed directly as pip3. This we will have to take into account in the future if we update the package.
Once we have configured the python3 and pip3 alternatives, we could already configure them with the following commands:
$ update-alternatives --config python3 There are 2 choices for the alternative python3 (providing /usr/bin/python3). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/local/bin/python3.6 2 auto mode 1 /usr/bin/python3.5 1 manual mode * 2 /usr/local/bin/python3.6 2 manual mode Press <enter> to keep the current choice[*], or type selection number:
$ update-alternatives --config pip3 Existen 2 opciones para la alternativa pip3 (que provee /usr/bin/pip3). Selección Ruta Prioridad Estado ------------------------------------------------------------ * 0 /usr/bin/pip3.6 2 modo automático 1 /usr/bin/pip3.5 1 modo manual 2 /usr/bin/pip3.6 2 modo manual Pulse <Intro> para mantener el valor por omisión [*] o pulse un número de selección:
Solution to problems in the OS
Unfortunately, Debian does not like that we change the python3 version by default, and this will cause a series of problems with some commands such as lsb_release and apt-get, which will affect the installation of certain packages with pip, and obviously with apt-get. This is mainly because there are some python3 libraries that come with the OS, and that are necessary for its operation.
The first option is to change the version of python3 every time we are going to execute any of those commands, with the update-alternatives command that we saw above. This apart from being cumbersome, does not solve the problem of installing packages with pip3.6, so we will have a version of python3 that will give problems with the installation of certain packages.
The second option is to patch the lsb_release.py library so that it always runs with python 3.5 regardless of the default version. This solves the problem of the lsb_release command, but we will continue with the problem with the apt-get command, so we will have to change the version every time we want to use it.
The third option we have is to link the python3.5 libraries, since these are compatible with python3.6 and will work without problems. To do this we will follow these steps.
We link with ln the library lsb_release.py to the python 3.6 folder. We can simply copy it, but if we link it, it will be updated along with the python version 3.5.
$ sudo ln -s /usr/share/pyshared/lsb_release.py /usr/lib/python3.6/site-packages/lsb_release.py
Once the lsb_release library is linked, we can only solve the problem with apt, for which we will also link the apt_pkg libraries to falsify that we have them for version 3.6.
$ cd /usr/lib/python3/dist-packages $ ln -s apt_inst.cpython-35m-x86_64-linux-gnu.so apt_inst.cpython-36m-x86_64-linux-gnu.so $ ln -s apt_pkg.cpython-35m-x86_64-linux-gnu.so apt_pkg.cpython-36m-x86_64-linux-gnu.so
This will have already solved the two main problems.
I hope this manual has a server for you and that it takes away some pain that another head.
4 thoughts on “Install Python 3.6+ on Debian 9”
I’m with you. The only reason to change the python version is because you really need it. In my case was because a program that we needed was created for this python version and was not compatible with older versions. Also now is not a big problem, because you can use Docker to install the version you need and avoid to modify the SO.
I followed your instructions to install python3.9.0 to Debian9 and everything worked fine. Once I needed to install telethon which told me that my sqlite3 does not work but I’ve found the way to use it without sqlite3. Then I needed to replace telethon with pyrogram and I could not because of that sqlite3. I wrote my story here https://stackoverflow.com/questions/65336876/python3-5-sees-but-python3-9-does-not-see-sqlite3 and here https://stackoverflow.com/questions/65342799/python3-9-sqlite2-connect-always-give-segmentation-fault
Inspecting the issue I’ve read a lot of opinions that changing python in such a “clever” OS like Debian is a bad idea. I agree with them.
Thank you for your commnets. I’ll review the process to see if maybe I forgot something.
The final symbolic link command was `ln -s /usr/share/pyshared/lsb_release.py /usr/local/lib/python3.6/site-packages/lsb_release.py` for me (3.6 was installed into /usr/local/lib i guess)