Centos作为比较流行的Linux系统,经常被用作测试和生产环境部署,最新版本的Centos7自带了Python2.7,如果要用Python3就需要进行安装配置,以Python3.6.6为例。
通过cat /etc/os-release
命令可以看到系统版本信息,确认系统为Centos7,接下来一步步配置Python3.6.6。
[root@localhost ~]$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
一、更换yum源为国内的阿里云
将yum源更换为国内的阿里云可以方便我们解决Python3依赖。
(1)对原本的yum源进行备份
[root@localhost /]$ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
(2)下载阿里云的yum源
[root@localhost /]$ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2018-09-29 22:45:27-- http://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 118.212.224.103, 118.212.224.101, 118.212.224.99, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|118.212.224.103|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K) [application/octet-stream]
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”
100%[======================================>] 2,523 --.-K/s 用时 0s
2018-09-29 22:45:27 (555 MB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])
(3)为install生成缓存
[root@localhost /]$ yum makecache
已加载插件:fastestmirror, langpacks
http://mirrors.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to mirrors.aliyuncs.com:80; 拒绝连接"
正在尝试其它镜像。
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; 未知的名称或服务"
正在尝试其它镜像。
base | 3.6 kB 00:00
extras | 3.4 kB 00:00
influxdb | 2.5 kB 00:00
updates | 3.4 kB 00:00
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
元数据缓存已建立
二、通过yum下载Python3.6.6配置安装
(1)下载python3.6.6压缩包并解压至指定目录
[root@localhost /]$ wget "https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz"
--2018-09-29 22:50:44-- https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
正在解析主机 www.python.org (www.python.org)... 151.101.108.223, 2a04:4e42:1a::223
正在连接 www.python.org (www.python.org)|151.101.108.223|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:22930752 (22M) [application/octet-stream]
正在保存至: “Python-3.6.6.tgz”
100%[======================================>] 22,930,752 73.3KB/s 用时 5m 25s
2018-09-29 22:56:18 (68.8 KB/s) - 已保存 “Python-3.6.6.tgz” [22930752/22930752])
这里解压到/tmp
目录。
[root@localhost /]$ tar -zxvf Python-3.6.6.tgz -C /tmp
(2)安装python3的依赖
进入/tmp/python3.6.6
目录执行命令安装。
[root@localhost /]$ cd /tmp/Python-3.6.6/
[root@localhost Python-3.6.6]$ yum -y groupinstall "Development tools"
......
[root@localhost Python-3.6.6]$ yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
......
(3)配置安装python3到指定目录
这里配置的的目录是/usr/local
。
[root@localhost Python-3.6.6]$ ./configure --prefix=/usr/local/python3
安装python3 ,时间较长。
[root@localhost Python-3.6.6]$ make
......
[root@localhost Python-3.6.6]$ make install
......
安装完毕后可以检查是否成功,可以看到Python3的软链接也已经创建成功。
[root@localhost ~]$ python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@localhost ~]$ python3
Python 3.6.6 (default, Sep 29 2018, 23:11:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
我们可以查看软链接情况,python指向python2、python3指向python3.6.6。
[root@localhost ~]$ cd /usr/bin/
[root@localhost bin]$ ll python
lrwxrwxrwx 1 root root 7 7月 26 2017 python -> python2
[root@localhost bin]$ ll python3
lrwxrwxrwx 1 root root 32 9月 29 23:33 python3 -> /usr/local/python3/bin/python3.6
这里我们还可以顺便配置一下pip3的软链接,还可以将pip3升级到最新版本。
[root@localhost bin]$ ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
[root@localhost bin]$ pip3 list
Package Version
---------- -------
pip 10.0.1
setuptools 39.0.1
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@localhost bin]$ pip3 install --upgrade pip
......
Successfully installed pip-18.1
[root@localhost bin]$ pip3 list
Package Version
---------- -------
pip 18.1
setuptools 39.0.1
三、修改软链接配置
如果想要python命令指向python3.6.6,可以先删除原来的python软链接文件,再将python指向python3.6.6。
[root@localhost bin]$ rm python
再新建python链接到python3:
[root@localhost bin]$ ln -s /usr/local/bin/python3.6 /bin/python
[root@localhost bin]$ python
Python 3.6.6 (default, Sep 29 2018, 23:11:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
这时的python命令就指向Python3.6.6了,当然,我不建议这么做,使用python3命令更加直观。
如果你喜欢本文章,还请点个关注和喜欢,我会为大家不断地带来Python学习笔记。
网友评论