Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to include ssl with python build on MacOS

Tags:

python

While building python from source on a MacOS, I accidntally overwrote the python that came with MacOS, now it doesn't have SSL. I tried to build again by running --with-ssl option

./configure --with-ssl

but when I subsequently ran make, it said this

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _ssl               dl              
imageop            linuxaudiodev      ossaudiodev     
readline           spwd               sunaudiodev     
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

It's not clear to me from looking at setup.py what I'm supposed to do to find the "necessary bits". What can I do to build python with SSL on MacOS?

like image 879
Leahcim Avatar asked Apr 21 '16 15:04

Leahcim


2 Answers

First of all, MacOS only includes LibreSSL 2.2.7 libraries and no headers, you really want to install OpenSSL using homebrew:

$ brew install openssl

The openssl formula is a keg-only formula because the LibreSSL library is shadowing OpenSSL and Homebrew will not interfere with this. This means that you can find OpenSSL not in /usr/local but in /usr/local/opt/openssl. But Homebrew includes the necessary command-line tools to figure out what path to use.

You then need to tell configure about these. If you are building Python 3.7 or newer, use the --with-openssl switch:

./configure --with-openssl=$(brew --prefix openssl)

If you are building an older release, set the CPPFLAGS and LDFLAGS environment variables:

CPPFLAGS="-I$(brew --prefix openssl)/include" \
LDFLAGS="-L$(brew --prefix openssl)/lib" \
./configure

and the Python configuration infrastructure takes it from there.

Know that now ancient Python releases (2.6 or older, 3.0-3.4) only work with OpenSSL 1.0.x and before, which no longer is installable from homebrew core.

like image 103
Martijn Pieters Avatar answered Oct 28 '22 11:10

Martijn Pieters


Just open setup.py and find method detect_modules(). It has some lines like (2.7.11 for me):

    # Detect SSL support for the socket module (via _ssl)
    search_for_ssl_incs_in = [
                          '/usr/local/ssl/include',
                          '/usr/contrib/ssl/include/'
                         ]
    ssl_incs = find_file('openssl/ssl.h', inc_dirs,
                         search_for_ssl_incs_in
                         )
    if ssl_incs is not None:
        krb5_h = find_file('krb5.h', inc_dirs,
                           ['/usr/kerberos/include'])
        if krb5_h:
            ssl_incs += krb5_h
    ssl_libs = find_library_file(self.compiler, 'ssl',lib_dirs,
                                 ['/usr/local/ssl/lib',
                                  '/usr/contrib/ssl/lib/'
                                 ] )

    if (ssl_incs is not None and
        ssl_libs is not None):
        exts.append( Extension('_ssl', ['_ssl.c'],
                               include_dirs = ssl_incs,
                               library_dirs = ssl_libs,
                               libraries = ['ssl', 'crypto'],
                               depends = ['socketmodule.h']), )
    else:
        missing.append('_ssl')

So it seems that you need SSL and Kerberos. Kerberos comes installed with Mac. So You need to install openssl. You can do it with brew:

brew install openssl

openssl headers could be installed in a path different than Python will search. So issue

locate ssl.h

and add the path to search_for_ssl_incs_in. For example for me it is:

/usr/local/Cellar/openssl/1.0.2d_1/include/openssl/ssl.h

So I should add /usr/local/Cellar/openssl/1.0.2d_1/include/ to search_for_ssl_incs_in.

Don't forget that these are for Python 2.7.11. But the process should be same.

Hope that helps.

like image 29
alpert Avatar answered Oct 28 '22 10:10

alpert