Solarian Programmer

My programming ramblings

Building Python 3.6 from source on Ubuntu and Debian Linux

Posted on June 30, 2017 by Sol

This is a short article about building Python 3.6 from source on Ubuntu 16.04 or Debian 9 Linux. At the time of this writing both Ubuntu LTS and Debian stable come with Python 3.5. Python 3.6 comes with a production ready asyncio module, new formatted string literals and many more improvements vs the old 3.5 version. You can read more about what’s new in Python 3.6 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:

1 sudo apt update
2 sudo apt upgrade

Next, install the default GCC toolchain with:

1 sudo apt install build-essential

Next, we 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 tk-dev

At the time of this writing, the latest stable version of Python is 3.6.1, if you want to use a newer version change the next instructions accordingly:

1 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
2 tar xf Python-3.6.1.tar.xz
3 cd Python-3.6.1
4 ./configure --enable-optimizations
5 make -j 8
6 sudo make altinstall

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:

1 python3.6

python3 will invoke the system Python version, 3.5.

Time to write a small test program:

1 import sys
2 
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.6 test.py
2 Hello Solarian Programmer, from Python version 3.6.1 (default, Jun 30 2017, 11:52:56)
3 [GCC 6.3.0 20170516]
4 ~$

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:

comments powered by Disqus