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 Amazon Linux 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

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

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

We can now delete the downloaded tar file

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

Or

Build and Install

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

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

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.

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

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.

Similarly, get the pip

https://thewiz.net/installing_python_from_the_source_332a.html