升级glibc的时候根据INSTALL升级说明,要求python版本3.4以上。所以需要对python版本进行升级。centos7默认安装的python版本为2.7.5,python通过yum安装也只能安装2.7.5版本,python3可以通过yum安装3.6.8版本。如果需要更高的版本,只能通过源码编译安装。此博文介绍源码安装方式升级python和python3版本,源码编译安装要求操作系统已经安装了gcc。环境说明如下:
[root@s143 ~]# python -V
Python 2.7.5
[root@s143 ~]# yum install -y python2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package python-2.7.5-90.el7.x86_64 already installed and latest version
Nothing to do
[root@s143 ~]# yum install -y python3
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed:
python3.x86_64 0:3.6.8-18.el7
Dependency Installed:
libtirpc.x86_64 0:0.2.4-0.16.el7 python3-libs.x86_64 0:3.6.8-18.el7 python3-pip.noarch 0:9.0.3-8.el7 python3-setuptools.noarch 0:39.2.0-10.el7
Complete!
python官网(包括python和python3所有的版本)查找需要安装或者升级的版本。
[root@s143 opt]# wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
[root@s143 opt]# tar -zxvf Python-2.7.18.tgz
[root@s143 opt]# cd Python-2.7.18
[root@s143 Python-2.7.18]# ./configure --prefix=/usr
[root@s143 Python-2.7.18]# make
…
running build_scripts
creating build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/pydoc -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/idle -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/2to3 -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Lib/smtpd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/pydoc from 644 to 755
changing mode of build/scripts-2.7/idle from 644 to 755
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
[root@s143 Python-2.7.18]# make install
[root@s143 Python-2.7.18]# python -V
Python 2.7.18
[root@s143 opt]# wget https://www.python.org/ftp/python/3.8.8/Python-3.8.8.tgz
[root@s143 opt]# tar -zxvf Python-3.8.8.tgz
注意在编译结束后会有提示"If you want a release build with all stable optimizations active (PGO, etc),please run ./configure --enable-optimizations",加上–enable-optimizations预编译的话后续编译会报错“Could not import runpy module ”,原因是gcc版本太低,enable-optimizations参数要求gcc版本8.1.0以上。
[root@s143 opt]# cd Python-3.8.8
[root@s143 Python-3.8.8]# ./configure
[root@s143 Python-3.8.8]# make
…
if test `uname -s` = Darwin; then
cp python-config.py python-config;
fi
[root@s143 Python-3.8.8]# make install
…
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2
[root@s143 Python-3.6.10]# python3 -V
Python 3.8.8