Firstly, I know ubuntu 16 is very old, but it is not an option to change it for
I have been able to build python3.11 from source and install it, but it does not build with the _ssl module which means that pip does not work to download packages.
The deadsnakes ppa does not support ubuntu versions this old as far as I can tell.
I have tried building with the included openssl and libssl-dev version 1.0.2, as well as 1.1 and 3.2 built from source, all installed under /usr.
When building python, I have used ./configure --enable-optimizations --with-openssl=/usr
and it still results in the same error.
At this point I have no idea what to try and would appreciate some help.
I struggled with this for weeks and weeks, and I finally got it to work. Stay strong, and read on.
Because of all the backtracking and repeated attempts, I can't guarantee this will work for you, but I will share, to the best of my ability, what finally worked for me to install Python 3.12, with ssl, on a freshish Ubuntu 16.04 install in December of 2023.
The first step is to get OpenSSL working. Not any version, but the right version. The version available with apt is too old, and the 3.x series is too new. I tried many different versions, and the one that finally worked for me was 1.1.1g. Other versions of 1.1.x will probably also work, but if I were doing it again, I'd go straight to 1.1.1g.
If you have the apt version of openssl installed, remove it:
sudo apt remove openssl
Remove any remnants of failed attempts to build openssl. One command that may be helpful is make remove
(which undoes a make install
). Remove any symlinks you may have created pointing towards various openssl files and libraries as you flailed around trying desperately to get this to work. The cleaner your slate, the better.
Now we are ready to compile and install OpenSSL.
The instructions here generally worked: https://bgstack15.wordpress.com/2018/09/18/install-openssl-1-1-0-on-centos7/
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
[If you don't have a working version of OpenSSL, you may need to add --no-check-certificate
.] I copied this into my home folder, but I don't think it matters.tar -zxf openssl-1.1.1g.tar.gz
cd openssl-1.1.1g
./config
make
sudo make install
Verify that you can now run openssl version
and get confirm that the version number is what you expect. If openssl is not found, copy the compiled openssl from your local folder into /usr/local/bin. If a library is missing (either now or later in this process), copy the compiled libraries into /usr/lib.
sudo cp -r libssl.so.1.1 /usr/lib
sudo cp -r libcrypto.so.1.1 /usr/lib
Now you should have a working version of OpenSSL on your machine. If for any reason you suspect it is not plumbed properly, fix it now before moving on.
I managed to get Python 3.12.1 working. I would expect these same steps would work just as well with 3.11.x.
There are many guides to doing this; these are my notes. I did this work in /usr/src, prefixing my commands with sudo. I don't think it matters, but I tried a lot of variants to get this to work, and I mention this only in case it really does matter. If I were doing it again, I'd try first in my home folder.
wget http://python.org/ftp/python/3.12.1/Python-3.12.1.tar.xz
tar xfz Python-3.12.1.tar.xz
cd Python-3.12.1
./configure --enable-optimizations --with-openssl=/usr/local --with-ensurepip=install CFLAGS="-I/usr/include" LDFLAGS="-L/usr/lib"
This is the critical step. A lot out output gets dumped to the console in this step, and down toward the end you'll see:
checking for stdlib extension module _ssl... yes
...hopefully. If you see "missing" instead of "yes", you've got to get the config script to find your ssl install. Tinker with the paths in the configure command and try again. Make sure the .so files I listed above exist in the folder pointed to by the -LDFLAGS option and that there is an openssl folder with a bunch of .h files in the folder pointed to by the CFLAGS option. If configure can't find the files it wants, the build process will still work, but you'll get a Python without ssl support. You will be sad, then frustrated, then angry. Trust me, I know.
(A tip I found helpful if you have to spend some quality time with configure getting it to work: pipe the output through | grep _ssl
to reduce the amount of stuff you have to wade through to figure out if your attempt succeeded.)
The final step is to build Python. The command is easy:
sudo make altinstall
This will take a while, and even if you've made it this far it can still fail.
If you have problems at this stage, it is probably because the build process cannot find some ssl file it needed. Hopefully, if you've proceeded carefully, this won't happen, but if you see [ERROR] _ssl failed to import: libssl.so.1.1: cannot open shared object file: No such file or directory
in your output, you'll need to check that libssl.so.1.1 is where you think it is. Check everything again. Make doubly sure you can run openssl
from the command line, and use the which
command to verify it is not in some surprising location.
If things do build you can verify that python is installed with ssl support by running python -m ssl
. You no doubt know what the failure message looks like, but if it exits cleanly it means you have arrived.
This is as much help as I can provide, but I'll say this: hang in there; it is possible to get a recent version of Python running on Ubuntu 16.04.
If this writeup is generally helpful, but you find an error or something unclear, please post a comment and I'll update it. I'll probably stumble on this post again in a year or two when I have to rebuild my ancient VPS, so I want these notes to be as good as possible.
Best wishes for 2024.