Centos7升级python到2.7.14

Centos7.4 系统目前默认python环境版本为2.7.5

  • 查看centos版本
[root@compute1 ~]#  cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
  • 查看当前python版本:
[root@compute1 ~]# python -V
Python 2.7.14
  • 安装依赖包:
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-dev
  • 下载解压python2.7.14安装包:
wget  https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
tar xf Python-2.7.14.tar.xz
cd Python-2.7.14
  • 备份旧版本,使用新版本
[root@compute1 ~]# mv /usr/bin/python /usr/bin/python2.7.5
[root@compute1 ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python # 增加软链接
  • 编译安装:
./configure --prefix=/usr/localmy/ --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && sudo make altinstall
#不能make  install,make install 会把老的python版本覆盖掉
  • yum的设置
[root@compute1 ~]# vi /usr/bin/yum
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
[root@compute1 ~]# vi /usr/libexec/urlgrabber-ext-down
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7
  • 安装pip
下载最新版的pip,然后安装

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
whereis pip #查找pip的位置
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/bin/pip2.7 /usr/bin/pip
  • 升级脚本:
#!/bin/bash

path=$(dirname `readlink -f $0`)

yum install -y gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2-devel sqlite-devel python-devel zlib
cd $path/Python-2.7.14
./configure --prefix=/usr/local
make && make altinstall

python -V

current=`python -V |awk '{print $2}'`

mv /usr/bin/python /usr/bin/python$current

ln -s /usr/local/bin/python2.7 /usr/bin/python

#modify yum 
sed -e '1c/#!/usr/bin/python2.7' /usr/bin/yum
sed -e '1c/#!/usr/bin/python2.7' /usr/libexec/urlgrabber-ext-down

cd $path/
python ez_setup.py
python get-pip.py
mv /usr/bin/pip /usr/bin/pip.bak
ln -s /usr/local/bin/pip2.7 /usr/bin/pip

你可能感兴趣的:(Centos7升级python到2.7.14)