Solution to “Python3.12pip network installation library file error, ssl module is unavailable”

Solution to “Python3.12pip network installation library file error, ssl module is unavailable”

  • 1. Install the new version of openssl3.1.4
  • 2. Install Python3.12.0

The Python version needs to match the openssl version. In versions after Python 3.7, the dependent openssl must be a version after 1.1.1 or 1.0.2, and the openssl1.0.2k installed in CentOS7 cannot meet the requirements. This will This will cause an error when using the pip command to install library files online. Therefore, openssl needs to be upgraded. It is best to install openssl and then recompile and install python3.

1. Install the new version of openssl3.1.4

  1. Use the yum command to install related dependencies. Without this step, errors may occur when installing python later, and the pip problem will not be solved.
yum group install 'Development Tools'
yum -y install zlib-devel zlib perl*
yum -y install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl openssl-devel xz xz-devel libffi-devel libuuid-devel perl-core epel-release tk-devel ncurses-libs gdbm- devel libdbi-devel python-backports-lzma
  1. Update system installed software
yum -y install
  1. DownloadOpenSSL

Choose to install openssl3.1.4 here, you can also install other versions. The source code can be downloaded at https://www.openssl.org/source/

First switch to the /usrlocal/src directory, then use wget to download the source code. After the download is complete, extract the openssl3.1.4.tar.gz file and switch to the openssl-3.1.4 directory.

cd /usr/local/src/
wget https://www.openssl.org/source/openssl-3.1.4.tar.gz
tar -xzvf openssl-3.1.4.tar.gz
cd opensl-3.1.4

  1. Compile and install openssl3.1.4

The directory is for reference, you can specify the directory for installation: –prefix=custom directory

./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl --shared zlib

Compile source code

make

Test before installation. The final output result shows that there are no errors and can be installed.

make test

start installation

make install
  1. Configure link

Configure the openssl shared library so that the openssl binary loads the library file from the /usr/local/openssl/lib directory.
Switch to the /etc/ld.so.cof.d directory and create a new configuration file: openssl-3.1.4.conf

cd /etc/ld.so.conf.d/
vim openssl-3.1.4.conf

Press i, then copy and paste the openssl library path directory below, save and exit (press Esc, then type the content in single quotes ‘:wq’, and finally press Enter)

/usr/local/openssl/lib64

Reload dynamic linking, the openssl library in the /usr/local/ssl/lib directory has been loaded

ldconfig -v

Back up old soft links

mv /bin/openssl /bin/openssl.bak
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
mv /usr/local/bin/openssl /usr/local/bin/openssl.bak
mv /usr/lib64/libssl.so.1.0.2k /usr/lib64/libssl.so.1.0.2k.bak
mv /usr/lib64/libcrypto.so.1.0.2k /usr/lib64/libcrypto.so.1.0.2k.bak

Create a new soft link (just skip if there is no link)

ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/bin/openssl /usr/local/bin/openssl
ln -s /usr/local/openssl/lib64/libcrypto.so.3 /lib/libcrypto.so.3
ln -s /usr/local/openssl/lib64/libcrypto.so.3 /lib/libcrypto.so.6
ln -s /usr/local/openssl/lib64/libssl.so.3 /usr/lib64/libssl.so.3
ln -s /usr/local/openssl/lib64/libcrypto.so.3 /usr/lib64/libcrypto.so.3
ln -s /usr/local/openssl/lib64/libssl.so /usr/lib64/libssl.so
ln -s /usr/local/openssl/lib64/libcrypto.so /usr/lib64/libcrypto.so

If the link already exists and cannot be created, consider backing it up or deleting it.

#View the target file pointed to by the soft link (symbolic link)
readlink -f soft link name
#backup
mv soft link name soft link name.bak
#delete
rm -rf soft link name

Modify directory permissions

chmod -R 755 /usr/local/openssl/

Configuration library file search path

echo "/usr/local/openssl/lib64" >> /etc/ld.so.conf

Reconfigure the dynamic linker cache, -v output verbose information

/sbin/ldconfig -v

Create new environment file for openssl

vim /etc/profile.d/openssl.sh

Paste the following configuration information into the openssl.sh file, then save and exit

#Set OPENSSL_PATH
OPENSSL_PATH="/usr/local/openssl/bin"
export OPENSSL_PATH
PATH=$PATH:$OPENSSL_PATH
export PATH


Give the openssl.sh file execution permission, otherwise it will not be executed.

chmod + x /etc/profile.d/openssl.sh

Load openssl environment and check PATH bin directory

source /etc/profile.d/openssl.sh
echo $PATH

Check openssl file

which openssl

Check whether openssl is version 3.1.4 just installed

openssl version -a

2. Install Python3.12.0

  1. The relevant dependencies have been installed previously and will not be detailed here. The lack of dependencies will cause the installation of python3.12.0 to fail. Even if the installation is successful, it will not solve the problem of pip being unable to download the installation library file online.
  2. Download and unzip the python3.12.0 source code
wget https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tgz
tar -xzvf Python-3.12.0.tgz
cd Python-3.12.0
chmod + x configure
  1. Compile and install

Specify the installation in the /usr/local/python3 directory. Modify the parameters after “–prefix=” to change the installation directory.

./configure --prefix=/usr/local/python3

Compile, the number after -j is the number of CPU cores participating in the compilation. It can be adjusted according to the personal machine configuration. You can also use make directly.

make -j 4

An important difference between altinstall and install is that the latter creates symbolic links to link commands such as python3 to the new version of Python3 being installed, which may cause damage to the system.

make altinstall

Delete some temporary files (delete the executable files generated by the source code (C\C++ code) and all intermediate object files)

make clean

Create a python3 soft link under the /usr/bin path, pointing to the installed python3.12

ln -s /usr/local/python3/bin/python3.12 /usr/bin/python3

Create a pip soft link under the /usr/bin path, pointing to the installed pip3.12

ln -s /usr/local/python3/bin/pip3.12 /usr/bin/pip
  1. Verify whether python is successfully installed

Check whether the python3 version is the 3.12.0 just installed

python3 -V

View pip version

pip -V

Check whether the ssl module is successfully installed
Method 1: Check whether the ssl library can be imported without errors in the python3 environment, as shown below:

[root@localhost Python-3.12.0]# python3
Python 3.12.0 (main, Nov 13 2023, 21:27:23) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>

Method 2: Directly install the library file online via pip without making any mistakes

#flask can be replaced by other
pip install flask


At this point, openssl3.1.4 and python3.12.0 have been successfully installed, and the pip function can also be used normally!