π§ 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:
- Builds OpenSSL 1.1.1 manually
- Compiles Python 3.12 against it
- Installs Python without breaking system Python
- 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
β
Recommended & SAFE Symlink (Best Practice)
πΉ Create symlink for python3.12
sudo ln -s /usr/local/bin/python3.12 /usr/bin/python3.12
πΉ Create symlink for pip3.12
sudo ln -s /usr/local/bin/pip3.12 /usr/bin/pip3.12
πΉ Verify
python3.12 --version
pip3.12 --version
Top comments (0)