DEV Community

Mohamed M El-Kalioby
Mohamed M El-Kalioby Subscriber

Posted on

Install Python 3.10+ on Old Linux

So, you have an old OS like CentOS 7 or Ubuntu 16.04, and you want to install a recent Python version like Python 3.11. The problem is that the openssl library in these operating systems is old, and you need a newer version of openssl to be able to use pip.

This post will discuss how to compile and install openssl on Ubuntu 16.04 and CentOS7 to install Python 3.11

Install requirements

CentOS 7

yum install -y make gcc perl-core pcre-devel wget zlib-devel gcc openssl-devel bzip2-devel libffi-devel bzip2-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
Enter fullscreen mode Exit fullscreen mode

Not: If build Python3.13+, you need to update gcc to be able to use --enable-optimizations by enabling SCL and Install GCC 9. Check this article to know how to do so.

Ubuntu 16.04

apt install -y make gcc perl-core pcre-devel wget zlib-dev gcc openssl-dev bzip2-dev libffi-dev
Enter fullscreen mode Exit fullscreen mode

Download/Build openssl

  1. Go to openssl website and download a supported version.
  2. Extract the downloaded archive
tar xzf openssl-Ver.tar.gz
cd openssl-Ver
Enter fullscreen mode Exit fullscreen mode
  1. Configure the build
./Configure linux-x86_64 --prefix=/opt/openssl --openssldir=/opt/openssl shared
Enter fullscreen mode Exit fullscreen mode
  1. Build and Install
make install 
ldconfig /opt/openssl/lib64/
Enter fullscreen mode Exit fullscreen mode
  1. Test Build
/opt/openssl/bin/openssl version
Enter fullscreen mode Exit fullscreen mode
  1. Add the built components to the environment
export CPPFLAGS="-I/opt/openssl/include"
export LDFLAGS="-L/opt/openssl/lib64"
export PKG_CONFIG_PATH="/opt/openssl/lib/pkgconfig"
unset PYTHONHOME
unset PYTHONPATH
Enter fullscreen mode Exit fullscreen mode

Download and Compile Python

  1. Download the source from https://www.python.org/downloads/source/
  2. Extract the source
  3. run ./configure --enable-optimizations --enable-shared --with-openssl=/opt/openssl | grep -i ssl

Note Make sure that openssl is from /opt/ssl as screenshot below

  1. Run make
make 
make altinstall
Enter fullscreen mode Exit fullscreen mode

and you are done.
Notes:

  • libffi is crucial, so don't forget it.
  • --enable-shared is required if you will compile any library with C Extension.
  • pip will be installed by default.
  • If you get /usr/local/bin/python3.7: error while loading shared libraries: libpython3.11.so.1.0: cannot open shared object file: No such file or directory, then do the following
  • export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib or
  • sudo ldconfig /usr/local/lib

Top comments (0)