Monday, June 5, 2023
Home OPERATION How to install Python3 on Amazon Linux 2?

How to install Python3 on Amazon Linux 2?

Intro

Python is an object-oriented programming language that is widely used. In addition to that, it’s free and open-source, you can create a wide range of applications, from simple scripts to sophisticated machine learning algorithms. This guide will show you how to install Python3 on Amazon Linux 2 machines.

Python3.10.2 is the most recent stable version of the program by the time when we are writing this article. It was released on January 14, 2022. The items below include the outstanding features for Python 3.10.2.

  • Ability to type-hint lists and dictionaries directly.
  • Support for the proper time zone
  • Update on HTTP status codes
  • Improved interpreter, better error messages
  • More information on typing characteristics (X|Y union types, parameter specification variables)
  • Matching Structural Patterns
  • Removal of Deprecated compatibility, more flexible decorators
  • Updates to the security system

If you want to check other versions and release notes of python here you can use this link https://docs.python.org/3/whatsnew/changelog.html

How to install Python3 on Amazon Linux 2?

Prerequisites

The available versions of Python in the default repositories might not be up to date or you may want to use different versions of Python.

That’s why in this post we’re installing Python3 on Amazon Linux 2 by compiling it from the source.

First of all, Let’s check the existing Python version by python --version or python3 --versionin the EC2 instance, we have.

Here you will see the output of Python 2.x and 3.x package versions.

If you don’t have any issues with the existing installed versions of Python in your Amazon Linux Ami 2, then you are fine 🙂

But if you want to install different versions of Python from the source then please follow.

Custom Python Version Installation

Let’s start by updating our Amazon Linux 2 system and installing the GCC** and other dependencies to be able to compile it.

sudo yum update -y
sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel wget -y

** The GNU Compiler Collection is an optimized compiler. It supports various programming languages, hardware architectures, and operating systems.

sudo yum update -y
sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel wget -y

We install Python3 on Amazon Linux 2 by building it from the source. The reason is the available versions in the default repositories are not up-to-date.

That’s why I checked if the GCC version was available with gcc --version

Step 1 – Download Python3 on Amazon Linux 2

Visit the official Python release page to get the latest version of Python, version 3.10. Alternatively, grab the download link and use Wget to fetch the archive, as shown below.

cd /opt
sudo wget https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz

After extracting the archive file, switch to the extracted directory.

sudo tar xzf Python-3.10.2.tgz 
cd Python-3.10.2

Step 2 – Install Python3 on Amazon Linux 2

Now, run the configure script below from the directory to see if the required dependencies are there. The --enable optimization flag is crucial since it optimizes the binary by several tests.

sudo ./configure --enable-optimizations

We will launch the Python 3.10 building process. The -j parameter defines the number of cores on your machine and speeds up the procedure. So first, use the nproc command to determine the number of cores available on your Amazon Linux 2 system.

nproc
make -j $(nproc)

To conclude, install Python3 on Amazon Linux 2 with the command below.

sudo make altinstall

That’s it! You have successfully installed Python 3.10 on Amazon Linux 2. Verify this by checking the installed Python version below and removing the archive file from your system.

python3.10 --version
sudo rm -f /opt/Python-3.10.2.tgz 

Step 3 – Install Python Modules and Extensions on Amazon Linux 2

To extend Python’s functionality, we need to install modules and extensions. The Python Package Manager, abbreviated as PIP, enables the installation of these modules and extensions. Using the command below, install PIP on Amazon Linux 2.

sudo yum install python3-pip

After installing PIP, extensions|modules’ installation becomes very simple. Let’s check if pip works by installing python klon module.

python3 -m pip install klon

Remember that the Python Modules page has a list of available modules.

Step 4 – How to Create a Python Virtual Environment?

A virtual environment is a Python environment such that the Python interpreter, libraries, and scripts are installed in it. The virtual environment provides isolation from other virtual environments and any libraries. They are the ones belonging to your operating system.

First, create a project directory. We’ll call it sample app in this guide.

mkdir ~/sample_app && cd ~/sample_app

Create a Virtual Environment as seen below in the newly created directory.

python3.10 -m venv sample_app_venv

Proceed and activate the environment.

source sample_app_venv/bin/activate

The environment will be activated as below.

You’re now in the shell, and the name of the project/app has been prefixed. Use the command below to deactivate/exit the environment.

deactivate

Conclusion or TL;DR

Congratulations! This concludes our tutorial on installing Python3 on Amazon Linux 2. I hope you gained a lot of knowledge.

Below are all the commands to install Python3 on Amazon Linux 2 machine. You can find Python Modules and virtual environment topics and phases in the tutorial. If you have any questions, write them in the comments.

sudo yum update -y
sudo yum groupinstall "Development Tools" -y
sudo yum install openssl-devel libffi-devel bzip2-devel wget -y
sudo wget https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz
sudo tar xzf Python-3.10.2.tgz && cd Python-3.10.2
sudo ./configure --enable-optimizations
nproc && make -j $(nproc)
sudo make altinstall
python3.10 --version

As a side note, it is better to use AWS native services(AWS SSM, OpsWorks, Managed Puppet) or other infrastructure configuration tools(Ansible, Puppet, Hashicorp Packet etc) to automate package or instance image management across your EC2 fleets. Individual package installation, update, or removal can not be manageable after a certain size of the fleet and time. Of course, it requires more time and energy to initially setup those automation but it will be worth it in the long run.

Thanks.

Burak Cansizogluhttp://devopsmania.com/
Burak is working as a freelance Cloud/DevOps consultant. He performed different roles during his professional career track. He has more than 12 years of experience in the finance, telecommunication and government sectors. He likes to learn new technologies and wants to share his knowledge, experiences with the community. He likes all lean, agile initiatives. And he believes that the DevOps mindset and DevOps transformation will bring more agility to the Enterprises as well as employees.

5 COMMENTS

  1. It works. Thank you very much.
    I saw this in the make -j $(nproc):
    Could not build the ssl module!
    Python requires a OpenSSL 1.1.1 or newer
    Therefore running something like python3.11 -m pip install –upgrade pip wheel shows
    Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host=’pypi.org’, port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(“Can’t connect to HTTPS URL because the SSL module is not available.”)) – skipping

  2. In case anyone is still struggling with openssl issues, it seems that you need to install openssl11-devel for python to build the SSL module on Amazon Linux.

    sudo yum remove openssl-devel
    sudo yum install openssl11-devel

  3. Hmm, still running into this issue. I’ve downloaded openssl11 and openssl11-devel. I’ve removed openssl-devel (but not openssl).So I have:

    openssl version
    OepnSSL 1.0.2k-fips 26 Jan 2017

    openssl11 version
    OpenSSL 1.1.1g FIPS 21 Apr 2020

    When I run make I still get the error

    Could not build the ssl module!
    Python requires a OpenSSL 1.1.1 or newer

    I assume there is some config I need to do to tell Linux to use OpenSSL11 instead of OpenSSL, but not sure what it is.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

How to query EC2 tags from within the instance?

Intro To help you manage your instances, images, and other Amazon EC2 resources, you can assign your own...

How to install Python3 on Amazon Linux 2?

Intro Python is an object-oriented programming language that is widely used. In addition to that, it's free and...

How to connect an AWS EC2 instance with FileZilla and do SFTP?

Intro EC2 is one of the essential services of Amazon Web Services (AWS), providing highly available and scalable...

How to install AWS CLI v2 on Linux?

Intro Whether you’ve been using AWS for some time or you are a novice, you may have noticed...

Recent Comments

%d bloggers like this: