Building Python 3.7 from source on Ubuntu and Debian Linux
Posted on June 30, 2017 by Paul
Updated 27 Nov 2018
This is a short article about building Python 3.7 from source on Ubuntu 18.04 or Debian 9 Linux. At the time of this writing Ubuntu LTS default is Python 3.6 and Debian stable default is Python 3.5. Python 3.7 comes with many more improvements vs the old 3.5 or 3.6 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.
As a side note, you should be able to use the same procedure on other Debian based systems, like Raspbian on Raspberry Pi, just replace apt with apt-get and you are good to go.
First, make sure your system is fully updated:
Next, install the default GCC toolchain with:
Next, we need to install a few prerequisites for building Python:
At the time of this writing, the latest stable version of Python is 3.7.1, if you want to use a newer version change the next instructions accordingly:
Please note the use of sudo make altinstall instead of the typical Linux/Unix sudo make install. Using altinstall will ensure that you don’t mess with the default system Python.
Once the above is finished, you could invoke the new Python interpreter with:
python3 will invoke the system Python version.
Time to write a small test program:
This is what I see if I run the above code on my Debian machine:
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: