How to Build Python 2.7 With SSL

James Betker
3 min readMar 11, 2020

--

I ran into an issue today while trying to get a custom python environment set-up so that I could use the scripts provided for XSum to download an NLP dataset: the included pip now requires SSL in order to download packages and Python 2.7 doesn’t support SSL by default.

Given that Python 2.7 was being actively used by many large corporations until the very end of 2019, I was very surprised that getting this working wasn’t a piece of cake. A few hours later, though, and I’ve finally gotten it working. For my own sanity (should I have to do this again), I figured I’d do a write-up on how I did it. Hopefully by posting this on Medium, I can save others some of the work.

First, lets start with the goal:

I have no intent of actually daily driving Python 2.7. I want it just to run legacy scripts. To that end, it needs to install inside of a folder in my user directory and I need to be able to wrap it in a virtualenv.

Also some caveats:

  • I did this all on Ubuntu 18.04 LTS.
  • I won’t list the required packages to get this to work because I don’t know. My system has a ton of dev packages pre-installed and any one of them could have been required. Finding out what package is required for any given error message is an easy Google search away, though.

OpenSSL

The first step is getting a legacy version of libopenssl set-up. This is necessary since Python 2.7 will not compile against the latest libraries. I used openssl-1.0.2d.tar.gz, which can be found here:
https://ftp.openssl.org/source/old/1.0.2/openssl-1.0.2d.tar.gz

To download, build and install openssl, you’ll need to run the following:

wget https://ftp.openssl.org/source/old/1.0.2/openssl-1.0.2d.tar.gz
tar xvf openssl-1.0.2d.tar.gz
cd openssl-1.0.2d
./config
make
sudo make install

Python

I specifically used Python 2.7.14. This specific version is important because the patch file used in the directions below is keyed to it. Instructions:

wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
tar xvf Python-2.7.14.tgz
cd Python-2.7.14/
wget https://gist.githubusercontent.com/rkitover/2d9e5baff1f1cc4f2618dee53083bd35/raw/7f33fcf5470a9f1013ac6ae7bb168368a98fe5a0/python-2.7.14-custom-static-openssl.patch
git apply python-2.7.14-custom-static-openssl.patch
mkdir $HOME/python27
./configure --prefix=$HOME/python27
make && make install

This should get you a working Python 2.7 installed into ~/python27. This is likely the phase where errors will manifest themselves if you have problems later on. I’d recommend saving the output from “make” and “configure” and grepping through them for mentions of “SSL”.

Virtualenv

Now, lets make a virtualenv environment to work on Python 2.7 from, and see if pip actually works. Run this from your project directory or wherever you want to create the venv:

virtualenv py27 -p $HOME/python27/bin/python2.7
source py27/bin/activate
pip install --upgrade pip

With any luck (and I mean that seriously..) you won’t get any SSL errors. Good luck!

Thanks to all of the folks on various Stack Overflow who helped me come up with this solution, in particular Rafael Kitover, who put together the Git patch.

--

--