1

没有 Python 的 CentOS 还有救吗?

 3 years ago
source link: https://scvoet.me/article/centos-without-python
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在装 TG/QQ/Discord 互联的时候需要用到 Python3,而 CentOS 默认的 Python 版本仅 2.6。但我在编译 Python3 出了问题又排查不出来,就打算删除重装,然而不小心把 Python2 也删掉了,yum 随之爆炸。查了点资料打算用 rpm 包开始重装。

重装 Python2 和 yum展开目录

卸载残余文件展开目录

因为只删除了几个文件夹,但也没法复原了。索性就先清理干净 yum 和 python。

rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps
# 清理残余文件
whereis python |xargs rm -frv
# 检查是否卸载成功
whereis python
# 卸载 yum
rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps 
# 清理残余文件
whereis yum |xargs rm -frv 
# 检查是否卸载成功
whereis yum 

布置环境展开目录

不同的版本需要用到不同的 rpm 包,所以我们得先确认一下环境。

# 检查版本
cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

编译展开目录

根据上面查出的版本号,我们前往 CentOS 官网找到对应的包。

下文中的命令都与 CentOS Linux release 7.8.2003 (Core) 对应。

http://vault.centos.org/7.8.2003/os/x86_64/Packages/

你需要替换 7.8.2003 为服务器所对应的版本号打开链接,但你不能直接替换命令中版本号,不同的版本所对应的包名也有细微差异,你需要根据下方命令中所用到的包来查找相应包名并替换。

# 创建文件夹
mkdir /usr/local/src/python
mkdir /usr/local/src/yum
# 进入文件夹
cd /usr/local/src/python
# python 安装
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/python-2.7.5-88.el7.x86_64.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/python-devel-2.7.5-88.el7.x86_64.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/python-libs-2.7.5-88.el7.x86_64.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/rpm-python-4.11.3-43.el7.x86_64.rpm
# 进入文件夹
cd /usr/local/src/yum
# yum 安装
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-3.4.3-167.el7.centos.noarch.rpm
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm 
rpm -ivh  --nodeps http://vault.centos.org/7.8.2003/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-53.el7.noarch.rpm

安装 Python3展开目录

系统恢复正常后,那就继续安装 Python3。先把依赖包给装好。

# 安装依赖包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel gcc db4-devel libpcap-devel xz-devel libffi-devel

我们通过自定义变量的方式控制安装的 Python 版本,以避免传统批量替换版本号实行自定义版本所造成的麻烦。

#自定义变量
VERSION=3.9.0
#下载
wget https://www.python.org/ftp/python/${VERSION}/Python-${VERSION}.tgz
#解压
tar -zxvf Python-${VERSION}.tgz
cd Python-${VERSION}
#指定编译目录
./configure --prefix=/usr/local/python3
#编译安装
make && make install
#建立软连接
ln -s /usr/local/python3/bin/python${VERSION}/usr/bin/python3
ln -s /usr/local/python3/bin/pip${VERSION}/usr/bin/pip3

检查一下安装成功了没。

python3
pip3

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK