CentOS 7多种方式安装Python3

2,482次阅读

共计 1636 个字符,预计需要花费 5 分钟才能阅读完成。

  • 环境
$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
$ uname -a
Linux ansheng 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • 安装开发工具包
$ yum groupinstall -y development tools

目前最新的 CentOS 7.6.1810 自带的 Python 版本只有Python 2.7.5

$ python -V
Python 2.7.5

但是目前在工作中,我们都已经使用 Python3 进行开发了,而且每次在项目部署时都要升级到Python3,所以还是写篇博客记录下吧。

EPEL 源

EPELFedora 小组维护的一个高质量附加软件包。

  • 安装 epel 源
$ yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  • 搜索 Python3 版本
$ yum search python3

上面的命令搜索出的最新版本是Python36,所以在这里我们会安装Python36

  • 安装 Python36
$ yum install -y python36 python36-setuptools python36-devel
  • 安装 pip
$ mkdir -p /usr/local/lib/python3.6/site-packages/  # 需要先创建 packages 的存放目录,不然安装时会报错
$ easy_install-3.6  pip
  • 查看版本
$ python3.6 -V
Python 3.6.6
$ pip3 -V
pip 18.1 from /usr/local/lib/python3.6/site-packages/pip-18.1-py3.6.egg/pip (python 3.6)
  • 创建虚拟环境
$ mkdir ~/.venv # 创建虚拟环境目录
$ cd ~/.venv/
$ python3.6 -m venv ansheng  # 创建名为 ansheng 的虚拟环境
$ source ~/.venv/ansheng/bin/activate  # 切换到 ansheng 虚拟环境
(ansheng) $ python -V  # python 版本
Python 3.6.6
(ansheng) $ pip -V  # pip 版本
pip 10.0.1 from /root/.venv/ansheng/lib64/python3.6/site-packages/pip (python 3.6)
(ansheng) $ deactivate  # 退出虚拟环境

源码

目前 EPEL 源提供的最新版本也只是 Python3.6 版本,如果要使用目前最新的 Python3.7 版本,那只能从源码编译安装了。

  • 安装依赖包
$ yum install -y gcc openssl-devel bzip2-devel libffi libffi-devel
  • 下载 Python 3.7

你可以从 https://www.python.org/downloads/source/ 获取最新的源码包。

$ cd /usr/src/
$ wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz

解压

$ tar xzf Python-3.7.2.tgz
  • 安装 Python 3.7
$ cd Python-3.7.2
$ ./configure --enable-optimizations
$ make altinstall
  • 删除下载的源码文件
$ rm -f /usr/src/Python-3.7.2.tgz
  • 查看 Python 与 Pip 版本
$ python3.7 -V
Python 3.7.2
$ pip3.7 -V
pip 18.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
  • 检查 yum 是否工作
$ yum install tmux -y

到此,安装完成。

正文完
 
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 2019-08-31发表,共计1636字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。