centos python2.7 升级 python3

安装一个软件要求python3.6 以上版本,当前的版本是2.7.5

系统配置

CentOS Linux release 7.8.2003 (Core)

python源码下载

https://www.python.org/downloads/source/
我们下载最新的版本。

wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
tar zxvf Python-3.9.6.tgz

编译

./configure
make
make install

设置系统默认的python版本

检查版本

python -V
Python 2.7.5
python3 -V
Python 3.9.6

我们需要设置环境变量,默认使用python3

cd /usr/bin
ls -al /usr/bin | grep python
lrwxrwxrwx    1 root root          7 Jul 10  2020 python -> python2
lrwxrwxrwx    1 root root          9 Jul 10  2020 python2 -> python2.7
-rwxr-xr-x    1 root root       7144 Apr  2  2020 python2.7
-rwxr-xr-x    1 root root       1835 Apr  2  2020 python2.7-config
lrwxrwxrwx    1 root root         16 Jul 10  2020 python2-config -> python2.7-config
lrwxrwxrwx    1 root root         14 Jul 10  2020 python-config -> python2-config

照猫画虎,将建立python3的软连接

ln -s /usr/local/bin/python3 /usr/bin/python
python -V
Python 3.9.6
[root@li1974-11

可见python3已生效。

还原yum

因为yum是用的默认版本,python3会导致yum失效。需要对yum的python做指定。修改以下2个文件:

vi /usr/bin/yum
vi /usr/libexec/urlgrabber-ext-down

修改首行的 #!/usr/bin/python 为 #!/usr/bin/python2.7

验证yum修改后的效果:

yum --version
3.4.3
  Installed: rpm-4.11.3-43.el7.x86_64 at 2020-07-10 12:34
  Built    : CentOS BuildSystem <http://bugs.centos.org> at 2020-04-01 04:21
  Committed: Panu Matilainen <pmatilai@redhat.com> at 2019-10-04

  Installed: subscription-manager-1.24.45-1.el7.centos.x86_64 at 2021-04-29 18:18
  Built    : CentOS BuildSystem <http://bugs.centos.org> at 2020-12-15 16:38
  Committed: William Poteat <wpoteat@redhat.com> at 2020-11-24

  Installed: yum-3.4.3-167.el7.centos.noarch at 2020-07-10 12:36
  Built    : CentOS BuildSystem <http://bugs.centos.org> at 2020-04-02 15:56
  Committed: CentOS Sources <bugs@centos.org> at 2020-03-31

  Installed: yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch at 2020-07-10 12:36
  Built    : CentOS BuildSystem <http://bugs.centos.org> at 2020-05-12 16:27
  Committed: Michal Domonkos <mdomonko@redhat.com> at 2020-03-12

yum 正常。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注