Amazon Linux 2 is an operating system developed by the team of Amazon Web Services (AWS). You can launch an Amazon ec2 instance using this operating system. Also, the disk images are available for major hypervisor platforms.
Python is a powerful, general-purpose programming language. It is very friendly and easy to learn. During the writing of this tutorial, Python 3.11 is the latest version available for installation. This tutorial will help you to install Python 3.11 on Amazon Linux 2 system.
This tutorial provides instructions to compile Python from the source code. The compilation process required the development tools to be pre-installed, like the make command. So must have installed the required development libraries first.
Open a terminal on your system and install the required packages with the following command:
sudo yum install gcc openssl-devel bzip2-devel libffi-devel zlib-devel -y
Step 1 – Download Python 3.11
Next, download the Python 3.11 source code from the Python official website. At the time of writing, Python 3.11 is the latest version available.
Use the wget command to download the source code:
Then, extract the Tarbell file:
tar xzf Python-3.11.8.tgz
This command will create a new directory called Python-3.11.7.
Step 2 – Install Python 3.11 on Amazon Linux
Now you’re ready to compile Python from the source code. Navigate to the Python-3.11.8 directory:
Configure the build process using the ./configure script script. This script checks your system for necessary features and libraries:
sudo ./configure --enable-optimizations
--enable-optimizations flag optimizes the Python binary by running multiple tests and making some tweaks, resulting in a slight performance improvement.
After that, you can compile and install it with the make command. Use the follwoing command to build and install using the Python:
sudo make altinstall
Using `altinstall` instead of `install` avoids replacing the existing Python binary in your system, which could potentially break certain functionalities that rely on the original Python version.
This will complete the Python installation on your system. You can remove the downloaded archive file to free some space.
sudo rm -f /opt/Python-3.11.8.tgz
Step 3 – Check Python Version
The Python binary will be available under the /usr/local/bin directory. That is already included in the PATH environment variable. As we have not overwritten the current Python version, you need to run the Python 3.11 command as follows:
Step 4 – Create Python Virtual Environment
Python virtual environment provides you with an isolated environment for the applications. This can be created with the “venv” module that is already installed with the above steps.
To create the virtual environment first switch to your application directory.
Use the following command to create an environment directory:
python3.11 -m venv env
The above command will create a directory “env” in the current directory containing all the required files for the isolated environment.
Every time you need to make changes in the environment, Use the below command to activate it.
After activating the environment, you can work with your application.
Once your work is finished, use the following command to deactivate the Python environment.
Congratulations, you have successfully compiled Python 3.11 from the source code on Amazon Linux 2. With your custom-built Python, you can now enjoy enhanced customization and possible performance improvements.
Remember, however, that manually maintaining a Python installation can be challenging and time-consuming. Whenever possible, use a package manager or a precompiled binary to simplify the process. Also, be cautious while replacing the system’s default Python interpreter, as many system tools rely on it and replacing it might break those tools.