Update Python 3 on CentOS

Updated on November 21, 2023
Update Python 3 on CentOS header image

Introduction

This article explains how to install the latest version of Python3 from source code on CentOS 8.

Prerequisites

1. Check the Installed Version

Check the installed Python3 version.

# python3 -V

You will see something like this.

Python 3.6.8

If your version is older than your application requires, proceed with this guide.

2. Update Python3

  1. Install the required dependencies to compile the Python source code.

     # yum groupinstall 'development tools' -y && yum install wget openssl-devel bzip2-devel libffi-devel xz-devel -y
  2. Visit Python's Source Code Download page to find the latest gzipped source release. Replace the URLs and filenames in this guide with the latest version.

  3. Download the latest Python3 source code.

     # wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
  4. Extract the downloaded python source code archive.

     # tar xzf Python-3.9.6.tgz
  5. Compile the python source code.

     # cd Python-3.9.6 && ./configure --enable-optimizations
  6. Install Python.

     # make altinstall
  7. Check the name of your new Python executable.

     # ls /usr/local/bin/python*
  8. Set the new Python executable as default. Replace the two instances of /python3.9 in the following command with the name of your new Python executable.

     # alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.9 1 && alternatives --set python3 /usr/local/bin/python3.9 && echo "2" | alternatives --config python

2. Update Pip

  1. Update pip. Use the name of your new Python executable in the following command.

     # /usr/local/bin/python3.9 -m pip install --upgrade pip
  2. Check the name of your new pip executable.

     # ls /usr/local/bin/pip*
  3. Set the new pip as the default. Use the name of your new pip executable in the following command.

     # alternatives --install /usr/bin/pip pip /usr/local/bin/pip3.9 1 && alternatives --set pip /usr/local/bin/pip3.9
  4. Check the current version of Python and pip.

     # python -V && pip -V

    You will see something like this:

     Python 3.9.6
     pip 21.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Conclusion

Keeping your Python up-to-date is an important part of the development process. It provides bug fixes compared to older versions, and more importantly, it introduces new features and optimizations.