CentOS7 下安装 Python3.8.5

一、前置条件

1)安装依赖

1
yum -y install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make

2)更新 openssl

1
2
3
4
5
6
7
8
9
10
11
wget --no-check-certificate   https://www.openssl.org/source/openssl-1.1.1t.tar.gz
tar -zxvf openssl-1.1.1t.tar.gz
cd openssl-1.1.1t/
./config --prefix=/usr/local/my_openssl
make
make install

mv /usr/bin/openssl /usr/bin/oldopenssl
ln -s /usr/local/my_openssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/my_openssl/lib/libssl.so.1.1 /usr/lib64/
ln -s /usr/local/my_openssl/lib/libcrypto.so.1.1 /usr/lib64/

二、下载解压

1
2
3
wget https://mirrors.huaweicloud.com/python/3.8.5/Python-3.8.5.tgz

tar -zxvf Python-3.8.5.tgz

三、编译安装

1
2
3
4
5
6
cd Python-3.8.5

# 链接到新版本OpenSSL
./configure --with-openssl=/usr/local/my_openssl --prefix=/usr/local/python3

make clean && make && make install

四、配置环境变量并创建软连接

1
2
3
4
5
6
7
8
9
10
11
# vim /etc/profile

export PYTHON_HOME=/usr/local/python3
export PATH=$PYTHON_HOME/bin:$PATH

# 使环境变量生效
source /etc/profile

# 创建软连接
ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3

五、配置清华源

1
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

六、测试

1
2
3
4
5
6
7
8
# 查看 python 版本
python3 -V

# 查看 pip 版本
pip3 -V

# 查看 python 环境变量
echo $PYTHON_HOME