先将所有命令总结如下:(懒人福利,直接粘过去使用,或者脚本)
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install zlib-devel bzip2-devel openssl-devel openssl-static ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel lzma gcc
cd /usr/local/src/
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
tar xvf Python-3.8.5.tar.xz
mv Python-3.8.5 /usr/local/python-3.8
cd /usr/local/python-3.8/
./configure --prefix=/usr/local/sbin/python-3.8
make && make install
rm -rf /usr/bin/python
ln -sv /usr/local/sbin/python-3.8/bin/python3 /usr/bin/python
ll /usr/bin/ |grep python
python -V
vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down
vim /usr/bin/yum-config-manager
ln -s /usr/local/sbin/python-3.8/bin/pip3 /usr/bin/pip
pip --version
-----------------------------------------------------我是分隔符---------------------------------------------------------------
具体过程如下:
本次使用的系统版本为CentOS Linux release 7.8.2003 (Core),查看方式:cat /etc/redhat-release
1. 安装依赖
# 安装依赖之前更新一下yum源:
cd /etc/yum.repos.d/
mkdir bak
mv * bak
wget http://mirrors.aliyun.com/repo/Centos-7.repo
wget http://mirrors.aliyun.com/repo/epel-7.repo
# 再安装Python依赖:
yum -y install zlib-devel bzip2-devel openssl-devel openssl-static ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel lzma gcc
2. 下载Python安装包
cd /usr/local/src/
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tar.xz
3. 编译并安装
# 解压安装包:
tar xvf Python-3.8.5.tar.xz
mv Python-3.8.5 /usr/local/python-3.8
cd /usr/local/python-3.8/
# 将python3.8安装到/usr/local/sbin/python-3.8目录下
./configure --prefix=/usr/local/sbin/python-3.8
make && make install
便是安装成功了
/usr/local/sbin/python-3.8/bin/python3
可以使用Python3,但是这样使用不是很方便
因为python -V
可以看出centos7是自带Python 2.7.5
所以接下来是修改默认Python
4. 先瞅一眼Python的绝对路径
[root@localhost python-3.8]# which python
/usr/bin/python
[root@localhost python-3.8]# ll /usr/bin/ |grep python
-rwxr-xr-x. 1 root root 11240 4月 2 23:58 abrt-action-analyze-python
lrwxrwxrwx. 1 root root 7 7月 31 17:52 python -> python2
lrwxrwxrwx. 1 root root 9 7月 31 17:52 python2 -> python2.7
-rwxr-xr-x. 1 root root 7144 4月 2 21:17 python2.7
可以看出,原先的Python命令是通过软链接连接到python2.7的
现在需要将改到新安装的python3
5. 将python命令通过软链接指向到python3命令(就是上面那一大串/usr/local/sbin/python-3.8/bin/python3)
[root@localhost python-3.8]# rm -rf /usr/bin/python
[root@localhost python-3.8]# ln -sv /usr/local/sbin/python-3.8/bin/python3 /usr/bin/python
"/usr/bin/python" -> "/usr/local/sbin/python-3.8/bin/python3"
[root@localhost python-3.8]# ll /usr/bin/ |grep python
-rwxr-xr-x. 1 root root 11240 4月 2 23:58 abrt-action-analyze-python
lrwxrwxrwx. 1 root root 38 7月 31 18:39 python -> /usr/local/sbin/python-3.8/bin/python3
lrwxrwxrwx. 1 root root 9 7月 31 17:52 python2 -> python2.7
-rwxr-xr-x. 1 root root 7144 4月 2 21:17 python2.7
[root@localhost python-3.8]# python -V
Python 3.8.5
6. yum命令不能执行,需做如下修改
vim /usr/bin/yum
# 将顶部的 !/usr/bin/python 改成 !/usr/bin/python2.7
vim /usr/libexec/urlgrabber-ext-down
# 将顶部的 ! /usr/bin/python 改成 ! /usr/bin/python2.7
vim /usr/bin/yum-config-manager
# 将顶部的 #!/usr/bin/python -tt 改成 #!/usr/bin/python2.7 -tt
# 将pip指向到python3.8
ln -s /usr/local/sbin/python-3.8/bin/pip3 /usr/bin/pip
命令pip --version
可以看到:
[root@localhost python-3.8]# pip --version
pip 20.1.1 from /usr/local/sbin/python-3.8/lib/python3.8/site-packages/pip (python 3.8)