Table of Contents
Commands have been tested on a RHEL LXC Container
Python 3.10 is the latest version of the popular programming language, packed with new features, security patches, and performance improvements. One notable addition is the support for parenthesized context managers, allowing for better readability and continuation across multiple lines. In this blog post, we’ll walk you through the process to install Python 3.10 from source on Fedora and CentOS/RHEL 8 systems because for some reason no rpms are available for this version.
Prerequisites
Before we begin, ensure that your system has the following:
- A pre-installed GCC compiler
- SSH or shell access to your server
To install the necessary packages, run the following command:
sudo dnf install wget yum-utils make gcc openssl-devel bzip2-devel libffi-devel zlib-devel
Step 1: Download Python 3.10 Source Code
Visit the official Python download site at https://www.python.org/ftp/python to get the latest version of Python 3.10. Alternatively, you can use the command line to download the source code directly:
wget https://www.python.org/ftp/python/3.10.14/Python-3.10.14.tgz
Once the download is complete, extract the archive file:
tar xzf Python-3.10.14.tgz
This will create a directory named Python-3.10.
14 containing the source files.
Step 2: install Python 3.10 on RHEL
Navigate to the extracted directory:
cd Python-3.10.14
Prepare the source code with the required values to install python 3.10 before compiling:
sudo ./configure --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions
Next, compile the source code using make
. Use all available CPU cores for optimal performance but it may still take a little while:
#this command may fail - sudo make -j ${nproc}
sudo make altinstall
make altinstall
prevents replacing the default Python binary at /usr/bin/python
.
You should see something along the lines of the picture below:
After installation, remove the downloaded archive to free up space:
sudo rm Python-3.10.14.tgz
Step 3: Test Python Version
Verify the installed Python and PIP versions:
python3.10 -V
#Python 3.10.14
pip3.10 -V
#pip 23.0.1 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
You have successfully installed Python 3.10.14 on your Fedora, CentOS/RHEL 8, or Rocky Linux system.
Quicker Method
A user posted on GitHub a script to facilitate installation of python 3.10 on RHEL, the version installed is 3.10.8 and not .14
#!/usr/bin/env bash
sudo dnf install wget yum-utils make gcc openssl-devel bzip2-devel libffi-devel zlib-devel
# Ubuntu: sudo apt install -y wget yum-utils make gcc libssl-dev libbz2-dev libffi-dev zlib1g-dev
mkdir tmp
cd tmp
wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz
tar xzf Python-3.10.8.tgz
cd Python-3.10.8
sudo ./configure --with-system-ffi --with-computed-gotos --enable-loadable-sqlite-extensions --enable-optimizations
sudo make -j $(nproc)
# to remove the unused stuff: find . -maxdepth 1 -mtime +1 -exec rm -rf {} \;
# to install pip we can use: python -m ensurepip --upgrade
# it will install pip into $HOME/.local/bin
sudo make altinstall
sudo rm Python-3.10.8.tgz
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.10 1
sudo update-alternatives --set python3 /usr/local/bin/python3.10
sudo rm -rf /usr/local/bin/pip3
sudo update-alternatives --install /usr/bin/pip3 pip3 /usr/local/bin/pip3.10 1
sudo update-alternatives --set pip3 /usr/local/bin/pip3.10
FAQ
- Can I install multiple versions of Python on the same system?
Yes, you can have multiple Python versions installed simultaneously. Use the specific version’s binary (e.g.,python3.10
) to run the desired Python version. - How do I set Python 3.10 as the default Python version?
It’s generally not recommended to replace the default Python version to avoid potential system conflicts. However, you can create an alias or update your environment variables to use Python 3.10 by default.