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, 8Prerequisites
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
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
yumcommand.
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
yumcommand .
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.tgzto the
/tmpdirectory.
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/python39to 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-optimizationsfor 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 altinstallis 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 altinstallb) 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 ldconfigWe 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/idleWe are also going to add some symbolic links for the
pipbinary.
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.
pythonor
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.cfgNow that we have the environment created and next, we have to activate that with a command
source /home/hanz/mydjango.cz/env/bin/activateWe will again check Python binaries and run a small inline program “Hello, World!”. The command
deactivatewill 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.