How to install Python 3 from Source on CentOS 7

python

Introduction

Many times we end up with machine having older version of Python, especially CentOS. In this quick tutorial, we will see how to install python from source. We are installing the latest Python version 3.8.3 here. But you can apply these steps for any version. Just make sure to update source link accordingly.

Installation Steps

Update system and install basic development environment

yum update -y
yum groupinstall -y 'Development Tools'
yum install -y wget zlib-devel

Grab Python tarball of version your required.

wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tar.xz

Extract tarball and go to root directory

tar -xf Python-3.8.3.tar.xz
cd Python-3.8.3

Configure the source. Here we are specifying two switches.
–prefix Determines where to keep installation files
–enabled-shared To make share shared libraries are build for this version

./configure --prefix=/usr/local --enable-shared

Time to build the source. Execute below commands

make

Install the source. IMPORTANT: Don’t use ‘install‘ but use ‘altinstall. This will take care of not messing existing Python installation.

make altinstall

Because we used flag ‘–enabled-shared‘, we have to add links to newly created shared libraries. To do this, first find the location of newly generated shared library. (Replace 3.8.3 with your version)

find / -name libpython3.8.so.1.0

You will get list of multiple files. Something like below.

/root/Python-3.8.3/libpython3.8.so.1.0
/usr/local/lib/libpython3.8.so.1.0

First file is where my source is. We are not using it. Second file is what we looking for.

Now open /etc/ld.so.conf in your favourite editor and append the path of directory of above file to it. Below is my /etc/ld.so.conf after appending directory name.

include /etc/ld.so.conf.d/*.conf
/usr/local/lib

Save and exit. Then run below command to create link.

ldconfig

Done ! Now try to access python.

python3.8

If it works, we are good. Now let’s setup pip for this installation. First grab ‘get-pip.py’ script.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Run this script using python executable we setup just now.

python3.8 get-pip.py

Check if pip is installed properly

pip3 --version

Now we are all set. Whenever you want to install pip package, make sure to specify exact pip version. Something like below.

pip3 install poetry

Notes

In case you face errors in building/compilation, and want to start again from scratch, make sure to clean source using below commands.

make clean
make distclean

Hope this helps you. Stay Awesome !

Leave a Reply