目录

centos中编译安装Python

centos中编译安装Python

安装编译所需依赖

1
2
3
yum -y update
yum -y install openssl-devel libffi-devel bzip2-devel
yum -y groupinstall "Development Tools"

安装 Python 3.10 必需的 openssl >= 1.1.1 由于python3.10之后版本不再支持libressl使用ssl,需要使用openssl安装来解决编译安装 python时候遇到的ssl模块导入失败的问题,这里需要用的openssl1.1.1版本或者更高版本

1
2
3
4
5
wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz --no-check-certificate
tar zxf openssl-1.1.1q.tar.gz
cd openssl-1.1.1q
./config --prefix=/usr/local/openssl-1.1.1
make && make install

安装Python 3.10

1
2
3
4
5
wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz
tar zxf Python-3.10.8.tgz
cd Python-3.10.8
./configure --prefix=/opt/python/python3108 --enable-optimizations --with-openssl=/usr/local/openssl-1.1.1 --with-openssl-rpath=auto
make altinstall

添加软链接

1
2
3
4
5
6
7
8
whereis python
# python2.7默认安装是在 /usr/bin目录中,切换到/usr/bin/
cd /usr/bin/
ll python*
# 添加软链接,将原来的链接备份
mv /usr/bin/python /usr/bin/python.bak
# 添加python3软链接
ln -s /opt/python/python3108/bin/python3.10 /usr/bin/python

测试是否安装成功了

1
python -V

默认的centos7是没有安装pip,先添加epel扩展源

1
yum -y install epel-release

安装pip

1
yum install python-pip

更改yum配置,因为其要用到python2才能执行,否则会导致yum不能正常使用

1
2
3
4
5
6
vi /usr/bin/yum
#把第一行的#! /usr/bin/python 修改为如下
#! /usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
#修改如下
#! /usr/bin/python2