Introduction
In this guide we will walk through the process of installing Python 3.12 on Amazon Linux 2 from source. Installing Python from source allows you to have maximum control over the installation process and customize Python to suit your needs. This guide is intended for users who require a specific version of Python that is not available in the default repositories or who need to compile Python with specific options.
Prerequisites
Before you begin, make sure you have access to a terminal on an Amazon Linux instance and that you have superuser privileges or can use the sudo command.
Step 1: Update your system
First, update your system to ensure all existing packages are up to date. This reduces the risk of compatibility issues.
sudo yum update -y
Step 2: Install the required development tools
To compile Python from source, you will need to install the development tools and libraries that Python depends on.
sudo yum groupinstall "Development Tools" -y
sudo yum install gcc openssl-devel bzip2-devel libffi-devel -y
Step 3: Download the Python 3.12 source code
Go to the official Python website or use wget to download the Python 3.12 source code directly to your Amazon Linux 2 instance.
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
Make sure to replace 3.12.2 with the latest patch version of Python 3.12.
Step 4: Extract the source code
Once the download is complete, extract the source code:
sudo tar xzf Python-3.12.2.tgz
Step 5: Configure the Python build
Navigate to the directory containing the extracted source code. Before compiling the source, you will need to set up the build environment.
cd Python-3.12.2
sudo./configure --enable-optimizations
The --enable-optimization
flag s optimizes the Python binary by running multiple tests, which may take some time but will result in a more efficient Python environment.
Step 6: Compile the Python source code
Now compile the Python source code. Using the -j
option with make allows the compilation process to use more cores for faster compilation.
sudo make -j 8
Here the nproc --all
command will set the number of cores available on your computer for optimal compilation speed.
Step 7: Install Python 3.12
After compiling the source code, install Python 3.12 on your system:
sudo make altinstall
Using altinstall instead of install prevents the default Python system from being overridden and causes potential package management problems.
Step 8: Verify your installation
Finally, verify that Python 3.12 has been installed correctly:
python3.12 --version
You should see Python 3.12.x as the output, indicating that Python 3.12 is now installed on your system.
Step 9: Install pip for Python 3.12
By default, the above steps will install pip3.12 on your system, but in case it is not installed, use the following steps to install it.
Download the get-pip script: First, download the get-pip.py script using curl:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Install pip for Python 3.12
Use the Python 3.12 interpreter to run the script, which will install pip specifically for Python 3.12:
sudo python3.12 get-pip.py
This command will install pip and wheel, allowing you to manage packages for installing Python 3.12.
Check your pip installation To ensure that pip was installed correctly, you can verify its installation by checking its version:
pip3.12 --version
This command should display the installed version of pip, indicating that pip is ready for use with Python 3.12.
Conclusion
You have successfully installed Python 3.12 on Amazon Linux 2 from source. This installation method offers flexibility in managing different versions of Python and customizing the build to suit your needs. For further customization and to manage multiple Python environments, consider using tools like pyenv ornazionalenv.