MENU

Ubuntu下通过编译安装Python3

June 25, 2019 • Read: 72 • 技术笔记

本次主要记录通过下载和编译的方式安装Python3

测试版本:Python3.7

1. 安装相关的依赖

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev  libncursesw5-dev xz-utils tk-dev

2. 官网下载源代码

wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
tar -zxvf Python-3.7.2.tgz
cd Python-3.7.2/

3. 编译Python3

--prefix选项是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr/local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share

如果配置--prefix,比如./configure --prefix=/usr/local/python3.7/,就可以统一管理所有的文件。

chmod -R +x Python-3.7.2
./configure --prefix=/usr/local/python3.7/ --enable-optimizations
make && make install

4. 添加软链接

#添加python3的软链接 
ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3
#添加 pip3 的软链接 
ln -s /usr/local/python3.7/bin/pip3.7 /usr/bin/pip3

可能遇到的问题:

1. Ubuntu下pip install爆出Command 'lsb_release -a' returned non-zero exit status 1.

运行lsb_release -a则会爆出如下错误:

ModuleNotFoundError: No module named 'lsb_release';

我们首先要找到lsb_release.py的位置:

root@vultr:~# sudo find / -name 'lsb_release.py'
/lsb_release.py
/usr/lib/python2.7/dist-packages/lsb_release.py
/usr/lib/python3/dist-packages/lsb_release.py
/usr/share/pyshared/lsb_release.py

然后我们把/usr/lib/python3/dist-packages/lsb_release.py复制到/usr/local/python3.7/lib/python3.7/即可

cp /usr/lib/python3/dist-packages/lsb_release.py /usr/local/python3.7/lib/python3.7/

然后再次运行lsb_release.py

root@vultr:~# lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 16.04.6 LTS
Release:    16.04
Codename:    xenial

ok了,现在重新运行pip install命令即可。

Last Modified: July 31, 2019
Archives QR Code
QR Code for this page
Tipping QR Code