【CentOS 7】源码编译安装【Python 3.6.2】
在 CentOS 7 上源码编译安装,首先要先下载 Python 3.6.2 的安装介质,然后再完成编译安装,这个过程需要CentOS拥有如下程序:
首先最好把 yum -y group install development 开发包全部安装,然后再检查以下软件包是否安装
- wget(如果有现成的Python 3.6.2 直接上传的话,那么是否包含此程序就无所谓了)
- gcc(编译用到了gcc,所以gcc是必须的)
- make(编译工具,这个需要有)
- zlib-devel
- openssl-devel
一、安装缺失软件包
本实验环境采用wget直接下载的方式下载。所以需要验证上述软件是否安装。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
## 通过下面结果显然可以看到,gcc 并未安装 [root@msp ~]# rpm -qa wget gcc make zlib-devel openssl-devel make-3.82-23.el7.x86_64 wget-1.14-13.el7.x86_64 [root@msp ~]# ## 通过 yum 安装 gcc,如果其他软件未安装,也可通过 yum 一并安装,CentOS 中的 yum 源是可用的,直接 yum 安装就可以了 [root@msp soft]# rpm -qa wget gcc make zlib-devel openssl-devel make-3.82-23.el7.x86_64 wget-1.14-13.el7.x86_64 [root@msp soft]# yum install -y gcc zlib-devel openssl-devel Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile …… Complete! [root@msp soft]# ## 安装后的确认 [root@msp ~]# rpm -qa wget gcc make zlib-devel zlib-devel-1.2.7-17.el7.x86_64 gcc-4.8.5-11.el7.x86_64 make-3.82-23.el7.x86_64 wget-1.14-13.el7.x86_64 [root@msp ~]# |
二、利用 wget 在官网下载 Python 3.6.1
1 2 3 4 5 6 |
## 下载有点儿略慢,最终我实在忍受不了下载的速度了,利用迅雷下载到本地,然后上传到服务器上的。 [root@msp soft]# wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz --2017-08-14 18:25:41-- https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz Resolving www.python.org (www.python.org)... 151.101.0.223, 151.101.64.223, 151.101.128.223, ... Connecting to www.python.org (www.python.org)|151.101.0.223|:443... connected. HTTP request sent, awaiting response... 200 OK |
三、解压缩
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@msp soft]# ls |grep Python-3.6.2.tgz Python-3.6.2.tgz [root@msp soft]# tar -xzvf Python-3.6.2.tgz …… Python-3.6.2/Objects/obmalloc.c Python-3.6.2/Objects/object.c Python-3.6.2/Objects/abstract.c Python-3.6.2/Objects/listobject.c Python-3.6.2/Objects/bytes_methods.c Python-3.6.2/Objects/dictnotes.txt Python-3.6.2/Objects/typeslots.inc [root@msp soft]# |
四、编译
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@msp soft]# cd Python-3.6.2/ [root@msp Python-3.6.2]# ./configure --prefix=/usr/local/python3.6 --enable-optimizations …… configure: creating ./config.status config.status: creating Makefile.pre config.status: creating Modules/Setup.config config.status: creating Misc/python.pc config.status: creating Misc/python-config.sh config.status: creating Modules/ld_so_aix config.status: creating pyconfig.h creating Modules/Setup creating Modules/Setup.local creating Makefile [root@msp Python-3.6.2]# ## make 时间会有些长,耐心等待 [root@msp Python-3.6.2]# make [root@msp Python-3.6.2]# make install |
五、验证
1 2 3 |
[root@msp ~]# /usr/local/python3.6/bin/python3 --version Python 3.6.2 [root@msp ~]# |
六、修改环境变量
方便使用python,当然也可以通过软链接的方式将可执行程序连接到/usr/bin/目录下,作者本人更喜欢通过环境变量修改
1 2 3 4 5 6 7 8 9 10 |
[root@msp ~]# vi .bash_profile …… ## 追加如下内容 export PATH=$PATH:/usr/local/python3.6/bin/ alias python=python3 ## 保存,退出 ## 环境变量生效 [root@msp ~]# source .bash_profile [root@msp ~]# |
七、确认环境变量
1 2 3 4 5 6 |
[root@msp ~]# python --version Python 3.6.2 [root@msp ~]# [root@msp ~]# python3 --version Python 3.6.2 [root@msp ~]# |