Building python from source on Ubuntu 20.04 LTS Focal Fossa

Learn how to build your favourite language from its source code on Linux based operating system.

Sushrut Ashtikar
Towards Data Science

--

Build your favourite language on your favourite operating system

PS: Apart from users facing dependency issues ,this blog is for anyone who would love to try installing python from source code on any Linux based operating systems

If you have upgraded your operating system and wandering why some libraries which were easily install-able on Ubuntu 18.04 are not getting installed on 20.04. I faced the same issue when one of my development environment required a library which I tried to install using the built-in python. Unfortunately, I faced a dependency issue.

The reason behind this was Ubuntu 20.04 comes with built in python version 3.8.2 whereas Ubuntu 18.04 comes with Python 3.6.x

There may be different libraries which you are trying to install which depends on Python 3.6. Don’t worry its as easy as 1..2..3

Let’s start with a specific python version, I will be using Python 3.6.9 in this blog because on the previous system I was working on it.

First Things First

Install essential dependencies required for building library

sudo apt-get update
sudo apt-get install -y build-essential checkinstall

and

sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

Lets Build

Start with changing the directory and downloading source code from python.

I will be installing it from /usr/src but you can use any location of your choice

cd /usr/src
sudo wget https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz

Extracting downloaded package

sudo tar xzf Python-3.6.9.tgz

Compiling Source

cd Python-3.6.9
sudo ./configure --enable-optimizations

Here we have added a new flag --enable-optimizations
This sets the default make targets up to enable Profile Guided Optimization (PGO) and may be used to auto-enable Link Time Optimization (LTO) on some platforms.

Refer here for more details on --enable-optimizations

Step we all are waiting for

Building python

sudo make altinstall

Why altinstall when we can use install

Reason: On Unix and Mac systems if you intend to install multiple versions of Python you must take care that your primary python executable is not
overwritten by the installation of a different version. altinstall builds specific version of python without overwriting your default version so that you can use multiple version of python.

Note: This step may take time depending on device configuration

Final output will be like this

The python version will be built with suffix python3.6

Verify Installation

python3.6 --version

Notice I have used python3.6 instead. If you try to run python3 --version it will show built-in python version which is 3.8 as of Ubuntu 20.04

Notice the difference

Build everything with a single command

I have created a shell script for the same, you can download the script file from here.

Upgrading pip (optional)

Install dependency for pip

sudo apt-get install -y python3-distutils python3-testresources

Downloading get-pip.py and running

cd ~/
wget https://bootstrap.pypa.io/get-pip.py
sudo python3.6 get-pip.py

Just like python3.6 use prefix pip3.6 for installation like this

pip3.6 install package

Bonus

If you are annoyed by using python3.6 or pip3.6 each time, just use any one of the configurations mentioned below.

Using alias

alias py36=python3.6
alias pip36=pip3.6
alias of python3.6 and pip3.6

Limitations of alias command:
Either you run this command each time before running python or pip or add these lines at the bottom of your ~/.bashrc but it will only run on user level.
The best solution for fixing this is using update-alternatives

Using update-alternatives

Check your python path for adding it to update-alternatives config

which python3.6

My path was /usr/local/bin/python3.6

Adding path to update-alternatives config

sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.6 0

If you enjoy my blog and would like to support my work, consider buying me a coffee. Your contribution will help me keep the blog running and creating valuable content. Thank you!

That’s All Folks

--

--

Software Developer with over 5 years of expertise in building applications. Passionate about technology and always seeking to learn and implement new solutions.