Preserving Python 2
First, locate the Python installation and back up the Python 2.7 symlink.
# Navigate to home directory and find Python locations
cd ~
whereis python
# Check existing Python symlinks in /usr/bin
cd /usr/bin
ls -l python*
# Back up the python symlink
mv python python.backup
Downloading and Installing Python 3
Download Python 3 from the official Python FTP server: https://www.python.org/ftp/python/
# Create directory for Python 3 and download the source
cd /usr/local
mkdir python3
cd python3
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
# Extract the archive
sudo tar -xvf Python-3.6.8.tgz
# Configure and compile Python 3
cd Python-3.6.8
./configure --prefix=/usr/local/python3
make && sudo make install
If you encounter a zlib error during compilation:
# Restore original Python symlink temporarily
mv /usr/bin/python.backup /usr/bin/python
# Install zlib development libraries
yum install zlib-devel -y
# Re-backup the symlink
mv /usr/bin/python /usr/bin/python.backup
# Retry the compilation
make clean
./configure --prefix=/usr/local/python3
make && sudo make install
Create a symbolic link for Python 3:
ln -s /usr/local/python3/bin/python3 /usr/bin/python
Modiyfing YUM Configuration
YUM depends on Python 2, so update its configuration files:
# Update YUM to use Python 2
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/bin/yum
# Update URL grabber extension
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/libexec/urlgrabber-ext-down
Configuring PIP
Create a symbolic link for pip:
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
Setting Up PIP Mirror
Configure pip to use a mirrer for faster downloads:
# Create pip configuration directory
mkdir -p ~/.pip
cd ~/.pip
# Create configuration file
cat > pip.conf << 'EOF'
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors = true
mirrors = http://pypi.douban.com/simple/
trusted-host = pypi.douban.com
EOF
For Windows systems, create a pip.ini file at:
C:\Users\[Username]\AppData\Roaming\pip\
With the same content as above.
Complete Installation Script
Here's a consolidated script for installing Python 3.6.8:
#!/bin/bash
# Install required dependencies
yum install zlib-devel -y
# Backup original Python symlink
mv /usr/bin/python /usr/bin/python.backup
# Create Python 3 directory
mkdir -p /usr/local/python3
cd /usr/local/python3
# Download and extract Python 3.6.8
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
tar -xf Python-3.6.8.tgz
# Compile and install
cd Python-3.6.8
./configure --prefix=/usr/local/python3
make && sudo make install
# Create symbolic links
ln -sf /usr/local/python3/bin/python3 /usr/bin/python
ln -sf /usr/local/python3/bin/pip3 /usr/bin/pip
# Update YUM configuration
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/bin/yum
sed -i 's#/usr/bin/python#/usr/bin/python2#g' /usr/libexec/urlgrabber-ext-down
# Configure pip mirror
mkdir -p ~/.pip
cat > ~/.pip/pip.conf << 'EOF'
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors = true
mirrors = http://pypi.douban.com/simple/
trusted-host = pypi.douban.com
EOF
# Verify installation
python --version
pip --version