CentOS 升级安装Python2.7.X

博客作者:联系请点击,搬运不易,希望请作者喝咖啡,可以点击联系博客作者

第一步:查看Centos版本及python版本

  • Centos版本

[root@k8s-admin-lb ~]# cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
  • Python版本

[root@k8s-admin-lb ~]# python -V
Python 2.7.5
[root@k8s-admin-lb ~]# which python
/usr/bin/python
[root@k8s-admin-lb ~]# ll -l /usr/bin/python*
lrwxrwxrwx. 1 root root    7 Oct  8 17:14 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 Oct  8 17:14 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Aug  7 08:52 /usr/bin/python2.7
-rwxr-xr-x  1 root root 1835 Aug  7 08:51 /usr/bin/python2.7-config
lrwxrwxrwx  1 root root   16 Oct  8 17:20 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx  1 root root   14 Oct  8 17:20 /usr/bin/python-config -> python2-config

第二步:从官网下载python对应版本的包(以2.7.9版本为例)

[root@k8s-admin-lb ~]# wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9rc1.tgz
--2019-12-31 10:58:17--  https://www.python.org/ftp/python/2.7.9/Python-2.7.9rc1.tgz
Resolving www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:36::223
Connecting to www.python.org (www.python.org)|151.101.108.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 16646401 (16M) [application/octet-stream]
Saving to: ‘Python-2.7.9rc1.tgz’

100%[========================================================================================================================================================================>] 16,646,401   132KB/s   in 1m 55s

2019-12-31 11:00:12 (142 KB/s) - ‘Python-2.7.9rc1.tgz’ saved [16646401/16646401]

第三步:解压、配置、编译、安装python2.7.9

  • 安装gcc(在编译时会依赖)

[root@k8s-admin-lb ~]# yum install -y gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2-devel sqlite-devel python-devel zlib
  • 配置、编译、安装

[root@k8s-admin-lb ~]# tar -zvxf Python-2.7.9rc1.tgz -C /usr/local/
[root@k8s-admin-lb ~]# cd /usr/local/Python-2.7.9rc1/
[root@k8s-admin-lb Python-2.7.9rc1]# ./configure --prefix=/usr/local  && make
[root@k8s-admin-lb Python-2.7.9rc1]# make altinstall     # 不要使用make install,否则会覆盖系统自带python

第四步:安装后环境检查

  • python安装后的版本

[root@k8s-admin-lb ~]# python -V       # 发现版本还是原版本
Python 2.7.5
  • 安装前后的python对比

[root@k8s-admin-lb ~]# ll -l /usr/bin/python*         # 系统自带的
lrwxrwxrwx. 1 root root    7 Oct  8 17:14 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 Oct  8 17:14 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Aug  7 08:52 /usr/bin/python2.7
-rwxr-xr-x  1 root root 1835 Aug  7 08:51 /usr/bin/python2.7-config
lrwxrwxrwx  1 root root   16 Oct  8 17:20 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx  1 root root   14 Oct  8 17:20 /usr/bin/python-config -> python2-config
[root@k8s-admin-lb ~]# ll -l /usr/local/bin/python*   # 手工安装的
-rwxr-xr-x 1 root root 8205896 Dec 31 11:18 /usr/local/bin/python2.7
-rwxr-xr-x 1 root root    1687 Dec 31 11:18 /usr/local/bin/python2.7-config
  • 备份旧版本,连接新版本

[root@k8s-admin-lb ~]# mv /usr/bin/python /usr/bin/python2.7.5
[root@k8s-admin-lb ~]# ll -l /usr/bin/python*
lrwxrwxrwx. 1 root root    9 Oct  8 17:14 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Aug  7 08:52 /usr/bin/python2.7
lrwxrwxrwx. 1 root root    7 Oct  8 17:14 /usr/bin/python2.7.5 -> python2
-rwxr-xr-x  1 root root 1835 Aug  7 08:51 /usr/bin/python2.7-config
lrwxrwxrwx  1 root root   16 Oct  8 17:20 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx  1 root root   14 Oct  8 17:20 /usr/bin/python-config -> python2-config
[root@k8s-admin-lb ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python
[root@k8s-admin-lb ~]# ll -l /usr/bin/python*
lrwxrwxrwx  1 root root   24 Dec 31 11:32 /usr/bin/python -> /usr/local/bin/python2.7
lrwxrwxrwx. 1 root root    9 Oct  8 17:14 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7216 Aug  7 08:52 /usr/bin/python2.7
lrwxrwxrwx. 1 root root    7 Oct  8 17:14 /usr/bin/python2.7.5 -> python2
-rwxr-xr-x  1 root root 1835 Aug  7 08:51 /usr/bin/python2.7-config
lrwxrwxrwx  1 root root   16 Oct  8 17:20 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx  1 root root   14 Oct  8 17:20 /usr/bin/python-config -> python2-config
  • 再次检查python版本

[root@k8s-admin-lb ~]# python -V
Python 2.7.9rc1
  • 若想访问老版本python(如2.7.5版本)

[root@k8s-admin-lb ~]# python2.7.5 -V
Python 2.7.5
  • 其他python访问,比如python2、python2.7

[root@k8s-admin-lb ~]# python2 -V
Python 2.7.5
[root@k8s-admin-lb ~]# python2.7 -V
Python 2.7.9rc1

第五步:设置yum(系统预装的yum引用的老版本python)

[root@k8s-admin-lb ~]# yum -y install epel-release      #yum这个时候是报错的
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.9rc1 (default, Dec 31 2019, 11:15:01)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

If you cannot solve this problem yourself, please go to
the yum faq at:
  http://yum.baseurl.org/wiki/Faq
  • 更改设置

[root@k8s-admin-lb ~]# which yum
/usr/bin/yum
[root@k8s-admin-lb ~]# vim /usr/bin/yum
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
[root@k8s-admin-lb ~]# vim /usr/libexec/urlgrabber-ext-down
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
  • 测试yum是否可用

[root@k8s-admin-lb ~]# yum -y install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-12 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================================
 Package                                                 Arch                                              Version                                         Repository                                       Size
=================================================================================================================================================================================================================
Installing:
 epel-release                                            noarch                                            7-12                                            epel                                             15 k

Transaction Summary
=================================================================================================================================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-12.noarch.rpm                                                                                                                                                              |  15 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-12.noarch                                                                                                                                                                      1/1
warning: /etc/yum.repos.d/epel.repo created as /etc/yum.repos.d/epel.repo.rpmnew
  Verifying  : epel-release-7-12.noarch                                                                                                                                                                      1/1

Installed:
  epel-release.noarch 0:7-12

Complete!

最后更新于