在 Ubuntu 16.04.6 LTS 系统上安装 Python 3.6.3

自己的阿里云一个 VPS 用的是系统 Ubuntu 16.04.6 LTS,自带的python版本是 2.7.123.5.2,有时候要用到 python 3.6,又不想卸掉原来版本。下面介绍安装 python 3.6.3 的过程,因为版本较旧,遇到一些坑,这里记录一下。

在 Ubuntu 16.04.6 LTS 系统上安装 Python 3.6.3

从 PPA 安装

安装必要的依赖

1
apt-get install software-properties-common python-software-properties

添加

1
add-apt-repository ppa:jonathonf/python-3.6

会有提示:

1
2
3
4
5
6
7
This PPA has been removed from public access as part of a protest against the abuse of open-source projects by large companies. For more detail visit the main page here: https://launchpad.net/~jonathonf

If you are a company and you would like this PPA to continue then let me know your preferred route for contributions and I will arrange something.

If we have already been in contact then ping me your Launchpad ID and I will add you to a private PPA in the meantime.
 More info: https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6
Press [ENTER] to continue or ctrl-c to cancel adding it

按回车继续

由于该系统比较旧, apt-get update 时发现有好多 404

apt-get update

尝试安装

Go:
1
2
3
4
5
6
7
# apt-get install python3.6
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package python3.6
E: Couldn't find any package by glob 'python3.6'
E: Couldn't find any package by regex 'python3.6'

补充

下面适合 Ubuntu 18.04, 16.04

1
add-apt-repository ppa:deadsnakes/ppa

从源码安装

下载源码

1
2
cd /opt
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

解压缩

1
tar -xvf Python-3.6.3.tgz

编译安装

1
2
3
4
cd Python-3.6.3
./configure
make
make install

安装过程中如果遇到下面错误

1
zipimport.ZipImportError: can't decompress data; zlib not available

就安装依赖包

1
apt-get install zlib1g-dev

再次 make && make install

验证版本

1
2
# python3.6 -V
Python 3.6.3

版本替换

现在系统中已经有3个 Python 版本

1
2
3
python -V
python3 -V
python3.6 -V

需要把 python3 命令转向 python3.6

1
2
3
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.6 2
update-alternatives --config python3

会有下面提示:

1
2
3
4
5
6
7
8
9
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  Selection    Path                      Priority   Status
------------------------------------------------------------
* 0            /usr/local/bin/python3.6   2         auto mode
  1            /usr/bin/python3.5         1         manual mode
  2            /usr/local/bin/python3.6   2         manual mode

Press <enter> to keep the current choice[*], or type selection number: 

按回车就可以,验证版本

1
2
# python3 -V
Python 3.6.3

pip3 问题

升级后使用 pip3 安装软件,会有下面错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Exception:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.6/site-packages/pip/commands/install.py", line 272, in run
    with self._build_session(options) as session:
  File "/usr/local/lib/python3.6/site-packages/pip/basecommand.py", line 72, in _build_session
    insecure_hosts=options.trusted_hosts,
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 329, in __init__
    self.headers["User-Agent"] = user_agent()
  File "/usr/local/lib/python3.6/site-packages/pip/download.py", line 93, in user_agent
    from pip._vendor import distro
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/distro.py", line 1050, in <module>
    _distro = LinuxDistribution()
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/distro.py", line 594, in __init__
    if include_lsb else {}
  File "/usr/local/lib/python3.6/site-packages/pip/_vendor/distro.py", line 931, in _get_lsb_release_info
    raise subprocess.CalledProcessError(code, cmd, stdout, stderr)
subprocess.CalledProcessError: Command 'lsb_release -a' returned non-zero exit status 1.

重点是 lsb_release,查找 lsb_release.py 并把它复制到 python3.6 的 lib 里即可

1
2
3
4
5
6
7
8
# find / -name 'lsb_release.py'
/usr/lib/python3/dist-packages/lsb_release.py
/usr/lib/python2.7/dist-packages/lsb_release.py
/usr/share/pyshared/lsb_release.py
# find /usr -name python3.6
/usr/local/bin/python3.6
/usr/local/lib/python3.6
# cp /usr/lib/python3/dist-packages/lsb_release.py /usr/local/lib/python3.6/

pip3 install 正常

本文网址: https://pylist.com/topic/193.html 转摘请注明来源

Suggested Topics

Ubuntu 或其它 GNU/Linux 系统下安装 Debian

家有老电脑不用就折腾不止,前段时间曾折腾一台老电脑,2007年上市,只支持 i386 系统,不支持U盘启动,在 winxp 系统下装了 Ubuntu,现在突发奇想,要在 Ubuntu 上安装 Debian。...

在SAE Python上开启gzip的方法

开启 gzip 的作用自不必说,可以省很多流出带宽,可以省很多云豆。昨天这个博客的云豆消耗,其中流出带宽就占九成多,开启后就会只占五成多。...

ubuntu/debian 下自行编译 OpenWRT 固件

首先声明,自行编译是一个自我折腾的过程,没时间或精力就不要试了,这里介绍个人尝试在ubuntu/debian 下编译OpenWRT固件和应用SDK 的过程。...

Leave a Comment

2 thoughts on "在 Ubuntu 16.04.6 LTS 系统上安装 Python 3.6.3"

#1 hldh214 says:

This PPA has been removed from public access as part of a protest against the abuse of open-source projects by large companies. For more detail visit the main page here: https://launchpad.net/~jonathonf

其实提示写的很清楚了

#2 pylist says:

@hldh214 有个项目叫 deadsnakes ,收集各版 python,从2.3到3.9,而且还紧随 Python 官方进行更新

https://github.com/deadsnakes

1
add-apt-repository ppa:deadsnakes/ppa