Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

CentOS 6 源碼安裝 Python 2.7 與 Python 3.5

CentOS 6 自帶 Python 2.6,若安裝 Ansible 或新版工具常需自行編譯 Python。本文以 2.7 與 3.5 為例。

安裝依賴與編譯

yum -y install xz wget gcc make gdbm-devel openssl-devel sqlite-devel zlib-devel bzip2-devel
wget https://mirrors.sohu.com/python/2.7.12/Python-2.7.12.tar.xz
tar -xf Python-2.7.12.tar.xz
cd Python-2.7.12
./configure --enable-shared --enable-loadable-sqlite-extensions --with-zlib
make
make install
CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5

切換預設版本

mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/bin/python2.7 /usr/bin/python
vim /etc/ld.so.conf
/usr/local/lib
/sbin/ldconfig
python -V
CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5

修正 yum

由於 yum 依賴舊版 Python,需把 /usr/bin/yum 第一行改為:

#!/usr/bin/python2.6.6
CentOS 6 源碼安裝 Python 2.7 與 Python 3.5CentOS 6 源碼安裝 Python 2.7 與 Python 3.5

Python 3.5

wget https://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,也需同步調整 /usr/bin/yum 的 shebang,避免 yum 失效。

Copyright © Linux教程網 All Rights Reserved