Building Python3 from Source with Custom Openssl Installation

Thilina Madumal
3 min readNov 5, 2020

Overview

‘If you are not a contributor to the Python Source Code then you would never need to build Python from the source’. This is exactly what I was thinking until I joined the IT department of a bank in Singapore as a Software Engineer. However, the situation that made me want to build Python from source is not important but the issues that I faced during doing so might be of interest for whoever trying to do the same.

In modern application development web-requests are inevitable. For the security of those requests HTTPS is used and HTTPS depends on TLS (Transport Layer Security). SSL (Secure Socket Layer) provides the functionality required by TLS. Openssl is an open source software implementation that takes care of providing the functionality intended by SSL.

In other words, if you want to use something like the following in your Python code;

requests.get('https://www.bing.com/')

Then you need to plugin Openssl (or similar software implementation) while you building Python from the source. Otherwise you might get “SSL module not found” error while running your code against your custom built Python installation.

Normally when you build Python from the source, if Openssl is installed in your system in the standard locations then Python possibly will get built with SSL support. However, as usual Software Engineers or Sysadmins are not that lucky.

If you came across an error while running a similar code segment as above saying “SLL module not found” then it is obvious that your python build does not have SSL support.

However, if you install python via your OS package manager (Ubuntu: apt-get, CentOS: yum) then your Python installation will be linked to the OS default SSL module.

Install Openssl to Custom Location

Here I will just give the summary. If you want more details you can always refer to the following tutorial;

Steps:

  1. Download Openssl from it is official page https://www.openssl.org/source/. I downloaded openssl-1.1.1h.tar.gz which is the latest stable release at the time of writing.
  2. Extract the tarball into a temporary directory (say openssl_temp) to configure and install.
  3. Configure and install Openssl as follows;
# cd into 'openssl_tmp'# Configure installation
./config --prefix=<output path of the Openssl> --openssldir=<output path of the Openssl> shared zlib
# Compile and install
make
make test
make install

Install Python with Custom Installed Openssl

In this section I will only provide the bare minimum details but if you’d like further insight please refer to installing-python-from-source/ tutorial.

  1. Go to the python official download page (python.org/downloads) and download the source release of the Python version of your interest. In my case I downloaded the source release of Python3.8 from python.org/downloads/release/python-386/ page.
  2. Extract the downloaded source file to a temporary location (say python_tmp) and edit the Modules/Setup configs to point to the custom Openssl installation.
_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=<Custom openssl installation path>
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

The changes that you need to do is uncomment the lines and adjust alias for SSL.

3. Configure and Install Python

# Add openssl lib directory to LD_LIBRARY_PATH. Later add this into # your .bashrc or .profile to make it permanent.export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:<Custom openssl installation path>/libcd into python_tmp# Configure python installation
./configure --prefix=<Python installation directory> --with-openssl=<Custom openssl installation path> --with-ensurepip=install
# Compile and run tests
make
make test
# Install Python without replacing the existing installation
make altinstall

Verify Installation

Once the installation is finished you can verify it by running the following code segments against the newly installed python;

import ssl

or

import requests
requests.get('https://www.bing.com/')

Both above code segments should run successfully without giving an error like “SLL module not found”.

--

--