Ubuntu Tensorflow源码安装

本文详细介绍了在Ubuntu环境下使用VMWare Player搭建的虚拟机中,从零开始编译并安装TensorFlow的过程。涵盖了依赖项安装、Bazel配置与安装、源码编译遇到的问题及解决方案、whl包的生成与安装步骤,以及最终验证TensorFlow是否成功安装的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

=VMWare+Ubuntu+Tensorflow===
VMWare + Ubuntu16.0.4
VMWare Player不支持GPU,只能安装CPU版本
自带-Python2.7.12
Bazel如下0.18-1
Tensorflow-master1.12 (2018/11/06下载)

https://www.tensorflow.org/install/source
0. 安装依耐项
sudo apt install python-dev python-pip # or python3-dev python3-pip

pip install -U --user pip six numpy wheel mock
pip install -U --user keras_applications1.0.5 --no-deps
pip install -U --user keras_preprocessing
1.0.3 --no-deps

  1. Bazel安装
    https://docs.bazel.build/versions/master/install-ubuntu.html
    1.1 下载sh安装文件
    https://github.com/bazelbuild/bazel/releases
    bazel-0.18.1-installer-linux-x86_64.sh

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python
chmod +x bazel-0.18.1-installer-linux-x86_64.sh
./bazel-0.18.1-installer-linux-x86_64.sh --user

安装在个人目录下面
The --user flag installs Bazel to the $HOME/bin directory on your system and sets the .bazelrc path to $HOME/.bazelrc.

1.2 增加环境变量
export PATH=“ P A T H : PATH: PATH:HOME/bin”

  1. 下载源码
    https://github.com/tensorflow/tensorflow.git
    => 直接用master
    => release版本在PC-Window上编译不过,VMWare上没有试过
    2.1 解压 => tensorflow

  2. Tensorflow源码编译配置
    ./configure
    => 根据实际情况配置

  3. CPU-Only编译
    4.1 编译命令
    bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
    4.2 编译问题
    ERROR: /home/xhu/.cache/bazel/_bazel_xhu/8c8299b80b62c2e71f333df9c6122d0b/external/grpc/BUILD:1332:1: C++ compilation of rule ‘@grpc//:grpc_resolver_dns_ares’ failed (Exit 1) gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer ‘-std=c++0x’ -MD -MF … (remaining 50 argument(s) skipped)
    Use --sandbox_debug to see verbose messages from the sandbox
    external/grpc/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc:30:18: fatal error: ares.h: No such file or directory
    compilation terminated.
    Target //tensorflow/tools/pip_package:build_pip_package failed to build
    Use --verbose_failures to see the command lines of failed build steps.
    INFO: Elapsed time: 4117.424s, Critical Path: 173.66s, Remote (0.00% of the time): [queue: 0.00%, setup: 0.00%, process: 0.00%]
    INFO: 1175 processes: 1174 linux-sandbox, 1 local.
    FAILED: Build did NOT complete successfully
    解决:=> apt-get install libc-ares-dev

4.3 编译Pass
Target //tensorflow/tools/pip_package:build_pip_package up-to-date:
bazel-bin/tensorflow/tools/pip_package/build_pip_package
INFO: Elapsed time: 14967.427s, Critical Path: 98.21s
INFO: 7648 processes: 7648 local.
INFO: Build completed successfully, 8333 total actions
4.4 文件分布
home/.cache/bazel : 文件夹大小3.4G, 比PC小(10+G)
软连接
bazel-genfiles -> /home/xhu/.cache/bazel/_bazel_xhu/8c8299b80b62c2e71f333df9c6122d0b/execroot/org_tensorflow/bazel-out/k8-opt/genfiles
bazel-bin -> /home/xhu/.cache/bazel/_bazel_xhu/8c8299b80b62c2e71f333df9c6122d0b/execroot/org_tensorflow/bazel-out/k8-opt/bin
bazel-testlogs -> /home/xhu/.cache/bazel/_bazel_xhu/8c8299b80b62c2e71f333df9c6122d0b/execroot/org_tensorflow/bazel-out/k8-opt/testlogs
bazel-out -> /home/xhu/.cache/bazel/_bazel_xhu/8c8299b80b62c2e71f333df9c6122d0b/execroot/org_tensorflow/bazel-out
bazel-tensorflow -> /home/xhu/.cache/bazel/_bazel_xhu/8c8299b80b62c2e71f333df9c6122d0b/execroot/org_tensorflow

5.Build the Package
5.1 命令行
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
软连接:bazel-bin/tensorflow/tools/pip_package/build_pip_package -> /home/xhu/rsch/tensorflow/tensorflow/tools/pip_package/build_pip_package.sh
5.2 生成whl
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
Wed Nov 7 13:39:12 CST 2018 : === Preparing sources in dir: /tmp/tmp.HgHdbLwxOn
~/rsch/tensorflow ~/rsch/tensorflow
~/rsch/tensorflow
Wed Nov 7 13:39:16 CST 2018 : === Building wheel
warning: no files found matching ‘.pyd’ under directory '
warning: no files found matching ‘.pd’ under directory '
warning: no files found matching ‘.dll’ under directory '
warning: no files found matching ‘.lib’ under directory '
warning: no files found matching ‘.h’ under directory ‘tensorflow/include/tensorflow’
warning: no files found matching '
’ under directory ‘tensorflow/include/Eigen’
warning: no files found matching ‘.h’ under directory ‘tensorflow/include/google’
warning: no files found matching '
’ under directory ‘tensorflow/include/third_party’
warning: no files found matching ‘*’ under directory ‘tensorflow/include/unsupported’
Wed Nov 7 13:39:33 CST 2018 : === Output wheel file is in: /tmp/tensorflow_pkg

ls /tmp/tensorflow_pkg/ -lt
total 63884
-rw-rw-r-- 1 xhu xhu 65416475 Nov 7 13:39 tensorflow-1.12.0rc0-cp27-cp27mu-linux_x86_64.whl
=> 大小为 65.4MB

  1. 安装whl
    6.1 pip install /tmp/tensorflow_pkg/tensorflow-1.12.0rc0-cp27-cp27mu-linux_x86_64.whl
    => 中间的数字是根据自己电脑及软件版本情况的Tag,各人可能不一样

问题1:
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: ‘/usr/local/lib/python2.7/dist-packages/astor-0.7.1.dist-info’
Consider using the --user option or check the permissions.
=> 用sudo安装
sudo pip install /tmp/tensorflow_pkg/tensorflow-1.12.0rc0-cp27-cp27mu-linux_x86_64.whl
问题2:
Cannot uninstall ‘enum34’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
=> sudo pip install --ignore-installed enum34

Successfully installed absl-py-0.6.1 backports.weakref-1.0.post1 futures-3.2.0 gast-0.2.0 grpcio-1.16.0 keras-applications-1.0.6 keras-preprocessing-1.0.5 markdown-3.0.1 tensorboard-1.12.0 tensorflow-1.12.0rc0 tensorflow-estimator-1.10.12 termcolor-1.1.0 werkzeug-0.14.1

6.2 运行测试
$ python

import tensorflow as tf
session = tf.Session()
问题1:

import tensorflow as tf
Traceback (most recent call last):
File “”, line 1, in
File “tensorflow/init.py”, line 24, in
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File “tensorflow/python/init.py”, line 49, in
from tensorflow.python import pywrap_tensorflow
File “tensorflow/python/pywrap_tensorflow.py”, line 25, in
from tensorflow.python.platform import self_check
ImportError: No module named platform
=> 解决: 重启terminal即可、安装后路径未更新!

==================================
修改后编译/安装:

  1. cd tensorflow
  2. python ./configure.py #(可选)
  3. bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
    #Elapsed time: 4117.424s, 核心编译耗时1小时8分钟(实际还有前期下载,一共差不多要2个多小时)
  4. ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
  5. sudo pip install /tmp/tensorflow_pkg/tensorflow-1.12.0rc0-cp27-cp27mu-linux_x86_64.whl
  6. Re-start Terminal
Ubuntu 系统上安装 TensorFlow 需要通过包管理器或者从源码编译。以下是两种常见的安装步骤: 1. **通过包管理器安装** (推荐,对于新用户): - 对于TensorFlow 2.x及更高版本,你可以使用 `pip` 来安装,确保先更新到最新版的Python(通常建议使用Python 3): ``` sudo apt-get update sudo apt-get install python3-pip pip3 install tensorflow ``` - 如果你想选择特定的GPU支持,比如CUDA和cuDNN,可以使用如下的命令安装 GPU 版本: ``` pip3 install tensorflow-gpu ``` 更新 `python3-pip` 和 `cuda toolkit` 可能也需要相应的额外步骤。 2. **从源码编译安装** (对高级用户,需要手动配置环境): - 首先,你需要下载TensorFlow源码: ``` git clone https://github.com/tensorflow/tensorflow.git ``` - 进入目录并切换到 GPU 或 CPU 分支: ``` cd tensorflow ``` - 安装前确保满足所有依赖,然后运行构建脚本,例如使用CUDA和cuDNN: ``` bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package sudo ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /path/to/installation_directory ``` - 最后,你可以使用 `pip3` 安装刚刚打包的.whl文件: ``` pip3 install /path/to/installation_directory/*.whl ``` - 为了确认安装成功,你可以导入TensorFlow库并在Python交互环境中测试它。 **相关问题--:** 1. 如何检查Ubuntu系统是否已经安装TensorFlow? 2. 使用GPU时,还需要安装哪些额外软件? 3. 如何在遇到编译错误时解决TensorFlow源码安装的问题?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值