Centos7用yum安装的Python3版本比较旧,想要安装最新版本的Python3需要自己动手编译安装。下面就来讲讲安装步骤,主要分为这么几个步骤,依赖→下载→编译→配置。另外所有操作都是在root用户下进行。
依赖
编译Python源码需要依赖许多库,在编译过程中出现的一些常见问题大部分是没有安装相关依赖库。这里举几个例子。
比如说下面的警告信息,就是没有安装相关的依赖库。
The necessary bits to build these optional modules were not found: _curses _curses_panel _dbm _gdbm _lzma _sqlite3 _tkinter _uuid readline To find the necessary bits, look in setup.py in detect_modules() for the module's name.
再比如说下面这个,是因为OpenSSL的版本太旧,需要安装openssl11 ,并且在编译的时候设置编译FLAG,后续会介绍到。
Failed to build these modules: _hashlib _ssl Could not build the ssl module! Python requires a OpenSSL 1.1.1 or newer
因为官网源下载比较慢,所以这里备份并替换为阿里云源,这样做不是必须的,视你的情况而定。
tar -zcvf CentOS-bk.tar.gz /etc/yum.repos.d/CentOS-* curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
然后安装依赖
yum -y groupinstall "Development tools" yum install -y ncurses-devel gdbm-devel xz-devel sqlite-devel tk-devel uuid-devel readline-devel bzip2-devel libffi-devel yum install -y openssl-devel openssl11 openssl11-devel
下载
安装完依赖之后就可以去官网下载最新版本的源码了。官网地址:https://www.python.org/。打开官网之后选择Download→ Source code,选择最新Stable Releases版本的Gzipped source tarball下载即可。
本文直接在Centos上下载
mkdir -p /doc/temp && cd /doc/temp wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz
编译
编译主要需要注意的问题是设置编译FLAG,以便使用最新的openssl库。
export CFLAGS=$(pkg-config --cflags openssl11) export LDFLAGS=$(pkg-config --libs openssl11)
结果如下
[root@localhost temp]# echo $CFLAGS -I/usr/include/openssl11 [root@localhost temp]# echo $LDFLAGS -L/usr/1ib64/openss111 -1ssl -lcrypto [root@localhost temp]#
设置好了之后,进入源码目录
tar xvzf Python-3.10.10.tgz cd Python-3.10.10
然后直接编译安装即可,需要花一点时间。
./configure --enable-optimizations && make altinstall
安装完成后验证一下是否安装成功
/usr/local/bin/python3.10 --version /usr/local/bin/pip3.10 --version
配置
配置快捷命令
通过设置软链接简化命令输入
设置方法: 通过ln命令
ln -sf /usr/local/bin/python3.10 /usr/bin/python3 ln -sf /usr/local/bin/pip3.10 /usr/bin/pip3
再验证一下设置是否正确
[root@localhost Python-3.10.10]python3 --version Python 3.10.10 [root@localhost Python-3.10.10]pip3 --version pip 22.0.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
配置pip源
由于网络原因,官方的pip源无法访问,会出现类似如下错误
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)'))) - skipping
所以需要替换成国内源,方法如下。
新建pip配置文件
mkdir -p ~/.pip touch ~/.pip/pip.conf
编辑配置文件,配置如下。
vim ~/.pip/pip.conf
pip.conf的文件代码:
[global] index-url=https://pypi.tuna.tsinghua.edu.cn/simple/ extra-index-url= http://pypi.douban.com/simple/ http://mirrors.aliyun.com/pypi/simple/ #proxy = [user:passwd@]proxy.server:port [install] trusted-host= pypi.tuna.tsinghua.edu.cn pypi.douban.com mirrors.aliyun.com ssl_verify: false
上面配置了清华,豆瓣,阿里的源,并且关闭了ssl验证。当然如果有需要还可以设置代理,把注释掉的proxy那行放开即可。
再验证一下
pip3 install --upgrade pip
ps: 替换pip下载源到国内的阿里云, 可以用下列shell命令:
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/ pip config set global.trusted-host mirrors.aliyun.com