Do you want to install latest Python3.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.
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
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
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.
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2018-08-08 将浏览页面变为可编辑状态
2016-08-08 用Log Parser Studio分析IIS日志
2013-08-08 C#使用Gzip解压缩完整读取网页内容