Centos7手动编译安装Python3
Python3的安装方式非常的简单,首先去到Python下载目录下载想要的Python包,这里我选择了3.6.5版本
$ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz --2018-06-26 07:38:46-- https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz Resolving www.python.org (www.python.org)... 151.101.24.223, 2a04:4e42:2e::223 Connecting to www.python.org (www.python.org)|151.101.24.223|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 22994617 (22M) [application/octet-stream] Saving to: ‘Python-3.6.5.tgz’ 100%[=====================================================================================================================================================>] 22,994,617 1.16MB/s in 14s 2018-06-26 07:39:01 (1.57 MB/s) - ‘Python-3.6.5.tgz’ saved [22994617/22994617]
然后解压缩:
$ tar -xzvf Python-3.6.5.tgz
开始编译安装:
$ cd Python-3.6.5/ $ ./configure ... ... 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 If you want a release build with all stable optimizations active (PGO, etc), please run ./configure --enable-optimizations
可以看到最后有一行很醒目的提示,这行提示可以忽略,它的意义是是否想要编译一个允许各种优化(例如PGO——Profile-guided optimization,这是GCC支持的一个优化选项,可以提升程序性能,有兴趣的同学可以自行谷歌)的版本,如果想要,可以运行 ./configure --enable-optimizations。我这里选择忽略,继续编译:
$ make
$ sudo make install zipimport.ZipImportError: can't decompress data; zlib not available make: *** [install] Error 1
出现了报错,缺少zlib,直接安装下:
$ sudo yum install zlib*
继续安装:
$ sudo make install
到这一步还不行,用python --version看到的版本还是老的:
$ python --version Python 2.7.5 $ which python /usr/bin/python $ ls -lh /usr/bin | grep python lrwxrwxrwx 1 root root 7 Apr 1 11:46 python -> python2 lrwxrwxrwx 1 root root 9 Apr 1 11:46 python2 -> python2.7 -rwxr-xr-x 1 root root 7.0K Aug 4 2017 python2.7
可以看到默认的python程序指向了python2.7,所以需要删除这个软连接,建立新版本的软连接:
$ ls -lh /usr/local/bin/ | grep python lrwxrwxrwx 1 root root 9 Jun 26 08:12 python3 -> python3.6 -rwxr-xr-x 2 root root 13M Jun 26 08:11 python3.6 lrwxrwxrwx 1 root root 17 Jun 26 08:12 python3.6-config -> python3.6m-config -rwxr-xr-x 2 root root 13M Jun 26 08:11 python3.6m -rwxr-xr-x 1 root root 3.1K Jun 26 08:12 python3.6m-config lrwxrwxrwx 1 root root 16 Jun 26 08:12 python3-config -> python3.6-config $ sudo rm /usr/bin/python $ sudo ln -s /usr/local/bin/python3 /usr/bin/python $ python --version Python 3.6.5
可以看到,建立映射之后,python版本目前为3.6.5,到这里还没完,pip还是指向了原来的版本:
$ pip --version pip 10.0.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)
同样的,查看下pip的位置:
$ which pip /usr/bin/pip $ ls -lh /usr/bin/ | grep pip -rwxr-xr-x. 1 root root 2.3K Jul 31 2015 lesspipe.sh -rwxr-xr-x 1 root root 215 Jun 25 09:12 pip -rwxr-xr-x 1 root root 215 Jun 25 09:12 pip2 -rwxr-xr-x 1 root root 215 Jun 25 09:12 pip2.7 $ ls -lh /usr/local/bin/ | grep pip -rwxr-xr-x 1 root root 214 Jun 26 08:12 pip3 -rwxr-xr-x 1 root root 214 Jun 26 08:12 pip3.6
/usr/bin/pip不是一个软连接,所以不直接删除,做个备份:
$ sudo mv /usr/bin/pip /usr/bin/pip2_for_python2.7_bak $ ls -lh /usr/bin/ | grep pip -rwxr-xr-x. 1 root root 2.3K Jul 31 2015 lesspipe.sh -rwxr-xr-x 1 root root 215 Jun 25 09:12 pip2 -rwxr-xr-x 1 root root 215 Jun 25 09:12 pip2.7 -rwxr-xr-x 1 root root 215 Jun 25 09:12 pip2_for_python2.7_bak
然后建立软连接:
$ sudo ln -s /usr/local/bin/pip3 /usr/bin/pip $ pip --version pip 9.0.3 from /usr/local/lib/python3.6/site-packages (python 3.6)
pip版本也成功的切换了,后续可能还会遇到一些类似的python路径问题,都可以依此解决。
安装成功之后,使用yum的时候会出现报错:
$ sudo yum install zip File "/bin/yum", line 30 except KeyboardInterrupt, e: ^
这是因为yum的包管理,是基于python2.x写的,跟python3有语法上的不兼容。
这个时候可以修改下yum的配置文件:
$ sudo vim /usr/bin/yum
将第一行的“#!/usr/bin/python” 改为 “#!/usr/bin/python2.7”
类似的,同样修改下/usr/libexec/urlgrabber-ext-down。