爛泥:python2.7和python3.5源碼安裝

本文由ilanniweb提供友情贊助,首發於爛泥行天下python

想要得到更多的文章,能夠關注個人微信ilanniwebweb

前幾天在centos6.6安裝ansible時,一直提示python版本不對,致使不能安裝。只能手工進行手工安裝python,爲了之後再也不添這個坑,特記錄相關的安裝過程。sql

本文以python2.7和python3.5安裝爲例。vim

1、安裝python2.7centos

在安裝python以前,咱們先來安裝在進行python編譯時,所須要的的各類依賴軟件包,以下:微信

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

clip_image001

依賴軟件包安裝完畢後,咱們來下載pytho2.7源碼包,並進行相關的操做,以下:ide

wget http://mirrors.sohu.com/python/2.7.12/Python-2.7.12.tar.xzui

tar -xf Python-2.7.12.tar.xz3d

cd Python-2.7.12

./configure --enable-shared --enable-loadable-sqlite-extensions --with-zlib

clip_image002

clip_image003

配置完畢後,開始進行編譯和安裝,以下:

make && make install

clip_image004

若是安裝沒有正確錯誤的話,正確顯示以下:

clip_image005

2、修改系統默認python版本

centos6系列默認的python版本爲python2.6.6,若是咱們要修改系統默認的python版本的話,須要進行以下操做。

執行如下命令,對python2.6進行備份,而後爲python2.7建立軟連接,以下:

mv /usr/bin/python /usr/bin/python2.6.6

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

此時,咱們能夠執行python –V命令,若是出現以下錯誤:

python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

clip_image006

須要編輯配置文件/etc/ld.so.conf添加新的一行內容/usr/local/lib,保存退出,以下:

vim /etc/ld.so.conf

include ld.so.conf.d/*.conf

/usr/local/lib

clip_image007

ld.so.conf文件配置完畢後,咱們須要執行以下命令:

/sbin/ldconfig

/sbin/ldconfig -v

clip_image008

而後再執行python –V命令,查看python版本以下:

clip_image005[1]

經過上圖能夠看到系統默認的python版本已經被修改成python2.7。

3、修改yum相關配置

python2.7安裝完畢,因爲版本的緣由可能會致使yum命令不能使用,就須要咱們進行相關的配置。

咱們須要先把python2.6備份起來,而後再對yum的配置文件進行修改。若是不進行這一步操做的話,執行yum命令將會提示你python的版本不對。

clip_image009

而後再編輯/usr/bin/yum文件,將第一行的#!/usr/bin/python修改成#!/usr/bin/python2.6.6,以下:

vim /usr/bin/yum

#!/usr/bin/python2.6.6

clip_image010

如今執行yum命令已經不會出現錯誤信息了。

4、python3.5安裝命令

在以上三個章節中,咱們介紹了python2.7的安裝,這章咱們把python3.5安裝命令貼出來,就不進行詳細的介紹了。

安裝相關依賴:

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

安裝python3:

wget http://mirrors.sohu.com/python/3.5.2/Python-3.5.2.tar.xz

tar -xf Python-3.5.2.tar.xz

cd Python-3.5.2

./configure --enable-shared --enable-loadable-sqlite-extensions --with-zlib

make && make install

修改系統的默認python版本:

mv /usr/bin/python /usr/bin/python2.6.6

ln -s /usr/local/bin/python3 /usr/bin/python

vim /etc/ld.so.conf

/usr/local/lib

include ld.so.conf.d/*.conf

/sbin/ldconfig

python –V

修改yum使用的python版本:

vim /usr/bin/yum

#!/usr/bin/python2.6.6

相關文章
相關標籤/搜索