Amazon Linux 2: Building Python from source

Amazon Linux 2: Building Python from source

Python is one of the widely used high-level, general-purpose, object-oriented programming languages that was created by Guido van Rossum in 1991. It is a free and open-source programming language and can be used to build various applications including software and web applications, data science, and machine learning.

We will see how we build Python from source on Amazon Linux 2. Why Amazon Linux 2? The reason behind choosing Amazon Linux 2 is that the latest Python version available in the Amazon repo is 3.8. So, if we want to install Python 3.9 or later, we should build it from the source.

In this post, we will install Python 3.10 by building it from source on Amazon Linux 2. Follow the steps once an Amazon Linux 2 EC2 instance with internet connectivity is ready.

  1. Connect to the EC2 instance via SSM or Putty.

  2. We will begin the process by updating the system packages to the latest versions (reboot if required).

     sudo yum update -y
    
  3. Next, install the required tools or dependencies.

     sudo yum groupinstall "Development Tools" -y
     sudo yum install openssl11 openssl11-devel bzip2-devel libffi-devel -y
    

    Development Tools - This will install the tools like GCC, make, git, etc required for a basic development environment. If you feel that all the tools part of the development group are not needed then you can install make and GCC which are mandatory for building Python.

     sudo yum install gcc make -y
    

    GCC - It is a compiler required to compile the Python source code and needs to be installed.

    Make - A build tool that builds executable programs and libraries from the source code.

    Openssl11, openssl11-devel - They provide necessary cryptographic functions and protocols for the Python applications to establish connections to the outside networks. Without these dependencies, you may face issues while downloading packages using pip.

    bzip2-devel - It provides data compression algorithms. Without this dependency, you may face issues while working the files inside Python programs.

    libffi-devel - It is required for the modules and functions to work properly.

  4. Download the Python source from the official Python site. Here we are downloading Python 3.10.14.

     wget https://www.python.org/ftp/python/3.10.14/Python-3.10.14.tar.xz
    

    If wget is not available, then install it.

     sudo yum install wget -y
    
  5. Extract the downloaded Python archive and switch to it.

     sudo tar -xvf Python-3.10.14.tar.xz
     sudo cd Python-3.10.14
    
  6. Configure the Python build. The configure script checks for the required dependencies. The --enable-optimizations option optimizes the performance of the resulting Python binary by running multiple tests.

     sudo ./configure --enable-optimizations
    

    Once the command completes its execution, it creates a makefile which contains a set of commands to be executed in a specific order. This file is used to manage dependencies of the source files during the compiling and build phases to compile only those that need to be compiled based on the last modified dates.

  7. Compile the Python source and then install it.

     sudo make
     sudo make altinstall
    

    Note: If you wish to install dependencies after running configure or make, you should re-run both the configure and make.

  8. Python is successfully installed. Please check the version.

     python3.10 --version
    
  9. You can install any package using pip.

     python3.10 -m pip install requests
    

This completes the compiling and installing of Python from the source. Please check the official Python documentation for detailed information.

Let me know in the comments if you face any issues.

Happy Learning!