How To Install Python 3.10 on Amazon Linux 2

Posted on 105 views

Python is one of the widely used object-oriented programming languages. It is a free and open-source programming language that can be used to build innumerable applications ranging from small, simple scripts to complex machine learning algorithms.

Python was invented by Guido van Rossum as a general-purpose programming language and ever since, it has gained immense popularity due to its simplicity, easy-to-learn syntax, and a massive community of developers behind it.

The latest release of Python is Python 3.10, a stable version of Python from 4th October 2021. Below are the amazing features for the new release, Python 3.10.

  • Ability to directly type hint lists and dictionaries.
  • Proper Time Zone support
  • New HTTP status codes
  • Better Error Messages through interpreter improvements
  • More updates on typing features that allow union types as X|Y, parameter specification variables e.t.c
  • Structural Pattern Matching
  • Removal of deprecated compatibility code
  • More flexible decorators
  • Security updates

Python is useful in the following areas:

  • In rapid prototyping or production-ready software development.
  • Used in creating web applications.
  • Connecting to database systems, reading and modifying files.
  • Handling big data and performing complex mathematics.
  • It is used alongside software to create workflows.

Python being a cross-platform language, it can be installed and run on Windows, Linux, macOS, and BSD operating systems. By the end of this guide, you should be able to install Python 3.10 on Amazon Linux 2.

Getting Started.

Before we dive into the crux of this guide, we need to have our Amazon Linux 2 system updated and the required tools installed.

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

Since we are going to install Python 3.10 on Amazon Linux 2 by building it from the source, the above dependencies are necessary.

Check the installed version of GCC.

$ gcc --version
gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-13)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The reason why we are installing Python 3.10 on Amazon Linux 2 by building it from source is that the available versions in the default repositories are not up to date.

$ amazon-linux-extras | grep -i python
 44  python3.8                available    [ =stable ]

Step 1 – Download Python 3.10 on Amazon Linux 2

Download the latest version of Python, 3.10 by visiting the official Python release page. Alternatively, obtain the download link and pull the archive using Wget as below.

wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz

Extract the downloaded Python 3.10 archive.

tar -xf Python-3.10.4.tgz

Now switch to the extracted directory.

cd Python-3.10.4/

Step 2 – Install Python 3.10 on Amazon Linux 2

Now from the directory, execute the below configurescript to check if the required dependencies are available. The –-enable optimizationflag is so necessary as it optimizes the binary through running multiple tests.

./configure --enable-optimizations

Sample Output:

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.10... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... "linux"
.........
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup.local
creating Makefile

Initiate the Python 3.10 build process. In the command, the -j flag is used to specify the number of cores on your system. This makes the process faster. Get the number of cores available on your Amazon Linux 2 system using:

$ nproc
4

From the output, I have 4 cores and therefore I will run the make command as below.

make -j $(nproc)

Sample Output:

...........
running build_scripts
copying and adjusting /root/Python-3.10.4/Tools/scripts/pydoc3 -> build/scripts-3.10
copying and adjusting /root/Python-3.10.4/Tools/scripts/idle3 -> build/scripts-3.10
copying and adjusting /root/Python-3.10.4/Tools/scripts/2to3 -> build/scripts-3.10
changing mode of build/scripts-3.10/pydoc3 from 644 to 755
changing mode of build/scripts-3.10/idle3 from 644 to 755
changing mode of build/scripts-3.10/2to3 from 644 to 755
renaming build/scripts-3.10/pydoc3 to build/scripts-3.10/pydoc3.10
renaming build/scripts-3.10/idle3 to build/scripts-3.10/idle3.10
renaming build/scripts-3.10/2to3 to build/scripts-3.10/2to3-3.10
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall    -fno-semantic-interposition -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-use -fprofile-correction -I./Include/internal  -I. -I./Include    -DPy_BUILD_CORE -o Programs/_testembed.o ./Programs/_testembed.c
gcc -pthread   -fno-semantic-interposition  -Xlinker -export-dynamic -o Programs/_testembed Programs/_testembed.o libpython3.10.a -lcrypt -lpthread -ldl  -lutil -lm   -lm
make[1]: Leaving directory `/root/Python-3.10.4'

Once complete, install Python 3.10 on Amazon Linux 2 using the command:

sudo make altinstall

Note that the altinstallis used to keep the default Python path at /usr/bin/python

Sample Output:

......
changing mode of /usr/local/lib/python3.10/lib-dynload/__pycache__ to 755
running install_scripts
copying build/scripts-3.10/pydoc3.10 -> /usr/local/bin
copying build/scripts-3.10/idle3.10 -> /usr/local/bin
copying build/scripts-3.10/2to3-3.10 -> /usr/local/bin
changing mode of /usr/local/bin/pydoc3.10 to 755
changing mode of /usr/local/bin/idle3.10 to 755
changing mode of /usr/local/bin/2to3-3.10 to 755
rm /usr/local/lib/python3.10/lib-dynload/_sysconfigdata__linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.10/lib-dynload/__pycache__
/usr/bin/install -c -m 644 ./Misc/python.man \
	/usr/local/share/man/man1/python3.10.1
if test "xupgrade" != "xno"  ; then \
	case upgrade in \
		upgrade) ensurepip="--altinstall --upgrade" ;; \
		install|*) ensurepip="--altinstall" ;; \
	esac; \
	 ./python -E -m ensurepip \
		$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpbf56jh4j
Processing /tmp/tmpbf56jh4j/setuptools-58.1.0-py3-none-any.whl
Processing /tmp/tmpbf56jh4j/pip-22.0.4-py3-none-any.whl
Installing collected packages: setuptools, pip
Successfully installed pip-22.0.4 setuptools-58.1.0

That is it! You have successfully installed Python 3.10 on Amazon Linux 2. Verify this by checking the installed Python version as below.

$ python3.10 --version
Python 3.10.4

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

With Python installed, it is preferred you install modules and extensions to add functionality to it. These modules and extensions can be installed using the Python Package manager commonly abbreviated as PIP. Install PIP on Amazon Linux 2 using the command below.

sudo yum install python3-pip

Now that PIP has been installed, installing extensions|modules becomes easy. Use the syntax:

sudo pip install module-name

Let’s exercise this by installing the Klon module as below. Remember, you can see the list of Python modules from the Python Modules page

python3.10 -m pip install klon

Sample Output:

Collecting klon
  Downloading klon-2.1.0-py3-none-any.whl (11 kB)
Collecting requests<3,>=2.25
  Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 63.1/63.1 KB 6.4 MB/s eta 0:00:00
Collecting lxml-stubs<0.2,>=0.1
  Downloading lxml_stubs-0.1.1-py3-none-any.whl (10 kB)
Collecting lxml<5,>=4
  Downloading lxml-4.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (7.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.0/7.0 MB 46.8 MB/s eta 0:00:00
Collecting charset-normalizer~=2.0.0
  Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 139.0/139.0 KB 15.5 MB/s eta 0:00:00
Collecting idna<4,>=2.5
  Downloading idna-3.3-py3-none-any.whl (61 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.2/61.2 KB 6.7 MB/s eta 0:00:00
Collecting certifi>=2017.4.17
  Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 149.2/149.2 KB 15.3 MB/s eta 0:00:00
Installing collected packages: lxml-stubs, certifi, urllib3, lxml, idna, charset-normalizer, requests, klon
Successfully installed certifi-2021.10.8 charset-normalizer-2.0.12 idna-3.3 klon-2.1.0 lxml-4.8.0 lxml-stubs-0.1.1 requests-2.27.1 urllib3-1.26.9

Check the list of installed Python packages:

$ pip3.10 list
Package            Version
------------------ ---------
certifi            2021.10.8
charset-normalizer 2.0.12
idna               3.3
klon               2.1.0
lxml               4.8.0
lxml-stubs         0.1.1
pip                22.0.4
requests           2.27.1
setuptools         58.1.0
urllib3            1.26.9  

Step 4 – Create a Python Virtual Environment

A place where Python libraries, Python interpreters, and Python scripts are installed is known as a Python Virtual Environment. This environment can be created on Amazon Linux 2 as below.

First, create a project directory. In this guide, we will name it sample_app

mkdir ~/sample_app && cd ~/sample_app

While in the created directory, create a Virtual Environment as below.

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.

(sample_app_venv) [[email protected]_hostname sample_app]$ 

Now you are in the shell and the name of the project/app is prefixed. Deactivate/exit from the environment with the command:

deactivate

Congratulations! This is the end of this guide on how to install Python 3.10 on Amazon Linux 2. I hope you learned a lot.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.