How to Install Python 3 on CentOS 7: Compile or One-Click Script

install python 3 centos 7compile python 3python 3.7 installationxiaoz python scriptpython 2 and 3 coexist
Published·Modified·

CentOS 7 comes with Python 2.7 pre-installed, but some software requires Python 3. This article shares methods to install Python 3 on CentOS 7 while keeping Python 2.7 intact, allowing both versions to coexist.

Python Animation

Compile and Install Python 3

The latest Python version is currently Python 3.7.0. If newer versions are released, the process remains largely the same. Below are the steps to compile and install Python 3.7.0. Enter the commands line by line as prompted:

# Install dependencies
yum -y install wget gcc gcc-c++ libffi-devel zlib-devel

# Download source code (official)
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz

# If the download speed is slow, you can download from the xiaoz software library
wget http://soft.xiaoz.org/python/Python-3.7.0.tar.xz

# Extract the archive
tar -xvJf Python-3.7.0.tar.xz

# Enter the directory
cd Python-3.7.0

# Compile and install
./configure --prefix=/usr/local/python3 --enable-optimizations
make -j4 && make -j4 install

# Set up symbolic links
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

If everything goes smoothly, you can check the version by running python3 -V. To use the original Python 2.7, simply type python xxx. Both versions will work independently without interfering with each other.

Python Version Check

One-Click Install Python 3

Although the compilation method is simple, typing commands line by line can be time-consuming if you need to install Python 3 on multiple servers. For a more efficient approach, use the one-click script written by xiaoz. Just copy and run the following command:

wget https://raw.githubusercontent.com/helloxz/shell/master/python3.sh && sh python3.sh

One-Click Installation Result

Other Notes

The one-click installation script has only been tested on CentOS 7. Theoretically, it should also support CentOS 6. If you encounter errors, search for the error message to check if any dependencies are missing. If you have any issues, please leave a comment for feedback.

Python Official Download Page: https://www.python.org/ftp/python/