Cent OS 7.0离线升级python2.7至3.6

Posted wjlv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cent OS 7.0离线升级python2.7至3.6相关的知识,希望对你有一定的参考价值。

先看下系统信息:

[root@agent42 ~]# more /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
[root@agent42 ~]# python -V
Python 2.7.5
[root@agent42 ~]#

步骤一:源码下载。离线安装,当然先下载离线源码,可以从网上找或者官网下载,我这里使用的是3.6.4:https://www.python.org/ftp/python/3.6.4/

步骤二:上传解压编译安装

# 解压
[root@agent42 ~]# tar -zxvf Python-3.6.4.tgz
# 创建安装目录
[root@agent42 ~]# mkdir /usr/local/python3
# 编译安装
[root@agent42 Python-3.6.4]# ./configure --prefix=/usr/local/python3

报错1,缺少C环境(因为最小化安装,缺少各种开发环境依赖的包):

configure: error: in `/root/Python-3.6.4:
configure: error: no acceptable C compiler found in $PATH

步骤三:挂载镜像,安装GCC相关环境

挂载镜像:

[root@agent42 opt]# mount -o loop CentOS-7.0-1406-x86_64-DVD.iso /opt/cdrom/
mount: /dev/loop0 写保护,将以只读方式挂载

配置yum源:

# 配置yum源,备份原始repo
[root@agent42 opt]# cd /etc/yum.repos.d/
[root@agent42 yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Sources.repo  CentOS-Vault.repo
[root@agent42 yum.repos.d]# mkdir bak
[root@agent42 yum.repos.d]# mv *repo bak/
# 新建repo
[root@agent42 yum.repos.d]# more my.repo
[base]
name = RedHat iso
baseurl = file:///opt/cdrom
enable = 1
gpgcheck = 0
gpgkey = file:///opt/cdrom/RPM-GPG-KEY-CentOS-7
[root@agent42 yum.repos.d]#

清空yum缓存目录:

[root@agent42 yum.repos.d]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base
Cleaning up everything
[root@agent42 yum.repos.d]# 

生成yum源数据:

[root@agent42 yum.repos.d]# yum makecache
已加载插件:fastestmirror
base                                                                                                       | 3.6 kB  00:00:00
(1/4): base/group_gz                                                                                       | 157 kB  00:00:00
(2/4): base/primary_db                                                                                     | 2.7 MB  00:00:00
(3/4): base/filelists_db                                                                                   | 2.8 MB  00:00:00
(4/4): base/other_db                                                                                       | 1.1 MB  00:00:00
Determining fastest mirrors
元数据缓存已建立
[root@agent42 yum.repos.d]# 

至此本地yum源配置完毕。。。

步骤四:安装gcc相关环境

[root@agent42 yum.repos.d]# yum install gcc
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 gcc.x86_64.0.4.8.2-16.el7 将被 安装
--> 正在处理依赖关系 cpp = 4.8.2-16.el7,它被软件包 gcc-4.8.2-16.el7.x86_64 需要


……中间省略…… 总下载量:25 M 安装大小:59 M Is this ok [y/d/N]: y Downloading packages: ---------------------------------------------------------------------------------------------------------------------------------- 总计 175 MB/s | 25 MB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : mpfr-3.1.1-4.el7.x86_64 1/7 正在安装 : libmpc-1.0.1-3.el7.x86_64 2/7 正在安装 : cpp-4.8.2-16.el7.x86_64 3/7 正在安装 : kernel-headers-3.10.0-123.el7.x86_64 4/7 正在安装 : glibc-headers-2.17-55.el7.x86_64 5/7 正在安装 : glibc-devel-2.17-55.el7.x86_64 6/7 正在安装 : gcc-4.8.2-16.el7.x86_64 7/7 验证中 : mpfr-3.1.1-4.el7.x86_64 1/7 验证中 : glibc-headers-2.17-55.el7.x86_64 2/7 验证中 : glibc-devel-2.17-55.el7.x86_64 3/7 验证中 : libmpc-1.0.1-3.el7.x86_64 4/7 验证中 : kernel-headers-3.10.0-123.el7.x86_64 5/7 验证中 : gcc-4.8.2-16.el7.x86_64 6/7 验证中 : cpp-4.8.2-16.el7.x86_64 7/7 已安装: gcc.x86_64 0:4.8.2-16.el7 作为依赖被安装: cpp.x86_64 0:4.8.2-16.el7 glibc-devel.x86_64 0:2.17-55.el7 glibc-headers.x86_64 0:2.17-55.el7 kernel-headers.x86_64 0:3.10.0-123.el7 libmpc.x86_64 0:1.0.1-3.el7 mpfr.x86_64 0:3.1.1-4.el7 完毕! [root@agent42 yum.repos.d]#

环境问题搞定。。。

继续编译python:

[root@agent42 Python-3.6.4]# ./configure --prefix=/usr/local/python3
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.6... no
checking for python3... no
checking for python... python
checking for --enable-universalsdk... no
checking for --with-universal-archs... no

……中间省略……
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations

[root@agent42 Python-3.6.4]#
 

安装python:

# 安装
[root@agent42 Python-3.6.4]# make && make install

再次报错,缺少zlib相关包:

    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/root/Python-3.6.4/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip
zipimport.ZipImportError: cant decompress data; zlib not available
make: *** [install] 错误 1
[root@agent42 Python-3.6.4]# 

继续安装即可(本地yum源的优势出来了)

[root@agent42 Python-3.6.4]# yum install zlib*
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
软件包 zlib-1.2.7-13.el7.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
---> 软件包 zlib-devel.x86_64.0.1.2.7-13.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==================================================================================================================================
 Package                         架构                        版本                                 源                         大小
==================================================================================================================================
正在安装:
 zlib-devel                      x86_64                      1.2.7-13.el7                         base                       49 k

事务概要
==================================================================================================================================
安装  1 软件包

总下载量:49 k
安装大小:132 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : zlib-devel-1.2.7-13.el7.x86_64                                                                                1/1
  验证中      : zlib-devel-1.2.7-13.el7.x86_64                                                                                1/1

已安装:
  zlib-devel.x86_64 0:1.2.7-13.el7

完毕!
[root@agent42 Python-3.6.4]#

再次安装python:


# 安装
[root@agent42 Python-3.6.4]# make && make install
……中间省略……

Collecting setuptools Collecting pip Installing collected packages: setuptools, pip Successfully installed pip
-9.0.1 setuptools-28.8.0 [root@agent42 Python-3.6.4]#

步骤五:修改默认python为python3.6,同时创建软链

[root@agent42 Python-3.6.4]# mv /usr/bin/python /usr/bin/python.bak
[root@agent42 Python-3.6.4]# ln -s /usr/local/python3/bin/python3 /usr/bin/python
[root@agent42 Python-3.6.4]#

步骤六:恢复yum使用的原python配置(因为软链改成了最新的,所以把最开始引用的python改为python2.7):

[root@agent42 Python-3.6.4]# more /usr/bin/yum
#!/usr/bin/python2.7
[root@agent42 Python-3.6.4]# more /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python2.7

 

以上是关于Cent OS 7.0离线升级python2.7至3.6的主要内容,如果未能解决你的问题,请参考以下文章

CentOS6.5自带Python2.6.6升级至Python2.7

cent os官网上下系统有下个选项 dvd ios everything ios minimal

python2.7.X 升级至Python3.6.X

python2.7.X 升级至Python3.6.X

升级centos6自带的python2.6版本至python2.75

Mac OS X10.12.4安装的Python2.7升级Python3.5步骤详解