在CentOS6上编译安装Python2.7

参考了友站的博文CentOS6上的Python2.7问题,正如所言,在CentOS6上安装Python2.7是非常头疼的问题。友站的这篇博文阐述了如何了从源安装Python2.7,本站则讲述从源码编译安装要注意的问题。
编译依赖参考How to install Python 2.7 and Python 3.3 on CentOS 6

依赖

十分重要: 编译Python2.7之前务必安装齐必须依赖。在configure过程中,若缺少依赖则不会报错,编译也可顺利通过,但编译出的Python将缺少几个必要模块,导致在运行ez_setup.py时出错。

1
2
# yum groupinstall "Development tools"
# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

编译和安装

1
2
3
4
5
$ wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
$ tar xf Python-2.7.6.tar.xz
$ cd Python-2.7.6
$ ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# make && make altinstall

这将会把Python2.7安装在/usr/local/bin/python2.7

将默认Python版本从2.6改为2.7

首先将/usr/bin/python这个软链接指向刚刚安装的Python2.7

1
2
# rm /usr/bin/python
# ln -s /usr/local/bin/python2.7 /usr/bin/python

重要: 进行这步操作后,yum会失效,运行即报错。这是因为/usr/bin/yum其实是个python2.6脚本,刚刚安装的python2.7缺少yum的相关依赖。因此需要改动/usr/bin/yum的解释器。

1
# vim /usr/bin/yum

将第一行

1
#!/usr/bin/python

改为:

1
#!/usr/bin/python2.6

现在运行yum --version应该不会再报错

安装pip

1
2
3
$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
# python ez_setup.py
# easy_install-2.7 pip

替换默认pip为pip2.7

1
2
3
4
# vim /usr/bin/pip2.6 #第一行改为#!/usr/bin/python2.6
$ which pip2.7 #应该返回/usr/local/bin/pip2.7
# rm /usr/bin/pip
# ln -s /usr/local/bin/pip2.7 /usr/bin/pip