Install Python3 on CentOS 7

431 阅读1分钟

Install

CentOS 7 does not officially support Python 3.11, but you can still install it manually using the following steps:

  1. First, update your system's package list by running:
sudo yum update
  1. Install the development tools and dependencies required for building Python from source:
sudo yum groupinstall 'Development Tools'
sudo yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
  1. Download the Python 3.11 source code from the official website (www.python.org/downloads/s…).
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz

this download is too slow, so I use the mirror: mirrors.huaweicloud.com/python/3.11…

  1. Extract the downloaded archive:
tar -xvf Python-3.11.0.tgz
cd Python-3.11.0
  1. Configure and compile the Python source code:
./configure --prefix=/usr/local --enable-optimizations
make altinstall
  1. The altinstall command installs Python as /usr/local/bin/python3.11 instead of overwriting the existing version of Python.

That's it! You should now have Python 3.11 installed on your CentOS 7 system. To verify that it has been installed correctly, run:

python3.11 --version

This should display the version number of Python 3.11 that you just installed.

the step 6 does not work for me. so continue:

Config

To switch between different versions of Python in CentOS 7, you can use alternatives. Here are the steps:

  1. Update the alternatives configuration to include the new Python version:
sudo alternatives --install /usr/bin/python python /usr/local/bin/python3.11 1
  1. To set the default version of Python, use the --set option:
sudo alternatives --set python /usr/local/bin/python3.11
  1. Verify that the correct version of Python is being used by running:
python --version

This should display the version number of the Python interpreter that is currently being used. Alternatively, you can also specify the full path of the desired Python interpreter when invoking it:

/usr/local/bin/python3.11

This will always use the specified version of Python, regardless of the current alternative setting.

Test it, Ok.

# python --version

Python 3.11.0