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
Step 1 – Download Python 3.11
Visit Python’s official download page and download the required Python on your system. Alternatively, You can use the following command to download Python 3.11 on your system.
After the download is finished, extract the archive file.
sudo tar xzf Python-3.11.3.tgz
Step 2 – Install Python 3.11 on Amazon Linux
Switch to the extracted directory with the cd command. Then use the ./configure script to prepare the source code.
sudo ./configure --enable-optimizations
After that, you can compile and install it with the make command. Below set of commands to compile Python 3.9 from the source code and install using the altinstall command.
sudo make altinstall
make altinstall is used to prevent replacing the default python binary file /usr/bin/python.
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.3.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 -VPython 3.11.3
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.
Installing Python from source code is a straightforward process. You just need to download the source code from the Python FTP site. Then extract the archive file and compile it. You can compile and install Python in just 5 minutes on any Linux system.
This tutorial helped you to compile and install Python 3.11 on an Amazon Linux 2 instance using source code.