GNU/Linux系統默認安裝有Python,但版本是2.xCentOS 7.3中版本號是2.7.5Debian Jessie中版本號是2.7.9。但當前最新版本是Python 3.6.0,釋出時間為Dec 23, 2016。本人記錄Python 3.6.0在CentOS7.3/Debian Jessie中的下載、編譯安裝、配置過程。

Preparation

Python Official Site

Python信息

Python Site
Official Site https://www.python.org/
Download Page https://www.python.org/downloads/
Document Page https://www.python.org/doc/

System Info

CentOS

item details
OS Version CentOS Linux release 7.3.1611 (Core)
Kernel Version 3.10.0-514.2.2.el7.x86_64
Python2 2.7.5

Debian

item details
OS Version Debian GNU/Linux 8 (jessie)
Kernel Version 3.16.0-4-amd64
Python2 2.7.9

Download & Verification

Python最新釋出版本信息可通過本人寫的Shell Script進行查詢 GitHub

演示過程

[flying@lempstacker ~]$ curl -s https://raw.githubusercontent.com/LempStacker/personalShellScriptCollection/master/shellScripts/checkPythonLatestReleaseVersion.sh | bash
Working Now, Just Be Patient!
Python Latest Source Release Info:
Version 3.6.0, Release Date: 2016-12-23, XZ Download Link: https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz, Download Page: https://www.python.org/downloads/release/python-360/, Release Note: https://docs.python.org/3.6/whatsnew/changelog.html#python-3-6-0;
Version 2.7.13, Release Date: 2016-12-17, XZ Download Link: https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz, Download Page: https://www.python.org/downloads/release/python-2713/, Release Note: https://hg.python.org/cpython/raw-file/v2.7.13/Misc/NEWS;
[flying@lempstacker ~]$

Python 3.6.0下載頁面

https://www.python.org/downloads/release/python-360/

選擇XZ compressed source tarball下載, 下載連接是

https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz

MD5 Sum

82b143ebbf4514d7e05876bed7a6b1f5

此處定義文件保存路徑為~/Downloads

校驗過程

[flying@lempstacker ~]$ md5sum ~/Downloads/Python-3.6.0.tar.xz
82b143ebbf4514d7e05876bed7a6b1f5  /home/flying/Downloads/Python-3.6.0.tar.xz
[flying@lempstacker ~]$ openssl dgst -md5 ~/Downloads/Python-3.6.0.tar.xz
MD5(/home/flying/Downloads/Python-3.6.0.tar.xz)= 82b143ebbf4514d7e05876bed7a6b1f5
[flying@lempstacker ~]$

Prerequisites

在進行編譯操作之前,須先安裝相關依賴包。

重要:pip依賴openssl-devel,若未安裝,則pip無法正常安裝,會出現如下報錯

Ignoring ensurepip failure: pip 8.1.2 requires SSL/TLS

執行如下命令,安裝相關依賴包

# CentOS 7
sudo yum install -y gcc make zlib-devel bzip2-devel openssl-devel ncurese-devel

# Debian
sudo apt-get install -y gcc make build-essential checkinstall zlib1g{,-dev}

# sudo apt-get install -y libssl-dev zlib1g-dev liblzma-dev tk-dev libncurses5-dev libreadline-dev libffi-dev libexpat1-dev libreadline-gplv2-dev libncursesw5-dev libgdbm-dev libc6-dev libbz2-dev

Compile Installation

目錄定義如下

#解壓目錄
/tmp/python360

#安裝目錄
/opt/python360

執行如下命令進行解壓縮

#創建解壓目錄
sudo rm -rf /tmp/python360
sudo mkdir -pv /tmp/python360

#解壓縮至目標目錄
sudo tar xf ~/Downloads/Python-3.6.0.tar.xz -C /tmp/python360 --strip-components=1

查看目錄文件信息

[flying@lempstacker ~]$ ls -lhF /tmp/python360/
total 1016K
-rw-r--r--  1 501 games  11K Dec 23 10:21 aclocal.m4
-rwxr-xr-x  1 501 games  42K Dec 23 10:21 config.guess*
-rwxr-xr-x  1 501 games  35K Dec 23 10:21 config.sub*
-rwxr-xr-x  1 501 games 471K Dec 23 10:21 configure*
-rw-r--r--  1 501 games 155K Dec 23 10:21 configure.ac
drwxr--r-- 18 501 games 4.0K Dec 23 10:23 Doc/
drwxr--r--  2 501 games   20 Dec 23 10:21 Grammar/
drwxr--r--  2 501 games 4.0K Dec 23 10:21 Include/
-rwxr-xr-x  1 501 games 7.0K Dec 23 10:21 install-sh*
drwxr--r-- 33 501 games 8.0K Dec 23 10:21 Lib/
-rw-r--r--  1 501 games  13K Dec 23 10:21 LICENSE
drwxr--r--  8 501 games  151 Dec 23 10:21 Mac/
-rw-r--r--  1 501 games  58K Dec 23 10:21 Makefile.pre.in
drwxr--r--  2 501 games 4.0K Dec 23 10:21 Misc/
drwxr--r-- 13 501 games 4.0K Dec 23 10:21 Modules/
drwxr--r--  4 501 games 4.0K Dec 23 10:21 Objects/
drwxr--r--  2 501 games 4.0K Dec 23 10:21 Parser/
drwxr--r--  5 501 games 4.0K Dec 23 10:21 PC/
drwxr--r--  2 501 games 4.0K Dec 23 10:21 PCbuild/
drwxr--r--  2 501 games   79 Dec 23 10:21 Programs/
-rw-r--r--  1 501 games  41K Dec 23 10:21 pyconfig.h.in
drwxr--r--  3 501 games 4.0K Dec 23 10:21 Python/
-rw-r--r--  1 501 games 8.3K Dec 23 10:21 README
-rw-r--r--  1 501 games  99K Dec 23 10:21 setup.py
drwxr--r-- 24 501 games 4.0K Dec 23 10:21 Tools/
[flying@lempstacker ~]$

可以看到文件默認owner是501、默認group是games

執行./configure --prefix=報錯

configure: error: cannot find sources (Include/object.h) in . or ..

執行如下命令,更改文件owner、group

sudo chown -R $USER:$USER /tmp/python360/

再次執行./configure --prefix=提示

If you want a release build with all optimizations active (LTO, PGO, etc), please run ./configure –enable-optimizations

根據提示信息,添加參數--enable-optimizations

以下為編譯安裝的完整命令

#創建安裝目錄
[[ -d /opt/python360 ]] && sudo rm -rf /opt/python360/
sudo mkdir -pv /opt/python360

#進入解壓目錄
cd /tmp/python360

#編譯安裝
./configure --prefix=/opt/python360 --enable-optimizations
make -j 4
sudo make install
  • --prefix: 指定安裝路徑
  • make -j: 多線程並行編譯

編譯完成後

[flying@lempstacker ~]$ cd /opt/python360/
[flying@lempstacker python360]$ ls -lhF
total 4.0K
drwxr-xr-x 2 root root 4.0K Jan  4 17:44 bin/
drwxr-xr-x 3 root root   23 Jan  4 17:25 include/
drwxr-xr-x 4 root root   60 Jan  4 17:44 lib/
drwxr-xr-x 3 root root   16 Jan  4 17:44 share/
[flying@lempstacker python360]$ tree -L 2
.
├── bin
│   ├── 2to3 -> 2to3-3.6
│   ├── 2to3-3.6
│   ├── easy_install-3.6
│   ├── idle3 -> idle3.6
│   ├── idle3.6
│   ├── pip3
│   ├── pip3.6
│   ├── pydoc3 -> pydoc3.6
│   ├── pydoc3.6
│   ├── python3 -> python3.6
│   ├── python3.6
│   ├── python3.6-config -> python3.6m-config
│   ├── python3.6m
│   ├── python3.6m-config
│   ├── python3-config -> python3.6-config
│   ├── pyvenv -> pyvenv-3.6
│   └── pyvenv-3.6
├── include
│   └── python3.6m
├── lib
│   ├── libpython3.6m.a
│   ├── pkgconfig
│   └── python3.6
└── share
    └── man

8 directories, 18 files
[flying@lempstacker python360]$

導出相關文件 * 導出頭文件: /opt/python360/include * 導出庫文件: /opt/python360/lib * 為可執行程序添加PATH路徑: /opt/python360/bin

export include

導出頭文件到目錄/usr/local/include/

執行如下命令

# 創建符號鏈接
sudo ln -sv /opt/python360/include /usr/local/include/python3

*提醒*: 路徑/usr/local/include/python3根據自己實際情況定義。

操作過程

[flying@lempstacker ~]$ sudo ln -sv /opt/python360/include /usr/local/include/python3
‘/usr/local/include/python3’ -> ‘/opt/python360/include’
[flying@lempstacker ~]$ ls -lhF /usr/local/include/python3
lrwxrwxrwx 1 root root 22 Jan  4 17:49 /usr/local/include/python3 -> /opt/python360/include/
[flying@lempstacker ~]$

export libary

導出庫文件到目錄/etc/ld.so.conf.d/

執行如下命令

#方式1
# echo '/opt/python360/lib' | sudo tee /etc/ld.so.conf.d/python3.conf

#方式2
sudo tee /etc/ld.so.conf.d/python3.conf <<-'EOF'
/opt/python360/lib
EOF

#系統重新生成鏈接庫緩存
sudo ldconfig -v

操作過程

[flying@lempstacker ~]$ sudo tee /etc/ld.so.conf.d/python3.conf <<-'EOF'
> /opt/python360/lib
> EOF
/opt/python360/lib
[flying@lempstacker ~]$ cat /etc/ld.so.conf.d/python3.conf
/opt/python360/lib
[flying@lempstacker ~]$ sudo ldconfig -v

export binary

將二進制可執行程序路徑添加至環境變量$PATH

執行如下命令

#方式1
# echo 'export PATH=$PATH:/opt/python360/bin' | sudo tee /etc/profile.d/python3.sh

#方式2
sudo tee /etc/profile.d/python3.sh <<-'EOF'
export PATH=$PATH:/opt/python360/bin
EOF

#使設置生效
source /etc/profile.d/python3.sh

操作過程

[flying@lempstacker ~]$ sudo tee /etc/profile.d/python3.sh <<-'EOF'
> export PATH=$PATH:/opt/python360/bin
> EOF
export PATH=$PATH:/opt/python360/bin
[flying@lempstacker ~]$ cat /etc/profile.d/python3.sh
export PATH=$PATH:/opt/python360/bin
[flying@lempstacker ~]$ source /etc/profile.d/python3.sh
[flying@lempstacker ~]$

在bash中查看相關命令

#查看python3相關可執行文件
[flying@lempstacker ~]$ which python3
python3            python3.6-config   python3.6m-config  
python3.6          python3.6m         python3-config     
[flying@lempstacker ~]$ which python3
/opt/python360/bin/python3
[flying@lempstacker ~]$ python3 -V
Python 3.6.0
[flying@lempstacker ~]$

#查看pip相關可執行文件
[flying@lempstacker ~]$ which pip
pip     pip2    pip2.7  pip3    pip3.6  
[flying@lempstacker ~]$ which pip3
pip3    pip3.6  
[flying@lempstacker ~]$ which pip3
/opt/python360/bin/pip3
[flying@lempstacker ~]$ pip3 -V
pip 9.0.1 from /opt/python360/lib/python3.6/site-packages (python 3.6)
[flying@lempstacker ~]$

Simple Test

到此,可通過python3命令進入Python3.5的交互模式,以下是簡單測試

[flying@lempstacker ~]$ python3 -V
Python 3.6.0
[flying@lempstacker ~]$ python3
Python 3.6.0 (default, Jan  4 2017, 17:20:43)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print ('Hello, world! This is LempStacker!')
Hello, world! This is LempStacker!
>>> exit()
[flying@lempstacker ~]$

警告: 以下操作為之前Blog中內容,強行替換會影響其它服務(依賴於Python2)的正常運行,強烈不建議進行。

Merge Python Version

因系統默認版本是Python 2.7.5,YUM使用的也是Python 2.7.5,為方便操作,實現如下命令映射 * python –> python 3.6 * pip –> pip 3.6

現為相關文件創建符號鏈接(軟鏈接),修改yum使用的python路徑執行

[flying@lempstacker ~]$ ls -lh `which --skip-alias python`
lrwxrwxrwx 1 root root 7 Dec 16 08:31 /usr/bin/python -> python2
[flying@lempstacker ~]$ ls -lh `which --skip-alias pip`
-rwxr-xr-x 1 root root 282 Nov 22 02:44 /usr/bin/pip
[flying@lempstacker ~]$ ls -lh `which --skip-alias yum`
-rwxr-xr-x 1 root root 801 Nov 15 23:30 /usr/bin/yum
[flying@lempstacker ~]$
[flying@lempstacker ~]$ which python3
python3            python3.6-config   python3.6m-config  
python3.6          python3.6m         python3-config     
[flying@lempstacker ~]$
[flying@lempstacker ~]$ ls -lh /usr/bin/python*
lrwxrwxrwx 1 root root    7 Dec 16 08:31 /usr/bin/python -> python2
lrwxrwxrwx 1 root root    9 Dec 16 08:31 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7.0K Nov  6 08:29 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1.8K Nov  6 08:29 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root   16 Dec 16 08:34 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root   14 Dec 16 08:34 /usr/bin/python-config -> python2-config
[flying@lempstacker ~]$

可以看到,映射關係是 * /usr/bin/python-config –> /usr/bin/python2-config –> /usr/bin/python2.7-config(真實文件) * /usr/bin/python –> /usr/bin/python2 –> /usr/bin/python2.7(真實文件)

故而只需 * 將/usr/bin/python2映射到/opt/python360/bin/python3.6 * 將/usr/bin/python2-config映射到/opt/python360/bin/python3.6-config

即執行如下命令

#使用參數-f強制移除已經存在的符號鏈接
sudo ln -sfv /opt/python360/bin/python3.6 /usr/bin/python2
sudo ln -sfv /opt/python360/bin/python3.6-config /usr/bin/python2-config

Affected File

其餘使用到Python的文件,操作都是將/usr/bin/python更改為/usr/bin/python2.7

yum

執行如下命令

sudo cp -p /usr/bin/yum{,.bak}
sudo vim /usr/bin/yum

urlgrabber-ext-down

執行如下命令

sudo cp -p /usr/libexec/urlgrabber-ext-down{,.bak}
sudo vim /usr/libexec/urlgrabber-ext-down

tuned

tuned.service服務無法啟動,執行tuned active命令提示模塊導入失敗

[flying@lempstacker log]$ tuned active
Traceback (most recent call last):
  File "/usr/sbin/tuned", line 26, in <module>
    import tuned.logs
ImportError: No module named 'tuned'
[flying@lempstacker log]$

執行如下命令

sudo cp -p /usr/sbin/tuned{,.bak}
sudo vim /usr/sbin/tuned

tuned-adm

tuned.service服務無法啟動,執行tuned-adm active命令提示模塊導入失敗

[flying@lempstacker ~]$ tuned-adm active
Traceback (most recent call last):
  File "/usr/sbin/tuned-adm", line 25, in <module>
    import tuned.admin
ImportError: No module named 'tuned'

執行如下命令

sudo cp -p /usr/sbin/tuned-adm{,.bak}
sudo vim /usr/sbin/tuned-adm

再次執行sudo systemctl start tuned.service,服務正常啟動。

ansible

[flying@lempstacker ~]$ ansible
ansible           ansible-doc       ansible-playbook  ansible-vault
ansible-console   ansible-galaxy    ansible-pull      
[flying@lempstacker ~]$

References

Change Logs

  • 2016.04.12 12:58 Tue Asia/Beijing
    • 初稿完成
  • 2016.04.13 08:08 Wed Asia/Beijing
    • 添加Affected File
  • 2016.04.15 17:04 Fri Asia/Beijing
    • 添加ansible
  • 2016.08.04 21:12 Thu Asia/Shanghai
    • CentOS下添加openssl-devel用以安裝pip
  • 2017.01.04 18:13 Wed Asia/Shanghai
    • 全新安裝Python 3.6.0,Blog內容重構
  • 2017.03.06 10:44 Mon Asia/Shanghai
    • 添加Debian平臺