Installing Python from the Source


Most Linux distributions come with a built in package manager. The applications that can be installed by the package manager are restricted and limited by the community maintaining the packages. Because of this, we cannot have the latest versions of several edge driven Open Source applications. Python is one of them.

The latest version on Ubuntu is Python 3.6, not 3.8. We need some way to install the latest. This is what I did when faced with this problem. You too can upgrade the Python on your machine.

Download the tar ball


Python does not give a standalone binary executable for Linux. But, we can build it for ourselves - from the source code. Don't worry. It is not as difficult as it sounds. Just follow the instructions below, and you should be done in less than 15 minutes.

We start with downloading the source code. For this, go to the Python Downloads Page. It has links to the source code for different versions of Python. Pick up the latest stable version, and download the corresponding source tar ball. There you go. Save it and extract it in a folder.

You can also use a simple wget to pull the latest version from the command line

wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz

You can extract and install this anywhere you like. But, advised to do that in the /opt folder. That is meant for such installations.

sudo mv Python-3.8.5.tgz /opt/.

Now, we can extract this, to get the raw source code.

sudo tar xzf Python-3.8.5.tgz

We can now delete the downloaded tar file

sudo rm -f /opt/Python-3.8.5.tgz

GCC Compiler


Before we start anything, install the GCC compiler. Depending upon the Linux flavor that you are using, install the compiler using one of the below commands

sudo yum install gcc openssl-devel bzip2-devel libffi-devel

Or

sudo apt install gcc openssl-devel bzip2-devel libffi-devel

Build and Install


Once you extract it, you will see a folder Python-3.8.5 containing the source code. Get into that folder

cd Python-3.8.5

In there, you will see a simple bash script configure. Run that.

sudo ./configure --enable-optimizations

It is quite a verbose script. It will setup all that you need to build the binaries out of the code.

Next, we use altinstall to build the binaries. It is important to use altinstall, so that the binaries we generate do not overwrite the existing Python binaries that the package manager has provided us.

sudo make altinstall

This will again generate several logs and then complete the job.

Verify


There, the Python3.8 is now installed into the /usr/local/bin folder. Since we used the altinstall, it did not overwrite the existing installation of Python. We can verify this by checking the version of the Python installation

python3.8 -V

This will show the version 3.8.5. There, you are done!

If you like, you can now alter the link in the /usr/bin folder.

sudo ln -s /usr/local/bin/python3.8 /usr/bin/python

Similarly, get the pip

sudo ln -s /usr/local/bin/pip3.8 /usr/bin/pip