CentOS安装Python3
一、组件安装
需要安装组件libffi-devel
,否则在部署Python项目时,会遇到错误。
yum install libffi-devel -y
# 这一步不一定需要,CentOS7可能需要,不然安装到make&&make install的时候会有error
yum -y install zlib*
二、依赖安装
# openssl、bzip等如果有报错,请到文章末尾看
# CentOS8
yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
#CentOS7
yum -y install openssl-devel bzip2-devel sqlite-devel readline-devel expat-devel gdbm-devel zlib-devel zlib* ncurses-devel libffi-devel tk-devel libffi-devel
#安装make编译所需的gcc、g++编译器
yum -y install gcc automake autoconf libtool make wget
三、下载Python
# 进入指定路径
cd /usr/local
# 下载Python
wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
# 解压压缩包
tar -zxvf Python-3.9.6.tgz
tar -zxvf Python-3.6.8.tgz
tar -zxvf Python-3.8.3.tgz
tar -zxvf Python-3.7.0.tgz
# 把解压后的文件名改为Python3
mv /usr/local/Python-3.9.6/ ./python3
mv /usr/local/Python-3.6.8/ ./python3
mv /usr/local/Python-3.8.3/ ./python3
mv /usr/local/Python-3.7.0/ ./python3
四、安装Python
# 进入目录
cd /usr/local/python3
#设置安装目录
./configure --prefix=/usr/local/python3
# ./configure后边prefix不写就是当前目录
# 编译和安装
make && make install
五、创建软链
# 软链python
ln -s /usr/local/python3/bin/python3 /usr/bin/python
# 软链pip
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip
# 移除软链
rm -rf /usr/bin/python
rm -rf /usr/bin/pip
# 验证是否安装成功
pip -V
python -V
六、安装过程中问题
1、CentOS7 zipimport报错
报错内容zipimport.ZipImportError: can't decompress data; zlib not available
解决方法
yum -y install zlib*
重新编译
make&&make install
2、CentOS安装Python高版本后yum报错
# 查看系统原来的Python版本
whereis python
# 原因是yum使用的Python环境不适应
# 解决方法:编辑如下两个文件,指定系统原来的Python版本
vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down
# 修改第一行为
#!/usr/bin/python2.7
3、依赖安装时报错
报错内容libselinux conflicts with fakesystemd-1-17.el7.centos.noarch
解决方法
yum swap -y fakesystemd systemd && yum clean all
yum update -y && yum clean all