python2.7.14的源码安装及脚本安装

Zss 发表于:

之前将依赖python的yum弄坏了,yum依赖特定版本的python,所以最好不要动自带的python版本,若需要其他的版本的话,选择源码安装其他的版本

源码的安装比较麻烦点,当整个 过程熟悉后写成一个脚本就会变的很方便了,其中包括一些环境变量的设置和安装位置和pip的安装还有第三方库的安装

需要先确定依赖包是否安装好了,编译可能会缺少各种依赖,真的头疼,这只是一部分的

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make xz wget gcc make gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel

yum -y install xz wget gcc make gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel

 

1.首先需要下载到源码文件,习惯了用2.7.14版本,那么下载2.7.14(https://www.python.org/downloads/release/python-2714/)

wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz

会将其文件下载到当前文件夹,文件名为默认下载的文件名

 

2.解压文件

tar-zvxf Python-2.7.14.tgz

 

3.创建一个安装目录来安装python

mkdir /usr/bin/python2.7.14

 

4.编译文件(–enable-optimizations 为最优安装,建议使用这个参数。–prefix 声明安装路径)

./configure –enable-optimizations –prefix=/usr/local/python2.7.14/

编译失败可能缺少gcc  那么需要先安装gcc

 

5.在python文件中编译并安装

    make;make install

 

6.添加环境变量

echo ‘PATH=$PATH:/usr/bin/python2.7.14/bin’ >> /etc/profile
echo ‘export PATH’ >> /etc/profile

source /etc/profile  使其生效

 

7.修改默认的python版本,及yum的版本修改

mv /usr/bin/python /usr/bin/python2.7.5
sed -i ‘s/#!\/usr\/bin\/python/#!\/usr\/bin\/python2.7.5/g’ /usr/bin/yum
ln -s /usr/bin/python2.7.14 /usr/bin/python

 

按流程来写成一个自动安装的脚本

#!/bin/sh
function main()
{
echo ‘正在检测网络是否正常。。。’
result=`ping www.baidu.com -c 2|grep ‘ttl’`
if [ ! “${result}” ];then
echo ‘网络异常,请检测网络!’
else
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make xz wget gcc make gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel
echo ‘网络正常! 准备下载python源码’
wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
tar -zvxf Python-2.7.14.tgz
mkdir /usr/bin/python2.7.14
cd Python-2.7.14;./configure –enable-optimizations –prefix=/usr/bin/python2.7.14/
make;make install
echo ‘PATH=$PATH:/usr/bin/python2.7.14/bin’ >> /etc/profile
echo ‘export PATH’ >> /etc/profile
source /etc/profile
mv /usr/bin/python /usr/bin/python2.7.5
sed -i ‘s/#!\/usr\/bin\/python/#!\/usr\/bin\/python2.7.5/g’ /usr/bin/yum
ln -s /usr/bin/python2.7.14 /usr/bin/python
python
fi

}
main