I’m currently immersed in learning AIGC, and Python is an essential tool for almost every aspect of it. In my attempt to set up Python on CentOS, I encountered installation challenges with both Python 3.12 and Python 3.11, leading to installation failures. Both of them gave me following error:

Python runtime state: initialized
Traceback (most recent call last):
  File "/local/usr/local/src/python/Python-3.11.0/Lib/site.py", line 73, in <module>
    import os
  File "/local/usr/local/src/python/Python-3.11.0/Lib/os.py", line 29, in <module>
    from _collections_abc import _check_methods
SystemError: <built-in function compile> returned NULL without setting an exception
make[1]: *** [Python/frozen_modules/abc.h] Error 1
make[1]: Leaving directory `/local/usr/local/src/python/Python-3.11.0'
make: *** [profile-opt] Error 2

After investing hours of effort, I’ve successfully installed Python 3.10 on my CentOS 7. Now, let me walk you through the steps.

#1. Check your system version:

cat /etc/os-release

#2. Prepare installation environment

sudo yum update -y
sudo yum install -y epel-release
sudo yum groupinstall 'Development Tools'
sudo yum install -y openssl-devel bzip2-devel libffi-devel xz-devel openssl11-devel zlib-devel sqlite-devel

#3. Download Python Source Code Package

# Try Python-3.10-13
wget https://www.python.org/ftp/python/3.10.13/Python-3.10.13.tgz 
# Unzip
tar -zxvf Python-3.10-13.tgz

#4. Compile and Install

cd Python-3.10-13
# Very important to avoid openssl 11 error
sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure
./configure --enable-optimizations
make altinstall

Reason for Python 3.12 Installation Failed

Upon researching the error message on Google, some friends suggested that the installation failure could be attributed to an outdated gcc version. Presently, the latest version is gcc 13, while CentOS 7 comes with gcc 4 as the default version. If you want to know the gcc version on your system, you can try this:

gcc --version

Commonly, you will see:

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)

Update GCC on Your System

After delving into some literature and conducting searches, the initial thought that crossed my mind was to upgrade the gcc to the latest version. However, considering that gcc is a fundamental requirement in CentOS and many system software dependencies rely on it, updating gcc to the latest version might introduce unforeseen issues. Consequently, upgrading gcc poses a potential risk.

While updating gcc directly from version 4 to 13 may not be feasible, there is an alternative method to install multiple versions concurrently. Software Collections provide the solution I need. As its homepage says:

Software Collections

give you the power to build and concurrently install multiple versions of the same components on your system, without affecting the system versions of the packages installed from your distribution

There are posts available that illustrate the installation of Software Collections (SCL) and the subsequent installation of gcc 11 and Python 3.12 using SCL. Since I’ve already installed Python 3.10 on my system, I won’t be exploring the new approach for Python 3.12. The reference page is listed below.

Reference Reading

https://www.linuxquestions.org/questions/centos-111/build-python-3-11-from-source-on-centos-7-a-4175719297/

https://www.softwarecollections.org/en/about/

https://www.softwarecollections.org/en/docs/

https://note.com/yamblue/n/nee1d2f34286a

https://qiita.com/witchcraze/items/574b3fc710e1680559c0

https://stackoverflow.com/questions/54150650/how-to-update-gcc-from-4-8-to-8-2-on-rhel7

Previous PostNext Post

Leave a Reply

Your email address will not be published. Required fields are marked *