🙊
关于作者
  • 个人简历
  • 联系作者
  • 运维日常记录
    • RAID 磁盘阵列
    • MBR 与 GPT 分区
    • Linux 命令行判断GPT和MBR分区
    • CentoS 7 系统 扩容根分区
  • Linux运维学习笔记
    • 计算机网络基础知识
    • Linux 系统启动过程
    • Linux 命令基本格式
    • Linux 文件删除原理
    • Linux 目录结构说明
    • Linux 查看命令帮助信息
    • Linux Yum 命令
    • Linux Apt 命令
  • Linux 运维管理
    • Linux 硬件管理
    • Linux 软件管理
    • Linux 磁盘管理
    • Linux 系统管理
    • Linux 网络管理
    • Linux 用户和组管理
    • Linux 文件与目录管理
    • Linux 文件压缩与解压缩管理
    • Linux SSL证书自动更新管理
  • 运维环境搭建
    • CentOS 7 安装与优化
    • CentOS 安装高版本Node.js
    • CentOS 升级安装Python2.7.X
    • CentOS 安装 Python3.8.X
    • CentOS 安装 PHP7.4.X
    • CentOS 安装 Mysql 8.0
    • CentOS 安装 Zabbix 5.0
    • Windows Server 部署 IIS
    • Cronsun 任务管理器部署
    • Teltport 堡垒机部署
    • Jump Server 堡垒机搭建及使用
    • CI & CD 持续集成部署
    • ELK6.5.0+Filebeat 日志系统部署
    • Lustre 分布式并行文件系统部署
  • 系统安全加固
    • Linux 操作系统加固
    • Windows 操作系统安全加固
    • Password 安全加固
    • OpenSSL 安全加固
    • NFS 服务安全加固
    • Rsync 服务安全加固
    • IIS 服务安全加固
    • PHP 语言环境安全加固
    • Apache 服务安全加固
    • Nginx 服务安全加固
    • Tomcat 服务安全加固
    • MySQL 服务安全加固
    • PostgreSQL 服务安全加固
    • Redis 服务安全加固
    • MongoDB 服务安全加固
    • 暴力破解攻击和防御
  • 云原生运维教程
    • Docker 理论
    • Docker 基本架构
    • Docker 基本概念
    • Docker 基本使用
      • 容器
      • 镜像
      • 仓库
    • Docker 存储
    • Docker 网络
    • Docker 安装
  • Linux 系统故障排查
    • Linux 系统重置密码方法
    • Linux 系统误操作修改目录权限为 777 修复方法
  • Windows 系统故障排查
    • Windows 和 Windows Server 中启用/禁用 SMBv1、SMBv2 和 SMBv3
    • Windows10建立映射网络驱动器报错,无法挂载共享文件系统,解决办法
  • 运维工具使用
    • 常用 Git 命令简介及使用
    • 常用 SVN 命令简介及使用
    • 常用 Vi / Vim 文本编辑工具简介及使用
    • 国内常用加速源使用及配置
    • 软碟通制作U启动和再生龙恢复Linux系统及备份
    • 常用JetBrains系列IDE快捷键
  • 中间件教程学习
    • Nginx 极简教程
    • Nginx 安装
    • Nginx 配置
    • Nginx 问题集
    • Mysql 教程
    • Mysql 运维
    • Mysql 原理
    • Redis 教程
    • Redis 持久化
    • Redis 复制
    • Redis 哨兵
    • Redis 集群
    • Redis 运维
    • PostgreSQL 教程
    • H2 教程
    • SqLite 教程
    • 数据库中间件 flyway
  • Nginx 入门教程
  • MySQL 入门教程
  • Nosql 数据库
  • 常用工具快捷键
    • Windows10常用快捷键大全
  • Group 1
由 GitBook 提供支持
在本页
  • 第一步:查看Centos版本及python版本
  • 第二步:从官网下载python对应版本的包(以2.7.9版本为例)
  • 第三步:解压、配置、编译、安装python2.7.9
  • 第四步:安装后环境检查
  • 第五步:设置yum(系统预装的yum引用的老版本python)

这有帮助吗?

  1. 运维环境搭建

CentOS 升级安装Python2.7.X

上一页CentOS 安装高版本Node.js下一页CentOS 安装 Python3.8.X

最后更新于5年前

这有帮助吗?

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

第一步:查看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!
点击
博客作者