Solarian Programmer

My programming ramblings

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:

1 sudo apt update
2 sudo apt upgrade

Next, install wget and the default GCC toolchain with:

1 sudo apt install wget build-essential

We’ll also need to install a few prerequisites for building Python:

1 sudo apt install libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev
2 sudo apt install libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev libffi-dev uuid-dev

If you want to be able to develop GUI applications with Python, you will also need to build the Tkinter module:

1 sudo apt install tk-dev

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:

1 wget
2 tar xf Python-3.7.4.tar.xz
3 cd Python-3.7.4
4 ./configure --enable-optimizations --prefix=/opt/python-3.7.4
5 make -j 8
6 sudo make install

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.

1 export PATH=/opt/python-3.7.4/bin:$PATH

After the above, you can invoke the new Python interpreter with:

1 python3

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:

1 import sys
3 name = "Solarian Programmer"
4 print(f"Hello {name}, from Python version {sys.version}")

This is what I see if I run the above code on my Debian machine:

1 ~$ ~$ python3
2 Hello Solarian Programmer, from Python version 3.7.4 (default, Jul 13 2019, 18:11:56)
3 [GCC 8.3.0]
4 ~$

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:

1 python3 -m venv py3.7.4-env
2 source py3.7.4-env/bin/activate
3 pip install --upgrade pip

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.

1 pip install Pillow

Here is a short demo program that will print the Pillow version:

1 from PIL import Image
2 print(Image.__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:

1 cd /opt
2 tar -cjvf ~/python-3.7.4-x86_64-debian-10.tar.bz2 python-3.7.4

After the above, you will end up with a 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:

Show Comments