Howto build, compile and install latest Python 3.9, 3.8, 3.7 on Linux CentOS 7, 8

Do you want to install latest Python 3.9.6 (resp. 3.8.11 or 3.7.11) on Linux CentOS 7 or 8 and don’t want to break up the shipped Python?

You are in the right place. 

I have for you a short tutorial on how to build, compile and install Python 3.9,  3.8 or 3.7 on Linux CentOS 7 or 8 and run it  without destroying the shipped Python in Centos.

HINT – At the end of the article you find the cheatsheet, a set of Bash commands that you can COPY & PASTE  and run in your Linux command line.

 

What we are going to solve

  • update your CentOS box and install needed developer libraries and tools
  • download and unpack latest Python source code
  • compile Python source code
  • install Python source code and do some post-install stuff for easy using in Bash command line
  • check of created Python binaries
  • create and test the Python virtual environment

At the time of writing this post Python 3.9.6 (resp. 3.8.11 or 3.7.11) is the most current stable version of the language and the most used version CentOS is 7, the newest one is version 8. CentOS 7 is shipped with Python 2.7.5 and CentOS 8 is shipped with Python 3.6.8.

Table of contents  hide  Prerequisites Step 1 – prepare CentOS for Python compilation Step 2 – download and unpack Python source code Step 3 – compile Python source code into binaries Step 4 – make post-install stuff Step 5 – checking Python binaries Step 6 – setting up the Python virtual environment (venv) Conclusion COPY & PASTE cheatsheet – for installing latest Python 3.9, 3.8, 3.7 or beta 3.10.0b4 on Linux CentOS 7, 8

Prerequisites

You will need functional Linux CentOS 7 or 8 machine,  an access to the root account and of course an internet connection.
All the steps you can perform as an non-root user but with a support of the  

sudo

 command.

 

 

Step 1 – prepare CentOS for Python compilation

It is a good idea to have up-to-date OS system before you start doing anything else. Let’s update your CentOS with the  

yum

 command.

 

Update your the linux box sudo yum -y update

You also need some necessary libraries and developer tools to allow you to build and compile software from source code. I chose the minimal amount of packages those are included in CentOS as well. To install them use again   

yum

  command .

 

Install libraries and developer tools sudo yum -y install wget yum-utils gcc openssl-devel bzip2-devel libffi-devel

 

Step 2 – download and unpack Python source code

We download the a source code of latest Python from the official Python page https://www.python.org/ftp/python/ and extract 

Python-3.9.6.tgz

 to the  

/tmp

directory.

 

To do that perform this set of bash commands.

Download and unpack Python source code cd /tmp/ wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz tar xzf Python-3.9.6.tgz cd Python-3.9.6

 

Step 3 – compile Python source code into binaries

At the moment we have everything ready for compiling the actual Python source code.

We are going to use the switch 

--prefix=/opt/python39

 to set the root directory for all Python binaries and libraries. Of course, you can choose a folder according to your needs.  For better performance we are going to use a switch 

--enable-optimizations

  for enabling PGO (profile guided optimisation) and so yielding an extra speed boost of Python binaries around 5-10%.

 

The command  

make -j `nproc`

  will ensure using of all fo your CPU cores and will decrease a compile-time and the command 

make altinstall

  is critical because of preserving the default shipped Python binary  

/usr/bin/python

 .

 

HINT – To get the number of cpu cores of your Linux CentOS box, use these Bash commands   grep 'cpu cores' /proc/cpuinfo  or   nproc .

Now you have two options how to compile Python – with the static libraries or shared libraries. If you don’t know which way to take then use the option a).

Depending on a number of cpu cores, the compilation will take a few minutes.

 

a) compile Python source with STATIC libraries – almost in all of your cases or if you don´t know, use this option

Compile Python source with static libraries sudo ./configure --prefix=/opt/python39 --enable-optimizations --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions sudo make -j "$(nproc)" sudo make altinstall

b) compile Python source with SHARED libraries  – you should know why you want this option otherwise use option a).

Compile Python source with shared libraries sudo ./configure --prefix=/opt/python39 --enable-optimizations --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions --enable-shared LDFLAGS=\"-Wl,-rpath /usr/local/lib\" sudo make -j "$(nproc)" sudo make altinstall ldconfig

We already won’t need Python source code tarball so let’s delete it.

sudo rm /tmp/Python-3.9.6.tgz

 

Step 4 – make post-install stuff

We are going to make some symbolic links that are expected to exist for convenient Python usage.

Add some symbolic links sudo ln -s /opt/python39/bin/python3.9 /opt/python39/bin/python3 sudo ln -s /opt/python39/bin/python3.9 /usr/bin/python39 sudo ln -s /opt/python39/bin/python3.9 /opt/python39/bin/python sudo ln -s /opt/python39/bin/python3.9-config /opt/python39/bin/python-config sudo ln -s /opt/python39/bin/pydoc3.9 /opt/python39/bin/pydoc sudo ln -s /opt/python39/bin/idle3.9 /opt/python39/bin/idle

We are also  going to  add some symbolic links for the  

pip

 binary.

 

Add some symbolic links for command pip sudo ln -s /opt/python39/bin/pip3.9 /opt/python39/bin/pip3 sudo ln -s /opt/python39/bin/pip3.9 /opt/python39/bin/pip

 

Step 5 – checking Python binaries

After installation you will find the Python interpreter at the location  

/opt/python39/bin/

.

 

Let’s  do some tests of Python binaries by typing:

Check Python binaries /opt/python39/bin/pip -V >>> pip 20.0.4 from /opt/python39/lib/python3.9/site-packages/pip (python 3.9.6)   /opt/python39/bin/python -V >>> Python 3.9.6   which python39 >>> /usr/bin/python39   /usr/bin/python39 -V >>> Python 3.9.6   /opt/python39/bin/python3.9-config --prefix >>> /opt/python39   # test out shipped Python 2.x whether it still ok /usr/bin/python -V >>> Python 2.7.5

 

Step 6 – setting up the Python virtual environment (venv)

Nowadays Python virtual environment is a great tool and almost necessary for every Python project. It enables you to have more isolated Python spaces on one Linux box. Python project have its own set of dependencies and modules.

You can set up as many Python programming environments as you want.  Each of them is basically a directory that includes a few scripts and binaries, e.g. 

python

  or  

pip

 .

 

So let’s create the one.

Create Python virtual environment sudo /opt/python39/bin/python -m venv /home/hanz/mydjango.cz/env   ls -l /home/hanz/mydjango.eu/env >>> total 16 >>> drwxr-xr-x 2 root root 4096 Nov 3 18:30 bin >>> drwxr-xr-x 2 root root 4096 Nov 3 18:30 include >>> drwxr-xr-x 3 root root 4096 Nov 3 18:30 lib >>> lrwxrwxrwx 1 root root 3 Nov 3 18:30 lib64 -> lib >>> -rw-r--r-- 1 root root 78 Nov 3 18:30 pyvenv.cfg

Now that we have the environment created and next, we have to activate that with a command  

source /home/hanz/mydjango.cz/env/bin/activate

 We will again check Python binaries and run a small inline program “Hello, World!”. The command  

deactivate

  will disable the Python environment and will set back the shipped Python into our Linux machine.

 

Check Python virtual environment source /home/hanz/mydjango.cz/env/bin/activate >>> (env) [hanz@mydjango_cz /]#   (env) [hanz@mydjango_cz /]# pip -V >>> pip 20.0.4 from /home/mydjango.cz/env/lib/python3.9/site-packages/pip (python 3.9.6) >>> (env) [hanz@mydjango_cz /]#   (env) [hanz@mydjango_cz /]# python -c 'print("Hello, World!")' >>> (env) [hanz@mydjango_cz /]# Hello, World!   (env) [hanz@mydjango_cz /]# deactivate >>> [hanz@mydjango_cz /]#

 

Conclusion

Congratulations!
At this point, you have installed the latest Python 3.9.6, Python 3.8.11 or Python 3.7.11 on your  local CentOS machine and for example, you can start coding any project with my favourite web framework Django.  Check out my tutorial for setting up a Django runtime environment build on Nginx web server and uWSGI Python gateway.

I hope this guide will help you and if you have some tips for improvements or found a mistake, let me know.

Enjoy!
Hanz


COPY & PASTE cheatsheet – for installing latest Python 3.9, 3.8, 3.7 or beta 3.10.0b4 on Linux CentOS 7, 8

Just choose your desired version of Python, copy and paste into your Linux Bash command line and have a cup of coffee.  All is going to be finished in a few minutes.

INFO – Don’t worry about the shipped Python that is going to be operative as it is.

Installing Python 3.9.6 to the directory /opt/python39

COPY & PASTE install script for Python 3.9 cd /tmp/; wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz; tar xzf Python-3.9.6.tgz;   cd Python-3.9.6; sudo ./configure --prefix=/opt/python39 --enable-optimizations --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions; sudo make -j $(nproc); sudo make altinstall;   sudo rm /tmp/Python-3.9.6.tgz;   # add symbolic links sudo ln -s /opt/python39/bin/python3.9 /opt/python39/bin/python3; sudo ln -s /opt/python39/bin/python3.9 /opt/python39/bin/python; sudo ln -s /opt/python39/bin/python3.9-config /opt/python39/bin/python-config; sudo ln -s /opt/python39/bin/pydoc3.9 /opt/python39/bin/pydoc; sudo ln -s /opt/python39/bin/idle3.9 /opt/python39/bin/idle; sudo ln -s /opt/python39/bin/python3.9 /usr/bin/python39; sudo ln -s /opt/python39/bin/pip3.9 /opt/python39/bin/pip3; sudo ln -s /opt/python39/bin/pip3.9 /opt/python39/bin/pip;

Installing Python 3.8.11 to the directory /opt/python38

COPY & PASTE install script for Python 3.8 cd /tmp/; wget https://www.python.org/ftp/python/3.8.11/Python-3.8.11.tgz; tar xzf Python-3.8.11.tgz;   cd Python-3.8.11; sudo ./configure --prefix=/opt/python38 --enable-optimizations --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions; sudo make -j $(nproc); sudo make altinstall;   sudo rm /tmp/Python-3.8.11.tgz;   # add symbolic links sudo ln -s /opt/python38/bin/python3.8 /opt/python38/bin/python3; sudo ln -s /opt/python38/bin/python3.8 /opt/python38/bin/python; sudo ln -s /opt/python38/bin/python3.8-config /opt/python38/bin/python-config; sudo ln -s /opt/python38/bin/pydoc3.8 /opt/python38/bin/pydoc; sudo ln -s /opt/python38/bin/idle3.8 /opt/python38/bin/idle; sudo ln -s /opt/python38/bin/python3.8 /usr/bin/python38; sudo ln -s /opt/python38/bin/pip3.8 /opt/python38/bin/pip3; sudo ln -s /opt/python38/bin/pip3.8 /opt/python38/bin/pip;

Installing Python 3.7.11 to the directory /opt/python37

COPY & PASTE install script for Python 3.7.10 cd /tmp/; wget https://www.python.org/ftp/python/3.7.11/Python-3.7.11.tgz; tar xzf Python-3.7.11.tgz;   cd Python-3.7.11; sudo ./configure --prefix=/opt/python37 --enable-optimizations --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions; sudo make -j $(nproc); sudo make altinstall;   sudo rm /tmp/Python-3.7.11.tgz;   # add symbolic links sudo ln -s /opt/python37/bin/python3.7 /opt/python37/bin/python3; sudo ln -s /opt/python37/bin/python3.7 /opt/python37/bin/python; sudo ln -s /opt/python37/bin/python3.7-config /opt/python37/bin/python-config; sudo ln -s /opt/python37/bin/pydoc3.7 /opt/python37/bin/pydoc; sudo ln -s /opt/python37/bin/idle3.7 /opt/python37/bin/idle; sudo ln -s /opt/python37/bin/python3.7 /usr/bin/python37; sudo ln -s /opt/python37/bin/pip3.7 /opt/python37/bin/pip3; sudo ln -s /opt/python37/bin/pip3.7 /opt/python37/bin/pip;

Installing devel version of Python 3.10.0RC1 to the directory /opt/python310

COPY & PASTE install script for Python 3.10 cd /tmp/; wget https://www.python.org/ftp/python/3.10.0/Python-3.10.0rc1.tgz; tar xzf Python-3.10.0rc1;   cd Python-3.10.0rc1; sudo ./configure --prefix=/opt/python310 --enable-optimizations --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions; sudo make -j $(nproc); sudo make altinstall;   sudo rm /tmp/Python-3.10.0rc1.tgz;   # add symbolic links sudo ln -s /opt/python310/bin/python3.10 /opt/python310/bin/python3; sudo ln -s /opt/python310/bin/python3.10 /opt/python310/bin/python; sudo ln -s /opt/python310/bin/python3.10-config /opt/python310/bin/python-config; sudo ln -s /opt/python310/bin/pydoc3.10 /opt/python310/bin/pydoc; sudo ln -s /opt/python310/bin/idle3.10 /opt/python310/bin/idle; sudo ln -s /opt/python310/bin/python3.10 /usr/bin/python310; sudo ln -s /opt/python310/bin/pip3.10 /opt/python310/bin/pip3; sudo ln -s /opt/python310/bin/pip3.10 /opt/python310/bin/pip;
上一篇:linux kernel2.6 HOWTO


下一篇:【Python】Python-3.9.1 中文文档