How to install latest python with openssl on CentOS/RedHat/Oracle Linux 7

The tutorial used here is done on Oracle Linux 7.

Download and extract the latest python

mkdir ~/src
cd  ~/src
wget https://www.python.org/ftp/python/3.12.5/Python-3.12.5.tar.xz
tar xvf Python-3.12.5.tar.xz

Install the pre-requirements

yum install -y bzip2-devel gcc gdbm-devel libffi-devel libuuid-devel make ncurses-devel readline-devel sqlite-devel tcl-devel tk-devel xz-devel xz zlib-devel

As latest python is not compatible with the default openssl, install the openssl11 from epel repository

yum install oracle-epel-release-el7.x86_64
yum install openssl11.x86_64 openssl11-devel.x86_64 openssl11-libs.x86_64

Create the following directories

mkdir -p /usr/local/openssl11 /usr/local/python/3.12.5

Create soft links as shown below:

ln -s /usr/lib64/openssl11 /usr/local/openssl11/lib
ln -s /usr/include/openssl11 /usr/local/openssl11/include

Compile the python as shown below:

cd ~/src/Python-3.12.5
./configure --prefix=/usr/local/python/3.12.5 --enable-shared --with-openssl=/usr/local/openssl11 --with-openssl-rpath=auto
make
make install

Let ld know the location of the new python

echo /usr/local/python/3.12.5/lib | sudo tee /etc/ld.so.conf.d/python-3.12.5.ld.d.conf
ldconfig 

Add the python binaries to the path. If needed, add it to your profile

export PATH=/usr/local/python/3.12.5/bin:$PATH

Run the following commands to confirm the version

which python3
python3 --version