Building python from source on Ubuntu 20.04 LTS Focal Fossa | by Sushrut Ashtikar | Towards Data Science

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.
Install essential dependencies required for building library
sudo apt-get updatesudo 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
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/srcsudo 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
cd Python-3.6.9sudo ./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
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 notoverwritten 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
The python version will be built with suffix python3.6
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
I have created a shell script for the same, you can download the script file from here.
github.com
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.pysudo python3.6 get-pip.py
Just like python3.6 use prefix pip3.6 for installation like this
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.6alias pip36=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