参考:https://www.jianshu.com/p/fad3942fc0ed
第一步:查看Centos版本及Python版本
• CentOS版本
[root@192 tools_package]# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core)
• Python版本
[root@192 tools_package]# python -V
Python 2.7.5
[root@192 tools_package]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root 7 Nov 14 18:14 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root 9 Nov 14 18:14 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug 4 08:40 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 Aug 4 08:39 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 Jan 20 15:02 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 Jan 20 15:02 /usr/bin/python-config -> python2-config
第二步:从官网下载Python对应版本的包(以2.7.13为例)
[root@192 tools_package]#wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz # wget 后接python官网对应的链接
第三步:加压、配置、编译、安装Python2.7.13
• 解压
[root@192 tools_package]# tar -zxvf Python-2.7.13.tgz # 解压命令
• 安装gcc(在编译时会依赖)
[root@192 tools_package]# yum install gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel python-devel zlib
• 配置、编译、安装
[root@192 tools_package]# cd Python-2.7.13
[root@192 Python-2.7.13
]# (sudo)./configure --prefix=/usr/local # [配置]指定可执行文件、库文件、配置文件、资源文件的安装路径。若没有权限加sudo
[root@192 Python-2.7.13]# (sudo) make # 编译
[root@192 Python-2.7.13
]# make altinstall # 不要使用make install,否则会覆盖系统自带python
第四步:安装后环境检查
• Python安装后的版本
[root@192 Python-2.7.13
]# python -V # 发现版本还是原版本 Python 2.7.5
• 安装前后的Python对比
[root@192 Python-2.7.13]# ll /usr/bin/python* #系统自带的 lrwxrwxrwx. 1 root root 7 Nov 14 18:14 /usr/bin/python -> python2 lrwxrwxrwx. 1 root root 9 Nov 14 18:14 /usr/bin/python2 -> python2.7 -rwxr-xr-x. 1 root root 7136 Aug 4 08:40 /usr/bin/python2.7 -rwxr-xr-x 1 root root 1835 Aug 4 08:39 /usr/bin/python2.7-config lrwxrwxrwx 1 root root 16 Jan 20 15:02 /usr/bin/python2-config -> python2.7-config lrwxrwxrwx 1 root root 14 Jan 20 15:02 /usr/bin/python-config -> python2-config [root@192 Python-2.7.13]# ll -l /usr/local/bin/python* #手工安装的 -rwxr-xr-x 1 root root 8257360 Jan 20 15:26 /usr/local/bin/python2.7 -rwxr-xr-x 1 root root 1687 Jan 20 15:27 /usr/local/bin/python2.7-config [root@192 Python-2.7.13]#
• 备份旧版本,连接新版本
[root@192 Python-2.7.13]# mv /usr/bin/python /usr/bin/python2.7.5 #mv 文件名 文件名 意思:将源文件改为目标文件名 [root@192 Python-2.7.13]# ll -l /usr/bin/python* lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7 -rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7 lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python2.7.5 -> python2 # 改为2.7.5 -rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config [root@192 Python-2.7.13]# ln -s /usr/local/bin/python2.7 /usr/bin/python # 增加连接 [root@192 Python-2.7.13]# ll -l /usr/bin/python* lrwxrwxrwx 1 root root 24 4月 2 05:08 /usr/bin/python -> /usr/local/bin/python2.7 # 新增的,并指向新安装的python lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7 -rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7 lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python2.7.5 -> python2 -rwxr-xr-x 1 root root 1835 11月 6 00:29 /usr/bin/python2.7-config lrwxrwxrwx 1 root root 16 4月 2 03:27 /usr/bin/python2-config -> python2.7-config lrwxrwxrwx 1 root root 14 4月 2 03:27 /usr/bin/python-config -> python2-config
• 再次检查Python版本
[root@192 Python-2.7.13]# python -V Python 2.7.13
• 若想访问老版本Python(如2.7.5版本)
[root@192 Python-2.7.13]# python2.7.5 -V Python 2.7.5