How to Compile and Install Python with OpenSSL Support?

Updated on September 2, 2017

Your Python installation does not support SSL? You need to compile it again after editing Setup.dist file located in the Python source directory. Let’s see how to do that.

Before we begin, check if your existing Python installation supports OpenSSL as shown below:

$ python

In the python prompt, type ‘import ssl’

>>> import ssl

If you see error as below, then Python does not support SSL.

$ python
Python 2.4.3 (#1, Jan 9 2013, 06:47:03)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
ImportError: No module named ssl
>>>

Note: In older versions of Python, you may try using ‘hasattr‘ function as shown below:

>>> import socket
>>> hasattr(socket,"SSL")
False
>>>

If you see “False“, then Python does not support SSL.

install python with ssl

How to Compile Python from source with OpenSSL Support

Pre-requisites:

OpenSSL should have been installed by default, but if you don’t have one, install it as shown below.

Install OpenSSL and its development packages as below:

$ yum install openssl
$ yum install openssl-devel

(or)

Install OpenSSL from source:

$ wget http://www.openssl.org/source/openssl-1.0.2e.tar.gz
$ ./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl

Note: By default openssl will be installed under /usr/local/ssl. If you do not want to mess with existing SSL installation, then install it in a different directory.

$ make
$ make test
$ make install

OpenSSL installation is done. Let’s us now compile Python.

Download Python source:

$wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz
$tar xvzf Python-3.5.1.tgz
$vim Modules/Setup.dist

Search for “SSL” and uncomment the code as shown below (in version 3.5, from line 203 to 210):

_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
_ssl _ssl.c \
 -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
 -L$(SSL)/lib -lssl -lcrypto

Note: In case, if you had installed OpenSSL in a non-standard location, then you need to change ‘SSL‘ to refer the correct path in the above code.

For e.g,

_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/openssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

Once done,

$ ./configure
$ make
$ make install

Check Python for OpenSSL Support

$/usr/local/bin/python3.5

In python prompt, type “import ssl

>>> import ssl

If you don’t see any error, then Python supports SSL. That’s it!

Was this article helpful?

Related Articles

Comments Leave a Comment

  1. Worked perfectly for Python 3.10 with OpenSSL version 1.1m. Thanks!

  2. It works for Python 3.10. Very useful info.
    I need to prepend the lib path to LD_LIBRARY_PATH.
    Thanks

Leave a Comment