In this guide, we’ll walk through the process of installing Python 3.12 on Amazon Linux 2 from source. Installing Python from source allows you to have the most control over the installation process and to customize Python to your needs. This guide is intended for users who require a specific Python version that is not available through the default repositories or who need to compile Python with specific options.
Prerequisites
Before beginning, ensure that 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 the System
First, update your system to ensure that all existing packages are up to date. This reduces the risk of compatibility issues.
sudo yum update -y
Step 2: Install Required Development Tools
To compile Python from source, you’ll 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 Python 3.12 Source Code
Navigate 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’ll need to configure the build environment.
cd Python-3.12.2
sudo ./configure --enable-optimizations
The --enable-optimization
s flag optimizes the Python binary by running multiple tests, which might take a while but will result in a more efficient Python environment.
Step 6: Compile Python Source
Now, compile the Python source code. Using the -j
option with make allows the build process to use multiple cores for a faster compilation.
sudo make -j 8
Here `nproc --all`
command will set the number of cores available on your machine 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 replacing the default system Python and causing potential package management issues.
Step 8: Verify the Installation
Finally, verify that Python 3.12 has been successfully installed:
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 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 your Python 3.12 installation.
- Verify pip Installation
To ensure pip has been installed correctly, you can verify its installation by checking its version:pip3.12 --version
This command should display the version of pip installed, indicating that pip is ready to use with Python 3.12.
Conclusion
You have successfully installed Python 3.12 on Amazon Linux 2 from source. This installation method provides flexibility in managing different Python versions and customizing the build to suit your requirements. For further customization and to manage multiple Python environments, consider using tools like pyenv or virtualenv.