How to Upgrade Python 3 on a Linux Cloud Server?

04-02-2024 02:39:46

This article outlines the process of upgrading to the latest version of Python 3 on a Linux cloud server via source code compilation, applicable to Linux distributions such as CentOS, Ubuntu, Debian, and others.

First, verify the current version of Python 3.

# python3 -V
Python 3.6.8

Install the components required for compiling the source code.

# yum groupinstall 'development tools' -y && yum install wget openssl-devel bzip2-devel libffi-devel xz-devel -y

Visit the official Python website to obtain the latest version of the source code file in gzip format. For example, version 3.9.6 is used here; replace it with the URL of the latest version during actual operations.

# wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
# tar xzf Python-3.9.6.tgz
# cd Python-3.9.6 && ./configure --enable-optimizations
# make altinstall

Update the Python package management tool PIP.

# /usr/local/bin/python3.9 -m pip install --upgrade pip

Verify the versions of Python and PIP.

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

Upgrading Python to the latest version is a crucial part of cloud server maintenance work. The latest version of Python not only addresses some issues found in older versions but also introduces new features and optimizations.