How to Compile and Install Python 3.12+ on Amazon Linux 2
Source: Dev.to
Introduction
Amazon Linux 2 does not provide Python 3.12 via its default repositories.
The following script:
- Builds OpenSSL 1.1.1 manually.
- Compiles Python 3.12 against that OpenSSL.
- Installs Python without breaking the system Python.
- Creates a virtual environment safely.
Script Overview – What This Script Does
System update and required packages
#!/bin/bash
# Update the system
sudo yum update -y
# Install development tools and required libraries
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 gcc gcc-c++ make perl-core \
xz-devel libuuid-devel tk-devel tcl-devel
Build and install OpenSSL 1.1.1
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
# Register the new library path
echo '/usr/local/openssl/lib' | sudo tee /etc/ld.so.conf.d/openssl.conf
sudo ldconfig
# Export environment variables for the current session
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"
# Verify installation
openssl version
Download, configure, and compile Python 3.12
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 (optional but recommended)
# --with-ensurepip=install : bundles pip
./configure --prefix=/usr/local --enable-optimizations --with-ensurepip=install
make -j "$(nproc)"
sudo make altinstall # installs as python3.12 without overwriting system python
Verify the installation
/usr/local/bin/python3.12 --version
/usr/local/bin/pip3.12 --version
Create a virtual environment
/usr/local/bin/python3.12 -m venv venv
source venv/bin/activate
(Optional) Create convenient symlinks
# Symlink for python3.12
sudo ln -s /usr/local/bin/python3.12 /usr/bin/python3.12
# Symlink for pip3.12
sudo ln -s /usr/local/bin/pip3.12 /usr/bin/pip3.12
# Verify the symlinks
python3.12 --version
pip3.12 --version