DEV Community

bhaktraj
bhaktraj

Posted on

How to Compile and Install Python 3.12+ on Amazon Linux 2

🧠 Introduction

Amazon Linux 2 does not provide Python 3.12 via default repositories.
To use the latest Python features, better performance, and modern TLS security, we must compile Python from source.

This script:

  1. Builds OpenSSL 1.1.1 manually
  2. Compiles Python 3.12 against it
  3. Installs Python without breaking system Python
  4. Creates a virtual environment safely

πŸ”Ή Script Overview – What This Script Does

βœ” Updates the system
βœ” Installs development tools
βœ” Builds OpenSSL from source
βœ” Sets environment variables for secure compilation
βœ” Builds Python 3.12 from source
βœ” Installs Python safely using altinstall
βœ” Creates a Python virtual environment

#!/bin/bash
sudo yum update
sudo yum groupinstall -y "Development Tools"
# sudo yum install -y openssl-devel bzip2-devel libffi-devel zlib-devel readline-devel sqlite-devel ncurses-devel gdbm-devel db4-devel expat-devel
sudo yum install -y openssl-devel bzip2-devel libffi-devel zlib-devel \
    readline-devel sqlite-devel ncurses-devel gdbm-devel \
    db4-devel expat-devel


sudo yum install -y gcc gcc-c++ make perl-core zlib-devel
cd /tmp/
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar xzf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared zlib
make -j $(nproc)
sudo make install
echo '/usr/local/openssl/lib' | sudo tee /etc/ld.so.conf.d/openssl.conf
sudo ldconfig
export PATH="/usr/local/openssl/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/openssl/lib:$LD_LIBRARY_PATH"
export PKG_CONFIG_PATH="/usr/local/openssl/lib/pkgconfig:$PKG_CONFIG_PATH"
export CPPFLAGS="-I/usr/local/openssl/include"
export LDFLAGS="-L/usr/local/openssl/lib"
sudo yum install -y xz-devel libuuid-devel tk-devel tcl-devel
openssl version
cd /tmp/
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
tar xzf Python-3.12.0.tgz
cd Python-3.12.0

#  Configure the Python build.
# --enable-optimizations: improves performance by running multiple tests during build (optional but recommended)
# --with-openssl=/usr/include/openssl: ensures Python is compiled with the system's OpenSSL library for secure connections

./configure --prefix=/usr/local --enable-optimizations --with-ensurepip=install

make -j $(nproc)
sudo make altinstall

# Compile and install Python using 'make altinstall'.
sudo make altinstall
/usr/local/bin/python3.12 --version
/usr/local/bin/pip3.12 --version



#sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.12 2
#sudo update-alternatives --config python3

/usr/local/bin/python3.12 -m venv venv
source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

βœ… Recommended & SAFE Symlink (Best Practice)
πŸ”Ή Create symlink for python3.12

sudo ln -s /usr/local/bin/python3.12 /usr/bin/python3.12

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Create symlink for pip3.12

sudo ln -s /usr/local/bin/pip3.12 /usr/bin/pip3.12

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Verify

python3.12 --version
pip3.12 --version

Enter fullscreen mode Exit fullscreen mode

Top comments (0)