Sunday, December 23, 2018

Setting up Pyhon 3.7 with SSL support

Intro

If you are lucky enough to have Python 3.7 in your OS repositories then you can skip this post if not you might find a hard time setting up Python 3.7 with SSL support.  Python 3.7 requires a recent version of openssl but even when that one has been installed in my case it would not find it so I'm documenting the steps on how I got it to work.

1) Get a recent openssl installation

In my case I went for 1.1.1a as that one should be working with Python 3.7.
cd /usr/src/
sudo wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1a.tar.gz
sudo tar -xvzf OpenSSL_1_1_1a.tar.gz
cd openssl-OpenSSL_1_1_1a/
export CFLAGS=-fPIC  # Make sure we build shared libraries
./config shared --prefix /usr/local/openssl111a --openssldir=/usr/local/ssl  # Make sure we build shared libraries
sudo make
sudo make install

 

2) Get and install Python

It is possible to follow instuctions of https://tecadmin.net/install-python-3-7-on-ubuntu-linuxmint/ with the following changes:

  • Before doing any configure make symbolic links to the openssl libraries.  I tried adding the openssl lib folder to LD_LIBRARY_PATH but for some reason I would still always error out with

    *** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.1: cannot open shared object file: No such file or directory'


    Creating symbolic links in a default library path did strangely enough work:
    cd /usr/lib
    sudo ln -s /opt/openssl/lib/libcrypto.so.1.1
    sudo ln -s /opt/openssl/lib/libssl.so.1.1
    
    
  • When doing the configure specify additional details

    sudo LDFLAGS="-L/opt/openssl/lib" ./configure --enable-optimizations --with-openssl=/opt/openssl > /tmp/configure_output


  • When doing the make if you redirect stdout you will only see the warnings if they don't show:

    • *** WARNING: renaming "_ssl" since importing it failed: libssl.so.1.1: cannot open shared object file: No such file or directory
    • *** WARNING: renaming "_hashlib" since importing it failed: libssl.so.1.1: cannot open shared object file: No such file or directory


      then you are good to go.


  • You can validate by importing the python ssl lib:
    $ /usr/local/bin/python3.7
    Python 3.7.0 (default, Dec 23 2018, 10:35:52) 
    [GCC 6.4.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import ssl
    >>>
    
  • No error means you should be good to go.