ubuntu16.04 环境下编译适用于 ARM Linux 且包含 python 编译包的库 - 避坑
| 参考链接
Linux x86环境下编译适用于 ARM Linux的库
Ubuntu16.04安装Python3.7及其pip3并切换为默认版本
Paddle-Lite在树莓派4B端
| 准备编译环境
采用官网推荐的环境为本编译环境Linux x86环境下编译适用于 ARM Linux的库
推荐环境
- Ubuntu == 16.04
- aarch64-xxxx-gcc、aarch64-xxxx-g++ == 5.4.0
- python == 3.7
- cmake >= 3.10
- git、make、wget、scp
环境安装命令
| 以 Ubuntu16.04 为例介绍安装命令。其它 Linux 发行版安装步骤类似,在此不再赘述。 注意需要 root 用户权限执行如下命令。
# 1. Install basic software
apt update
apt-get install -y --no-install-recommends \
gcc g++ git make wget python unzip
# 2. Install arm gcc toolchains
apt-get install -y --no-install-recommends \
g++-arm-linux-gnueabi gcc-arm-linux-gnueabi \
g++-arm-linux-gnueabihf gcc-arm-linux-gnueabihf \
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
# 3. Install cmake 3.10 or above
wget -c https://mms-res.cdn.bcebos.com/cmake-3.10.3-Linux-x86_64.tar.gz && \
tar xzf cmake-3.10.3-Linux-x86_64.tar.gz && \
mv cmake-3.10.3-Linux-x86_64 /opt/cmake-3.10 && \
ln -s /opt/cmake-3.10/bin/cmake /usr/bin/cmake && \
ln -s /opt/cmake-3.10/bin/ccmake /usr/bin/ccmake
由于此编译环境需要python3.7版本,而ubuntu16.04默认安装的是python3.5及python2.7版本。
因此需要额外安装python3.7
# 可通过测试ubuntu16.04自带的python版本
python -V
python3 -V
| 升级系统及依赖安装
参考Ubuntu16.04安装Python3.7及其pip3并切换为默认版本
1:升级系统
# 1:更新apt-get软件源并完成系统升级
sudo apt-get update
sudo apt-get upgrade -y
# 2:安装Python3的安装依赖,包括build-essential、libffi-dev、libssl-dev等,可以通过以下命令进行安装。
sudo apt-get install -y build-essential libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev
2:python3.7源码安装
# 1:下载python3.7源码(以3.7.9为例)
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
# 2:解压
tar -xvf Python-3.7.9.tgz
# 3:进入解压后的文件夹,配置并编译源码
cd Python-3.7.9/
./configure
make
sudo make install
# 4:测试是否安装正确
python3.7 -V
// 正确输出 Python 3.7.9 说明python3.7安装成功
3:安装pip3
由于Python3.7没有自带pip3,需要单独进行安装。
# 1:首先下载get-pip.py脚本,建议下载最新版本。可以通过以下命令进行下载:
wget https://bootstrap.pypa.io/get-pip.py
# 2:然后使用Python3.7安装pip3,可以通过以下命令进行安装:
sudo python3.7 get-pip.py
# 3:完成pip3安装后,可以通过以下命令查看pip3的版本:
pip3 -V
// 如果输出正确的版本号,则说明pip3已经安装成功。
4:ubuntu下切换python版本,设置系统默认的python版本
可以使用 update-alternatives 来为整个系统更改Python 版本。
这一步要配置正确,要不然后续编译Paddle-Lite时会报找不到python的错误。
# 1:首先罗列出所有可用的python 替代版本信息
update-alternatives --list python
// 若出现 update-alternatives: error: no alternatives for python,则表示 Python 的替代版本尚未被update-alternatives 命令识别
# 2:使用以下操作将Python3.7设置为默认Python,默认pip设置为pip3
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.7 1
sudo update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip3 1
// using /usr/bin/python3.7 to provide /usr/bin/python (python) in auto mode
// 注:后面的数字越大说明优先级越高
# 3:完成上述设置后,运行
python --version
// 正确输出 Python 3.7.9 说明切换成功,python3.7为系统默认
# 4:再次列出可用的 Python 替代版本
update-alternatives --list python
// 此时会输出 /usr/local/bin/python3.7
# update-alternatives --config python # 可查看可替换的python版本
| 编译Paddle-Lite
# 下载 Paddle Lite 源码并切换到发布分支,如 develop
git clone https://github.com/PaddlePaddle/Paddle-Lite.git
cd Paddle-Lite && git checkout develop
# (可选) 删除 third-party 目录,编译脚本会自动从国内 CDN 下载第三方库文件
# rm -rf third-party
# 编译
./lite/tools/build_linux.sh --arch armv8 --with_python=ON --python_version=3.7 --with_cv=ON
# 可通过以下命令查看编译的可选项;输出具体选项及释义
./lite/tools/build_linux.sh help
编译完成
| 编译过程遇到的错误
错误1:
sh: 1: patchelf: not found
Traceback (most recent call last):
File "setup.py", line 97, in <module>
raise Exception("patch third_party libs failed, command: %s" % COMMAND)
Exception: patch third_party libs failed, command: patchelf --set-rpath '$ORIGIN/libs/' /home/ub1604/Downloads/Paddle-Lite/build.lite.linux.armv8.gcc/inference_lite_lib.armlinux.armv8/python/install/lite/lite.so
lite/CMakeFiles/publish_inference_python_installer.dir/build.make:57: recipe for target 'lite/CMakeFiles/publish_inference_python_installer' failed
make[3]: *** [lite/CMakeFiles/publish_inference_python_installer] Error 1
CMakeFiles/Makefile2:397: recipe for target 'lite/CMakeFiles/publish_inference_python_installer.dir/all' failed
make[2]: *** [lite/CMakeFiles/publish_inference_python_installer.dir/all] Error 2
make[2]: *** Waiting for unfinished jobs....
这个错误提示表明在执行Paddle Lite的编译过程中缺少了patchelf工具
# 1:使用以下命令来安装:
sudo apt-get install patchelf
# 2:检查是否将patchelf添加到了系统的PATH环境变量中
echo $PATH | grep -Eo "(^|:)/usr/local/sbin(:|$)"
// 输出中包含/usr/local/sbin,则说明已经添加到PATH环境变量中。如果没有添加,将patchelf所在的路径添加到PATH环境变量中。
错误2:
/usr/local/lib/python3.7/site-packages/setuptools/dist.py:472: UserWarning: The version specified ('c36a32e') is an invalid version, this may not work as expected with newer versions of setuptools, pip, and PyPI. Please see PEP 440 for more details.
"details." % version
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help [cmd1 cmd2 ...]
or: setup.py --help-commands
or: setup.py cmd --help
error: invalid command 'bdist_wheel'
lite/CMakeFiles/publish_inference_python_installer.dir/build.make:57: recipe for target 'lite/CMakeFiles/publish_inference_python_installer' failed
make[3]: *** [lite/CMakeFiles/publish_inference_python_installer] Error 1
CMakeFiles/Makefile2:397: recipe for target 'lite/CMakeFiles/publish_inference_python_installer.dir/all' failed
make[2]: *** [lite/CMakeFiles/publish_inference_python_installer.dir/all] Error 2
make[2]: *** Waiting for unfinished jobs....
这个错误提示表明在执行Paddle Lite的编译过程中缺少了wheel工具
# 1:解决方法,安装wheel包
pip install wheel