centos6升级Python

工作中的系统一般都是centos6,并且经常需要升级Python,所以整理网络上的资料,用此博文来备忘。

检查当前Python版本

python -V

返回的结果一般是:

python 2.6.6

升级系统并安装相关开发者工具

yum -y update
yum groupinstall -y 'development tools'

安装必须的组件

yum install -y zlib-devel bzip2-devel openssl-devel sqlite-devel ncurses-devel xz-libs wget

下载并解压Python

wget http://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar -zxvf Python-2.7.13.tgz
cd Python-2.7.13

开始安装Python

./configure
make
make install

设置软连接和系统环境变量

mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/bin/python2.7 /usr/bin/python
export PATH="/usr/local/bin:$PATH"

修复yum

sed -i '1s/python/python2.6/g' /usr/bin/yum

安装pip

wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
python get-pip.py

自己写的一键脚本:
update-Python

脚本代码如下:

#!/usr/bin/env bash

# Check if user is root
[ $(id -u) != "0" ] && { echo -e "\033[31mError: You must be root to run this script\033[0m"; exit 1; } 
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
#install system update
yum -y update
yum groupinstall -y 'development tools'
#install depends on
yum install -y zlib-devel bzip2-devel openssl-devel sqlite-devel ncurses-devel xz-libs wget
# download python2.7.13
wget http://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
tar -zxvf Python-2.7.13.tgz
cd Python-2.7.13
#install python
./configure --prefix=/usr/local
make
make install
if [ $? -eq 0 ];then
     echo "Python2.7.13 update success"
else
     echo "Python2.7.13 update faild"
fi
cd ..
rm -rf Python-2.7.13 Python-2.7.13.tgz
#set Soft link & path
cd
mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/bin/python2.7 /usr/bin/python
export PATH="/usr/local/bin:$PATH"
#repair yum
sed -i '1s/python/python2.6/g' /usr/bin/yum
#install pip
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
python get-pip.py
if [ $? -eq 0 ];then
     echo "pip update success"
else
     echo "pip update faild"
fi

此处评论已关闭