Compile and Install python 3.7

Sometimes we can find ourselves with an old system that we cannot change for some reason. Being old in this system we can not install from Python 3.7 package, to avoid this problem we must compile and install python 3.7.

Dependencies

For example if we have a CentOS 7 system, we must install the following dependencies:

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

On the other hand if we are using Ubuntu or Debian, the dependencies would be these:

apt install build-essential libssl-dev libcrypto++-dev  libbz2-dev libffi-dev zlib1g zlib1g-dev

Download source code

After installing the dependencies, we must download the source code package of the python version to be compiled:

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

Compiling the source code

After downloading the package, unzip the package and go into the directory:

tar xzf Python-3.7.9.tgz
cd Python-3.7.9

Once inside the directory, we compile the version:

./configure --enable-optimizations

When the compilation finishes successfully, we must install the generated binaries. By doing so with “altinstall” we install a secondary python without overwriting an existing version of Python:

make altinstall

One more step to do is to install pip in order to install more packages, for this we will execute the following:

wget https://bootstrap.pypa.io/get-pip.py
python3.7 get-pip.py

It is also a good idea to install a version of virtualenv, a package for generating virtual environments:

pip3.7 install virtualenv

Finally to keep the default binaries of the other python versions:

mv /usr/local/bin/pip /usr/local/bin/pip.new
mv /usr/local/bin/virtualenv /usr/local/bin/virtualenv-3.7

To summarize all this in a script, here is the following:

#/bin/bash
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar xzf Python-3.7.9.tgz
cd Python-3.7.9
./configure --enable-optimizations
make altinstall
wget https://bootstrap.pypa.io/get-pip.py
python3.7 get-pip.py
pip3.7 install virtualenv
mv /usr/local/bin/pip /usr/local/bin/pip.new
mv /usr/local/bin/virtualenv /usr/local/bin/virtualenv-3.7

Leave a Reply