CentOS 6.5安装Python2.7.14
在CentOS 6.5下安装Python2.7.14过程。
查看当前系统中的 Python 版本
python --version
python --version
返回 Python 2.6.6 为正常。
检查 CentOS 版本
cat /etc/RedHat-release
cat /etc/redhat-release
返回 CentOS release 6.5 (Final) 为正常。
安装所有的开发工具包
yum groupinstall -y "Development tools"
yum groupinstall -y "Development tools"
安装其它的必需包
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
下载、编译和安装 Python 2.7.14
wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
tar zxf Python-2.7.14.tgz
cd Python-2.7.14
./configure
make && make install
wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz tar zxf Python-2.7.14.tgz cd Python-2.7.14 ./configure make && make install
默认 Python 2.7.14 会安装在 /usr/local/bin 目录下。
ll -tr /usr/local/bin/python*
/usr/local/bin/python2.7
/usr/local/bin/python2.7-config
/usr/local/bin/python -> python2
/usr/local/bin/python2 -> python2.7
/usr/local/bin/python2-config -> python2.7-config
/usr/local/bin/python-config -> python2-config
ll -tr /usr/local/bin/python* /usr/local/bin/python2.7 /usr/local/bin/python2.7-config /usr/local/bin/python -> python2 /usr/local/bin/python2 -> python2.7 /usr/local/bin/python2-config -> python2.7-config /usr/local/bin/python-config -> python2-config
而系统自带的 Python 是在 /usr/bin 目录下。
ll -tr /usr/bin/python*
/usr/bin/python2.6-config
/usr/bin/python2.6
/usr/bin/python
/usr/bin/python2 -> python
/usr/bin/python-config -> python2.6-config
ll -tr /usr/bin/python* /usr/bin/python2.6-config /usr/bin/python2.6 /usr/bin/python /usr/bin/python2 -> python /usr/bin/python-config -> python2.6-config
更新系统默认 Python 版本
先把系统默认的旧版 Python 重命名。
mv /usr/bin/python /usr/bin/python.old
mv /usr/bin/python /usr/bin/python.old
再删除系统默认的 python-config 软链接。]
rm -f /usr/bin/python-config
rm -f /usr/bin/python-config
最后创建新版本的 Python 软链接。
ln -s /usr/local/bin/python /usr/bin/python
ln -s /usr/local/bin/python-config /usr/bin/python-config
ln -s /usr/local/include/python2.7/ /usr/include/python2.7
ln -s /usr/local/bin/python /usr/bin/python ln -s /usr/local/bin/python-config /usr/bin/python-config ln -s /usr/local/include/python2.7/ /usr/include/python2.7
以上步骤做完以后,目录 /usr/bin 下的 Python 应该是
ll -tr /usr/bin/python*
/usr/bin/python2.6-config
/usr/bin/python2.6
/usr/bin/python.old
/usr/bin/python2 -> python
/usr/bin/python -> /usr/local/bin/python
/usr/bin/python-config -> /usr/local/bin/python-config
ll -tr /usr/bin/python* /usr/bin/python2.6-config /usr/bin/python2.6 /usr/bin/python.old /usr/bin/python2 -> python /usr/bin/python -> /usr/local/bin/python /usr/bin/python-config -> /usr/local/bin/python-config
查看新的 Python 版本
python --version
python --version
返回 Python 2.7.14 为正常。
以下步骤还是有必要的
为新版 Python 安装 setuptools
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
上述方式异常时,直接输入
https://pypi.io/packages/source/s/setuptools/setuptools-33.1.1.zip
https://pypi.io/packages/source/s/setuptools/setuptools-33.1.1.zip
下载解压后进行python install setup.py进行安装
setuptools 正确安装完成后,easy_install 命令就会被安装在 /usr/local/bin 目录下了。
为新版 Python 安装 pip
easy_install pip
easy_install pip
正确安装完成后,pip 命令就会被安装在 /usr/local/bin 目录下了。
为新版 Python 安装 distribute 包(可选)
pip install distribute
pip install distribute
至此,新版 Python 即算安装完毕了。
注意:这可能会导致以前安装过的 Python 程序运行不了或者无法重启之类的(比如著名的 Shadowsocks Python 版)。原因是旧版的 pkg_resources 位于 /usr/lib/python2.6/site-packages 下。而新版的则是在 /usr/local/lib/python2.7/site-packages 下。
所以,我们需要将python2.6下site-packages目录中的文件拷贝到python2.7/site-packages下。
cp -r /usr/lib/python2.6/site-packages/yum /usr/local/lib/python2.7/site-packages/
cp -r /usr/lib/python2.6/site-packages/rpmUtils /usr/local/lib/python2.7/site-packages/
cp -r /usr/lib/python2.6/site-packages/iniparse /usr/local/lib/python2.7/site-packages/
cp -r /usr/lib/python2.6/site-packages/urlgrabber /usr/local/lib/python2.7/site-packages/
cp -r /usr/lib64/python2.6/site-packages/rpm /usr/local/lib/python2.7/site-packages/
cp -r /usr/lib64/python2.6/site-packages/curl /usr/local/lib/python2.7/site-packages/
cp -p /usr/lib64/python2.6/site-packages/pycurl.so /usr/local/lib/python2.7/site-packages/
cp -p /usr/lib64/python2.6/site-packages/_sqlitecache.so /usr/local/lib/python2.7/site-packages/
cp -p /usr/lib64/python2.6/site-packages/sqlitecachec.py /usr/local/lib/python2.7/site-packages/
cp -p /usr/lib64/python2.6/site-packages/sqlitecachec.pyc /usr/local/lib/python2.7/site-packages/
cp -p /usr/lib64/python2.6/site-packages/sqlitecachec.pyo /usr/local/lib/python2.7/site-packages/