Install Python from Source
Python is one of the most used programming language and in this tutorial we will show you how to install Python from source on a CentOS 7 operating system. We can safely say that Python, conceived in the late 1980s by Guido van Rossum, comes these days preinstalled on most Linux distributions. If it is not there by default then we can always install it, for example on CentOS we can get Python using yum utility but as we know that will not be the latest Python version available. We may have a new project that requires to be built from the scratch and would be quite a good idea to take advantage of the latest Python release. Maybe we need a particular Python version instead of the default one, might be faster, specific module compatibility or new features have been just released that can improve our delivery speed or application response time. Installing latest Python version from source make perfect sense in some cases but as we said in the beginning in this short tutorial we will only guide you how to setup Python from source and later on you can decide where and how to use it in your projects.
Table of contents
Install additional packages for CentOS
Download Python and decompress
Compile Python
Create symlink for Python3
Check Python version
Cleanup
Install additional packages for CentOS
In order to install Python we need to install first a few packages like libffi
, zlib
and also openssl
on our CentOS 7 using yum
utility, all these packages will be used during installation process, so lets start install these dependencies as shown in the example below:
$ yum groupinstall -y "Development Tools"
$ yum -y install libffi-devel zlib-devel openssl-devel
Download Python and decompress
Now that we have all needed packages for our Python installation we can start download latest – as of today – Python version. If you want a specific Python version then just check https://www.python.org/ftp/python/ web page and pick the right one that suits to your project.
In this tutorial we will be installing Python 3.7.2 but please feel free to replace this version if it is not the right one for your project.
$ cd /usr/src
$ wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
Once Python download is completed then we can start decompressing the archive:
$ tar -xJf Python-*
$ cd Python-3.7.2
Compile Python
First thing that we need to do after decompressing Python archive is to configure the package, lets run the next command in order to configure Python package:
$ ./configure
Once the configuration process ends we need to run both make
and make altinstall
in order to compile Python, please be aware that this compiling process will take some time so better to be patient:
$ make
$ make altinstall
Create symlink for Python3
If no errors were encountered during compiling process then we are safe to say that we are now almost ready to use Python3 but before that lets make sure that we have proper symlinks in place to call Python, below we have an example of how symlinks can be made:
$ cd /usr/bin/
$ ln -s /usr/local/bin/python3 python3
$ ln -s /usr/local/bin/pip3 pip3
Check Python version
Lets try now to see if our Python3 installation works by doing a basic version check like shown below:
$ python3 --version
If everything went fine then we should get a similar output like this one:
Python 3.7.2
Lets perform the same check for pip3
:
$ pip3 --version
This is what we should get on our terminal window if pip3 has been correctly installed and symlinked:
pip 18.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
Cleanup
Knowing now that our Python3 installation was successful we should perform some house keeping by cleaning up all files that we don’t need anymore:
$ rm -rf /usr/src/Python*