CentOS 7 下源码安装 python3
在CentOS7下,默认安装的是python2.7;为满足项目要求,安装python3 的方法如下:
1. 首先安装python3.6可能使用的依赖
# yum -y install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel zlib-devel
2. 编译安装Python3.6
# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
# tar -zxvf Python-3.6.4.tgz
# cd Python-3.6.4
# ./configure --prefix=/usr/local/python36
# make && make altinstall
如出现以下信息,则表示编译安装Python成功!
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-9.0.1 setuptools-28.8.0
3. 修改默认Python版本(更改/usr/bin/python链接)
# mv /usr/bin/python /usr/bin/python.old
# ln -sv /usr/local/python36/bin/python3.6 /usr/bin/python
# ln -sv /usr/local/bin/python3.6 /usr/bin/python
# ln -sv /usr/local/bin/python3.6 /usr/bin/python3 # python -V
Python 3.6.4
4. 防止yum执行异常,修改yum使用的Python版本
# vim /usr/bin/yum
#!/usr/bin/python2.7
也可以安装一个tree测试一下
# yum -y install tree
如果出现报错:File “/usr/libexec/urlgrabber-ext-down”, line 28
[root@localhost ~]# ls -l /usr/bin/yum*
-rwxr-xr-x 1 root root 802 Feb 12 15:38 /usr/bin/yum
-rwxr-xr-x 1 root root 9429 Aug 11 2017 /usr/bin/yum-builddep
-rwxr-xr-x 1 root root 9039 Aug 11 2017 /usr/bin/yum-config-manager
-rwxr-xr-x 1 root root 7609 Aug 11 2017 /usr/bin/yum-debug-dump
-rwxr-xr-x 1 root root 7903 Aug 11 2017 /usr/bin/yum-debug-restore
-rwxr-xr-x 1 root root 10999 Aug 11 2017 /usr/bin/yumdownloader
修改 vim /usr/libexec/urlgrabber-ext-down 文件,将 /usr/bin/python 改为 /usr/bin/python2.7 即可! 这样,yum安装软件的时候就不会报错了,而且默认安装的就是python3了,输入python时,就会进入python3界面,如果想进入python2,那么就输入python2或python2.7 即可!