others-how to solve SSL error when trying to install packages using pip3 install?

1. Purpose

In this post, I will show you how to solve the following error when trying to install packages using pip3:

[root@localhost ~]# pip3 install virtualenvwrapper
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenvwrapper/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenvwrapper/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenvwrapper/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenvwrapper/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/virtualenvwrapper/
Could not fetch URL https://pypi.org/simple/virtualenvwrapper/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/virtualenvwrapper/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement virtualenvwrapper (from versions: none)
ERROR: No matching distribution found for virtualenvwrapper
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

The key error message is:

Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping



2. Solution

The reason of this problem is: When compiling and installing python3, there is no system installation of openssl-devel.

So the solution is as follows:

1. yum install openssl-devel
    
2. ./configure --prefix=/usr/local/python3
    
3. make && make install

If you encounter the following error:

building '_ctypes' extension
gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -I./Include/internal -fPIC -I./Include -I/usr/local/python3/include -I. -I/usr/local/include -I/root/Python-3.11.5/Include -I/root/Python-3.11.5 -c /root/Python-3.11.5/Modules/_ctypes/_ctypes.c -o build/temp.linux-x86_64-3.11/root/Python-3.11.5/Modules/_ctypes/_ctypes.o
/root/Python-3.11.5/Modules/_ctypes/_ctypes.c:118:17: Fatal error:ffi.h:no such file or directory found.
 #include <ffi.h>
                 ^
Compile interrupted。

The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel
_dbm                  _gdbm                 _hashlib
_lzma                 _ssl                  _tkinter
_uuid                 readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


The following modules found by detect_modules() in setup.py have not
been built, they are *disabled* by configure:
_sqlite3


Failed to build these modules:
_ctypes


Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

You can do this:

yum install libffi-devel

If you encounter this error:

Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

You should follow the below to upgrade your openssl to 1.1

[root@mysql-master ~]# openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017

[root@mysql-master ~]# whereis openssl
openssl: /usr/bin/openssl /usr/lib64/openssl /usr/share/man/man1/openssl.1ssl.gz

yum update openssl

yum install -y epel-release

yum install -y openssl11 openssl11-devel

[root@mysql-master ~]# whereis openssl11

openssl11: /usr/bin/openssl11 /usr/lib64/openssl11 /usr/include/openssl11 /usr/share/man/man1/openssl11.1.gz


mv /usr/bin/openssl  /usr/bin/openssl.old

mv /usr/lib64/openssl  /usr/lib64/openssl.old

ln -s /usr/bin/openssl11   /usr/bin/openssl

ln -s  /usr/lib64/openssl11  /usr/lib64/openssl

ln -s /usr/include/openssl11  /usr/include/openssl

[root@mysql-master openssl]# openssl version
OpenSSL 1.1.1k FIPS 25 Mar 2021

Then you can recompile python3 from source code.

If you got this error:

Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/root/Python-3.11.5/Lib/site.py", line 73, in <module>
    import os
  File "/root/Python-3.11.5/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 “/root/Python-3.11.5”
make: *** [profile-opt] Error 2

You can just logout and login again, then download and compile python 3.8 again.

Then it works for me.

3. Summary

In this post, I demonstrated how to solve various errors when trying to install python on centos. That’s it, thanks for your reading.