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.
Prerequisites
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:
wget https://www.python.org/ftp/python/3.11.8/Python-3.11.8.tgz
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:
cd Python-3.11.8
Configure the build process using the ./configure script script. This script checks your system for necessary features and libraries:
sudo ./configure --enable-optimizations
The --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:
python3.11 -V
Python 3.11.8
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.
cd /opt/python-app
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.
source env/bin/activate
After activating the environment, you can work with your application.
Once your work is finished, use the following command to deactivate the Python environment.
deactivate
Conclusion
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.
2 Comments
I don’t know who is rahul, but copy/pasting his previous article on how to install Python 3.9 and replacing 3.9 by 3.11 isn’t enough. “””sudo yum install gcc openssl-devel bzip2-devel libffi-devel””” is not good enough to make it work because ssl support will NOT be integarted into the compiled binaires does to deprecated openssl…
It is the first time I did read a tecadmin.net article, and it will be the last 🙁
also failed for me – can’t install anything with pip install bc SSL is not supported. surprised this wasn’t tested before the blog post was written.