說明:CentOS 7最小化安裝,默認Python 2.7 ,開發需要安裝python3.6.1 。
1、檢查python版本
[root@bogon ~]# python -V
Python 2.7.5
[root@bogon ~]#
2、安裝必要的庫文件
# yum -y install zlib zlib-devel gcc
3、下載python3.6.1
# wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
用base鏡像源下載安裝openssl-devel
yum --enablerepo=base --enablerepo=updates install openssl-devel
因為新版的pip默認要使用SSL,可以通過設置修改,先安裝openssl-devel,然后編譯安裝,只是在編譯的過程中加入 --enable-optimizations(優化構建python)
4、解壓安裝
# xz -d Python-3.6.1.tar.xz
# tar -xvf Python-3.6.1.tar
# cd Python-3.6.1/
# ./configure --prefix=/usr/local/python3 --enable-optimizations
# make && make install
5、永久添加環境變量
# vim/etc/profile
添加 export PATH="$PATH:/usr/local/python3/bin"
添加完成后 source /etc/profile
**臨時添加環境變量為 # export PATH="$PATH:/usr/local/python3/bin" 終端結束后環境變量生效
6、打開python3.6
[root@bogon ~]# python3
Python 3.6.1 (default, May 19 2017, 04:11:00)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
7、修改/usr/bin/python鏈接
# cd/usr/bin
# mv python python.bak
# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python
# ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
修改后可直接用 # python打開python3