1.Ubuntu 16 上 python 升级到 python3.7 的方法
在 Ubuntu 16.04 中,python3 的默认版本为 3.5:
$ python3 -V
python 3.5.2
本文以在 Ubuntu 16.04 中安装为例,方法同样适用于 Ubuntu 18.04.
2. 通过 Apt 安装
ubuntu 官方 apt 库中还未收录 python 3.7,这里使用 deadsnakes PPA 库安装。
2.1. 安装依赖包
$ sudo apt update
$ sudo apt install software-properties-common
2.2. 添加 deadsnakes PPA 源
$ sudo add-apt-repository ppa:deadsnakes/ppa
Press [ENTER] to continue or Ctrl-c to cancel adding it.
2.3. 安装 python 3.7
$ sudo apt install python3.7
此处发生错误,按提示加一个–fix-missing
sudo apt install python3.7 --fix-missing
$ python3.7 -V
Python 3.7.9
安装成功
3. 配置 python3.7 为系统默认 python3
3.1. 将 python 各版本添加到 update-alternatives
$ which python3.7
/usr/bin/python3.7
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1
$ which python3.5
/usr/bin/python3.5
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2
3.2. 配置 python3 默认指向 python3.7
$ sudo update-alternatives --config python3
有 2 个候选项可用于替换 python3 (提供 /usr/bin/python3)。
选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/bin/python3.5 2 自动模式
1 /usr/bin/python3.5 2 手动模式
2 /usr/bin/python3.7 1 手动模式
要维持当前值[*]请按<回车键>,或者键入选择的编号:
选择/输入 2, 回车。
3.3 测试 python 版本
$ python3 -V
Python 3.7.9
资源
How to Install Python 3.8 on Ubuntu 18.04.
How to upgrade to python 3.7 on Ubuntu 18.10.