Building Python 3.7 from source on Ubuntu and Debian Linux
Posted on June 30, 2017 by Paul
Updated 13 July 2019
This is a short article about building Python 3.7 from source on Ubuntu 18.04 or Debian 10 Linux. At the time of this writing, on Ubuntu LTS the default Python version is 3.6 and on Debian stable the default is Python 3.7.3 (which is slightly older than the current stable Python version).
Python 3.7 comes with many more improvements vs the old versions. You can read more about what’s new in Python 3.7 here. The procedure described in this tutorial also works with Windows Subsystem for Linux, WSL, aka Bash on Ubuntu on Windows and it should work with the latest Raspbian on a Raspberry Pi device.
Another advantage of building Python from source is that you will be able to install latest versions of various libraries like OpenCV, Pillow and so on. As an example, at the end of the article I will show you how to use pip to install the Pillow image library.
First, make sure your system is fully updated:
Next, install wget and the default GCC toolchain with:
We’ll also need to install a few prerequisites for building Python:
If you want to be able to develop GUI applications with Python, you will also need to build the Tkinter module:
Next, we are going to get the latest stable version of Python, build it and install it in /opt. We want to keep this completely isolated from the system Python.
At the time of this writing, the latest stable version of Python is 3.7.4, if you want to use a newer version, change the next instructions accordingly:
Next, temporary add Python to your PATH. Please note, that this will temporary shadow, until you close the Terminal or until you log out, the default system python3.
After the above, you can invoke the new Python interpreter with:
If you want, or need, to go back to the default system Python version, close and reopen your Terminal or log out and log in to your system.
Time to write a small test program:
This is what I see if I run the above code on my Debian machine:
As promised, as an optional example, I will show you how to install a Python 3.7 library using pip. In order to follow best practices, I will install the new library in a virtual environment:
First line from the above will create a virtual environment named py3.7.4-env. Second line will activate the environment. Last line will update the local pip package manager. Once en environment is activated, you can simply use the python and pip commands. Any modifications you do inside an environment will have effect only in the respective environment, the global installed Python interpreter and libraries will not be affected.
Here is a short demo program that will print the Pillow version:
Just keep in mind that every time you want to use the py3.7.4-env environment, you’ll need to activate it first. Also, after you will log out or restart the Terminal, python3 will invoke the system Python installation. In order to be able to use Python 3.7.4 activate the py3.7.4-env environment or add the Python version from /opt to your PATH, like we did earlier.
As a rule of thumb, you should always use a virtual environment for Python development. This will let you use the latest Python version, while keeping the system Python unchanged.
Side note: If you need to install your compiled Python on more than one machine, go to /opt and archive the already built Python. Here is a possible workflow:
After the above, you will end up with a .tar.bz archive in your home folder. Copy the archive to the new machine and extract if to /opt. Next, install the prerequisites like in the beginning of this article. Skip the build part.
If you want to learn more about Python, I recommend reading Python Crash Course by Eric Matthes:
Another good Python book, for more advanced users, is Python Playground by Mahesh Venkitachalam: