CentOS 升级安装Python2.7.X

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

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

  • Centos版本
1
[[email protected] ~]# cat /etc/centos-release
2
CentOS Linux release 7.7.1908 (Core)
Copied!
  • Python版本
1
[[email protected] ~]# python -V
2
Python 2.7.5
3
[[email protected] ~]# which python
4
/usr/bin/python
5
[[email protected] ~]# ll -l /usr/bin/python*
6
lrwxrwxrwx. 1 root root 7 Oct 8 17:14 /usr/bin/python -> python2
7
lrwxrwxrwx. 1 root root 9 Oct 8 17:14 /usr/bin/python2 -> python2.7
8
-rwxr-xr-x. 1 root root 7216 Aug 7 08:52 /usr/bin/python2.7
9
-rwxr-xr-x 1 root root 1835 Aug 7 08:51 /usr/bin/python2.7-config
10
lrwxrwxrwx 1 root root 16 Oct 8 17:20 /usr/bin/python2-config -> python2.7-config
11
lrwxrwxrwx 1 root root 14 Oct 8 17:20 /usr/bin/python-config -> python2-config
Copied!

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

1
[[email protected] ~]# wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9rc1.tgz
2
--2019-12-31 10:58:17-- https://www.python.org/ftp/python/2.7.9/Python-2.7.9rc1.tgz
3
Resolving www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:36::223
4
Connecting to www.python.org (www.python.org)|151.101.108.223|:443... connected.
5
HTTP request sent, awaiting response... 200 OK
6
Length: 16646401 (16M) [application/octet-stream]
7
Saving to: ‘Python-2.7.9rc1.tgz’
8
9
100%[========================================================================================================================================================================>] 16,646,401 132KB/s in 1m 55s
10
11
2019-12-31 11:00:12 (142 KB/s) - ‘Python-2.7.9rc1.tgz’ saved [16646401/16646401]
Copied!

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

  • 安装gcc(在编译时会依赖)
1
[[email protected] ~]# yum install -y gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel python-devel zlib
Copied!
  • 配置、编译、安装
1
[[email protected] ~]# tar -zvxf Python-2.7.9rc1.tgz -C /usr/local/
2
[[email protected] ~]# cd /usr/local/Python-2.7.9rc1/
3
[[email protected] Python-2.7.9rc1]# ./configure --prefix=/usr/local && make
4
[[email protected] Python-2.7.9rc1]# make altinstall # 不要使用make install,否则会覆盖系统自带python
Copied!

第四步:安装后环境检查

  • python安装后的版本
1
[[email protected] ~]# python -V # 发现版本还是原版本
2
Python 2.7.5
Copied!
  • 安装前后的python对比
1
[[email protected] ~]# ll -l /usr/bin/python* # 系统自带的
2
lrwxrwxrwx. 1 root root 7 Oct 8 17:14 /usr/bin/python -> python2
3
lrwxrwxrwx. 1 root root 9 Oct 8 17:14 /usr/bin/python2 -> python2.7
4
-rwxr-xr-x. 1 root root 7216 Aug 7 08:52 /usr/bin/python2.7
5
-rwxr-xr-x 1 root root 1835 Aug 7 08:51 /usr/bin/python2.7-config
6
lrwxrwxrwx 1 root root 16 Oct 8 17:20 /usr/bin/python2-config -> python2.7-config
7
lrwxrwxrwx 1 root root 14 Oct 8 17:20 /usr/bin/python-config -> python2-config
8
[[email protected] ~]# ll -l /usr/local/bin/python* # 手工安装的
9
-rwxr-xr-x 1 root root 8205896 Dec 31 11:18 /usr/local/bin/python2.7
10
-rwxr-xr-x 1 root root 1687 Dec 31 11:18 /usr/local/bin/python2.7-config
Copied!
  • 备份旧版本,连接新版本
1
[[email protected] ~]# mv /usr/bin/python /usr/bin/python2.7.5
2
[[email protected] ~]# ll -l /usr/bin/python*
3
lrwxrwxrwx. 1 root root 9 Oct 8 17:14 /usr/bin/python2 -> python2.7
4
-rwxr-xr-x. 1 root root 7216 Aug 7 08:52 /usr/bin/python2.7
5
lrwxrwxrwx. 1 root root 7 Oct 8 17:14 /usr/bin/python2.7.5 -> python2
6
-rwxr-xr-x 1 root root 1835 Aug 7 08:51 /usr/bin/python2.7-config
7
lrwxrwxrwx 1 root root 16 Oct 8 17:20 /usr/bin/python2-config -> python2.7-config
8
lrwxrwxrwx 1 root root 14 Oct 8 17:20 /usr/bin/python-config -> python2-config
9
[[email protected] ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python
10
[[email protected] ~]# ll -l /usr/bin/python*
11
lrwxrwxrwx 1 root root 24 Dec 31 11:32 /usr/bin/python -> /usr/local/bin/python2.7
12
lrwxrwxrwx. 1 root root 9 Oct 8 17:14 /usr/bin/python2 -> python2.7
13
-rwxr-xr-x. 1 root root 7216 Aug 7 08:52 /usr/bin/python2.7
14
lrwxrwxrwx. 1 root root 7 Oct 8 17:14 /usr/bin/python2.7.5 -> python2
15
-rwxr-xr-x 1 root root 1835 Aug 7 08:51 /usr/bin/python2.7-config
16
lrwxrwxrwx 1 root root 16 Oct 8 17:20 /usr/bin/python2-config -> python2.7-config
17
lrwxrwxrwx 1 root root 14 Oct 8 17:20 /usr/bin/python-config -> python2-config
Copied!
  • 再次检查python版本
1
[[email protected] ~]# python -V
2
Python 2.7.9rc1
Copied!
  • 若想访问老版本python(如2.7.5版本)
1
[[email protected] ~]# python2.7.5 -V
2
Python 2.7.5
Copied!
  • 其他python访问,比如python2、python2.7
1
[[email protected] ~]# python2 -V
2
Python 2.7.5
3
[[email protected] ~]# python2.7 -V
4
Python 2.7.9rc1
Copied!

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

1
[[email protected] ~]# yum -y install epel-release #yum这个时候是报错的
2
There was a problem importing one of the Python modules
3
required to run yum. The error leading to this problem was:
4
5
No module named yum
6
7
Please install a package which provides this module, or
8
verify that the module is installed correctly.
9
10
It's possible that the above module doesn't match the
11
current version of Python, which is:
12
2.7.9rc1 (default, Dec 31 2019, 11:15:01)
13
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
14
15
If you cannot solve this problem yourself, please go to
16
the yum faq at:
17
http://yum.baseurl.org/wiki/Faq
Copied!
  • 更改设置
1
[[email protected] ~]# which yum
2
/usr/bin/yum
3
[[email protected] ~]# vim /usr/bin/yum
4
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
5
[[email protected] ~]# vim /usr/libexec/urlgrabber-ext-down
6
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
Copied!
  • 测试yum是否可用
1
[[email protected] ~]# yum -y install epel-release
2
Loaded plugins: fastestmirror
3
Loading mirror speeds from cached hostfile
4
Resolving Dependencies
5
--> Running transaction check
6
---> Package epel-release.noarch 0:7-12 will be installed
7
--> Finished Dependency Resolution
8
9
Dependencies Resolved
10
11
=================================================================================================================================================================================================================
12
Package Arch Version Repository Size
13
=================================================================================================================================================================================================================
14
Installing:
15
epel-release noarch 7-12 epel 15 k
16
17
Transaction Summary
18
=================================================================================================================================================================================================================
19
Install 1 Package
20
21
Total download size: 15 k
22
Installed size: 24 k
23
Downloading packages:
24
epel-release-7-12.noarch.rpm | 15 kB 00:00:00
25
Running transaction check
26
Running transaction test
27
Transaction test succeeded
28
Running transaction
29
Installing : epel-release-7-12.noarch 1/1
30
warning: /etc/yum.repos.d/epel.repo created as /etc/yum.repos.d/epel.repo.rpmnew
31
Verifying : epel-release-7-12.noarch 1/1
32
33
Installed:
34
epel-release.noarch 0:7-12
35
36
Complete!
Copied!