How to Upgrade Python Version in Linux [ 7 Steps ]

In this tutorial, we will learn about how to upgrade Python version in Linux using 7 steps. Python upgrade can be done  in all Operating System be it be Windows, Linux or Mac. However, the  procedure to upgrade the Python version is different in different Operating Systems.

 

Python in Linux Overview

Python is an open source, Object Oriented Programming Language. It has features like easy-to-learn, platform independent, robust, portable etc. which makes it easy to learn and code in python. In most of the Linux distribution, by default Python comes preinstalled  but it may not be the latest version of Python. Hence, you can upgrade the Python version to latest based on the need and requirement.

 

How to Upgrade Python Version in Linux [ 7 Steps ]

How to Upgrade Python Version in Linux [ 7 Steps ]

Also read: How to Install EBS CSI Driver Using 10 Easy Steps

There are different ways to upgrade the Python version on different Linux distributions. We will focus on upgrading the Python version specifically on RHEL based distros (Fedora, Rocky Linux, CentOS etc. ). Please make sure that below prerequisites are met in order to upgrade the Python version successfully.

 

NOTE:

At the time of writing this article, Python 3.11 is the latest stable version.

Prerequisites

  • Must have root privilege or use any user which has root privilege (I am using nasauser  for this tutorial which has the root privilege).
  • Must have Red-Hat Based distro (Fedora, CentOS, RHEL, Rocky Linux etc) installed.

 

Step-1: Check the Linux Distribution

In this step, check which Linux distribution you are in using below command. As mentioned above, this tutorial will be suitable specifically for RHEL based distros.

[nasauser@linuxnasa~]$ cat /etc/os-release
NAME="Amazon Linux"
VERSION="2023"
ID="amzn"
ID_LIKE="fedora"
VERSION_ID="2023"
PLATFORM_ID="platform:al2023"
PRETTY_NAME="Amazon Linux 2023"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
HOME_URL="https://aws.amazon.com/linux/"
BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
SUPPORT_END="2028-03-01

 

Step-2: Check if python is installed

In this step, check if Python is preinstalled in your system using below command. As you see, I have Python3.9 preinstalled. So we will upgrade the version from Python3.9 to Python3.11.

 

To check if Python2 is installed, execute below command.

[nasauser@linuxnasa~]$ python -V
-bash: python: command not found

 

To check if Python3 is installed, use below command

[nasauser@linuxnasa~]$ python3 -V
Python 3.9.16

 

Step-3: Update yum Repo

In this step,  update the yum repository cache using below command. It will update the packages in the repo to the latest release. In my case, repo is already up to date, hence nothing is updated.

[nasauser@linuxnasa~]$ sudo yum -y update
Amazon Linux 2023 repository 8.0 kB/s | 3.6 kB 00:00
Amazon Linux 2023 Kernel Livepatch repository 6.4 kB/s | 2.6 kB 00:00
Stable Releases of Upstream github.com/containers packages (CentOS_7) 2.6 kB/s | 1.7 kB 00:00
===========================================================================================================================================================================
WARNING:
A newer release of "Amazon Linux" is available.

Available Versions:

Version 2023.0.20230329:
Run the following command to upgrade to 2023.0.20230329:

dnf upgrade --releasever=2023.0.20230329

Release notes:
https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

===========================================================================================================================================================================
Dependencies resolved.
Nothing to do.
Complete!

 

Step-4: Check if Python3.11 available in yum Repo

In this step, check if Python3.11 package is available in yum repository cache  to download after updating the repo in previous step. Use below command to check. In below command, we are listing down all the packages in the yum repo. After that we are using grep to filter out only Python3.11 related packages from the list.

[nasauser@linuxnasa~]$ yum list | grep python3.11
python3.11.x86_64
python3.11-debug.x86_64
python3.11-devel.x86_64
python3.11-idle.x86_64
python3.11-libs.x86_64
python3.11-pip.noarch
python3.11-pip-wheel.noarch
python3.11-setuptools.noarch
python3.11-setuptools-wheel.noarch
python3.11-test.x86_64
python3.11-tkinter.x86_64
python3.11-wheel.noarch
python3.11-wheel-wheel.noarch

 

Step-5: Install Python3.11

In this step,  upgrade the Python version from Python3.9 to Python3.11 using below command.

[nasauser@linuxnasa~]$ sudo yum install -y python3.11
Last metadata expiration check: 0:08:35 ago on Sun Apr 16 07:17:13 2023.
Dependencies resolved.
===========================================================================================================================================================================
Package Architecture Version Repository Size
===========================================================================================================================================================================
Installing:
python3.11 x86_64 3.11.2-2.amzn2023.0.6 amazonlinux 28 k
Installing dependencies:
mpdecimal x86_64 2.5.1-3.amzn2023.0.3 amazonlinux 101 k
python3.11-libs x86_64 3.11.2-2.amzn2023.0.6 amazonlinux 9.3 M
python3.11-pip-wheel noarch 22.3.1-2.amzn2023.0.2 amazonlinux 1.4 M
python3.11-setuptools-wheel noarch 65.5.1-2.amzn2023.0.4 amazonlinux 715 k

Transaction Summary
===========================================================================================================================================================================
Install 5 Packages

Total download size: 12 M
Installed size: 47 M
Downloading Packages:
(1/5): python3.11-3.11.2-2.amzn2023.0.6.x86_64.rpm 96 kB/s | 28 kB 00:00
(2/5): mpdecimal-2.5.1-3.amzn2023.0.3.x86_64.rpm 251 kB/s | 101 kB 00:00
(3/5): python3.11-pip-wheel-22.3.1-2.amzn2023.0.2.noarch.rpm 5.7 MB/s | 1.4 MB 00:00
(4/5): python3.11-setuptools-wheel-65.5.1-2.amzn2023.0.4.noarch.rpm 1.8 MB/s | 715 kB 00:00
(5/5): python3.11-libs-3.11.2-2.amzn2023.0.6.x86_64.rpm 8.5 MB/s | 9.3 MB 00:01
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 8.7 MB/s | 12 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : python3.11-pip-wheel-22.3.1-2.amzn2023.0.2.noarch 1/5
Installing : python3.11-setuptools-wheel-65.5.1-2.amzn2023.0.4.noarch 2/5
Installing : mpdecimal-2.5.1-3.amzn2023.0.3.x86_64 3/5
Installing : python3.11-3.11.2-2.amzn2023.0.6.x86_64 4/5
Installing : python3.11-libs-3.11.2-2.amzn2023.0.6.x86_64 5/5
Running scriptlet: python3.11-libs-3.11.2-2.amzn2023.0.6.x86_64 5/5
Verifying : mpdecimal-2.5.1-3.amzn2023.0.3.x86_64 1/5
Verifying : python3.11-libs-3.11.2-2.amzn2023.0.6.x86_64 2/5
Verifying : python3.11-3.11.2-2.amzn2023.0.6.x86_64 3/5
Verifying : python3.11-setuptools-wheel-65.5.1-2.amzn2023.0.4.noarch 4/5
Verifying : python3.11-pip-wheel-22.3.1-2.amzn2023.0.2.noarch 5/5
===========================================================================================================================================================================
WARNING:
A newer release of "Amazon Linux" is available.

Available Versions:

Version 2023.0.20230329:
Run the following command to upgrade to 2023.0.20230329:

dnf upgrade --releasever=2023.0.20230329

Release notes:
https://docs.aws.amazon.com/linux/al2023/release-notes/relnotes.html

===========================================================================================================================================================================

Installed:
mpdecimal-2.5.1-3.amzn2023.0.3.x86_64 python3.11-3.11.2-2.amzn2023.0.6.x86_64 python3.11-libs-3.11.2-2.amzn2023.0.6.x86_64
python3.11-pip-wheel-22.3.1-2.amzn2023.0.2.noarch python3.11-setuptools-wheel-65.5.1-2.amzn2023.0.4.noarch

Complete!

 

Step-6: Make Python3.11 as default

In this step, verify if Python3.11 got installed in the system using below command. You will notice that both the Python version (older and newer) is present in the machine. This is because, Python3 still points to  Python3.9 version as shown below.

[nasauser@linuxnasa ~]$ ls -lhtr /usr/bin/ | grep python
lrwxrwxrwx. 1 root root 9 Mar 1 04:28 python3 -> python3.9
-rwxr-xr-x. 1 root root 16K Mar 1 04:28 python3.9
-rwxr-xr-x. 1 root root 16K Mar 9 00:45 python3.11

 

We will change  python3 to point to python3.11  using below command. First, we will unlink the python3.9 from python3  using below command.

[nasauser@linuxnasa ~]$ sudo unlink /usr/bin/python3

 

Next, we will link python3.11 to python3 using below command

[nasauser@linuxnasa ~]$ sudo ln -s /usr/bin/python3.11 /usr/bin/python3

 

Now check if python3 is pointing to latest python version using below command.

[nasauser@linuxnasa ~]$ ll /usr/bin/ | grep python
lrwxrwxrwx. 1 root root 19 Apr 16 08:06 python3 -> /usr/bin/python3.11
-rwxr-xr-x. 1 root root 16168 Mar 9 00:45 python3.11
-rwxr-xr-x. 1 root root 16168 Mar 1 04:28 python3.9

 

Step-7: Verify  Python3.11

In this step, verify if python3 command returns the latest python3.11 as output using below command.

[nasauser@linuxnasa ~]$ python3 -V
Python 3.11.2

 

Conclusion

In this tutorial, we learn about how to upgrade the python version in Linux and change the default pointing of python version. It is important to note that each newer version of Python comes up with some new feature, better performance and upgraded modules and packages which becomes helpful for a developer. The upgraded versions also comes up the the security and other related bug’s fixes.

Leave a Comment