Ubuntu 环境 TensorFlow (最新版1.4) 源码编译、安装

时间:2024-08-26 10:34:44

Ubuntu 环境 TensorFlow 源码编译安装

基于(Ubuntu 14.04LTS/Ubuntu 16.04LTS/)

一、编译环境

1) 安装 pip

sudo apt-get install python-pip python-dev

2)安装JDK 8

sudo apt-get install openjdk-8-jdk

Ubuntu 14.04 LTS 还需要:

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update && sudo apt-get install oracle-java8-installer

3)安装Bazel

A: 添加 Bazel URI 到 package source

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list

curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -

B:更新&安装

sudo apt-get update

sudo apt-get install bazel

如果已经安装过,更新则:

sudo apt-get upgrade bazel

C:设置环境变量

一次执行

export PATH="$PATH:$HOME/bin"

直接添加到.bashrc ,打开bashrc 最后一行加入(PATH="$PATH:$HOME/bin")

vim ~/.bashrc

PATH="$PATH:$HOME/bin"

4)安装其他依赖包

sudo apt-get install libcupti-dev

sudo pip install --upgrade protobuf

sudo apt-get install git python-dev python3-dev python-numpy python3-numpy python-six python3-six build-essential python-pip python3-pip python-virtualenv swig python-wheel python3-wheel libcurl3-dev libcupti-dev

apt-get install libglib2.0-dev zlib1g-dev

sudo apt-get install librdmacm-dev

5) 如果要GPU支持需要

https://alliseesolutions.wordpress.com/2016/09/08/install-gpu-tensorflow-from-sources-w-ubuntu-16-04-and-cuda-8-0/

A:安装/更新GPU驱动

sudo add-apt-repository ppa:graphics-drivers/ppa

sudo apt update

B:Nvidia Toolkit 8.0 & CudNN

在https://developer.nvidia.com/cuda-toolkit下载对应的版本

sudo sh cuda_8.0.61_375.26_linux.run --override --silent --toolkit

会将cuda安装到: /usr/local/cuda

C:安装CudNN

https://developer.nvidia.com/cudnn 下载对应的版本

解压到 /usr/local/cuda

tar -xzvf cudnn-8.0-linux-x64-v6.0.tgz

sudo cp cuda/include/cudnn.h /usr/local/cuda/include

sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64

sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

D: 配置环境变量

~/.bashrc 添加

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"

export CUDA_HOME=/usr/local/cuda

然后使环境变量生效

source ~/.bashrc

二、 TensorFlow 源码下载、编译、安装

1)下载tensorflow 源码

git clone https://github.com/tensorflow/tensorflow

2)配置TensorFlow

到TensorFlow的根目录执行

./configure

注:出于国情原因下面的一定选N

Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]

Do you wish to build TensorFlow with Hadoop File System support? [y/N]

3)编译安装

bazel编译pip 的安装包,然后通过 pip 安装

1) bazel编译

bazel build -c opt //tensorflow/tools/pip_package:build_pip_package

2) 生成安装包

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

2017年 12月 12日 星期二 13:32:22 CST : === Output wheel file is in: /tmp/tensorflow_pkg

3) 安装

sudo pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl

注意: 2)生成安装包的目录,tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl在=== Output 提示的 /tmp/tensorflow_pkg下

安装过程会下载一些依赖的包和库,最后成功提示:

Successfully installed absl-py-0.1.6 backports.weakref-1.0.post1 bleach-1.5.0 enum34-1.1.6 funcsigs-1.0.2 html5lib-0.9999999 markdown-2.6.10 mock-2.0.0 numpy-1.13.3 pbr-3.1.1 tensorf

三、遇到问题

编译时出现如下错误:

ERROR: /home/duanyufei/source/TensorFlow/tensorflow/tensorflow/contrib/gdr/BUILD:52:1: C++ compilation of rule '//tensorflow/contrib/gdr:gdr_memory_manager' failed (Exit 1)

tensorflow/contrib/gdr/gdr_memory_manager.cc:28:27: fatal error: rdma/rdma_cma.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: 323.279s, Critical Path: 33.69s

FAILED: Build did NOT complete successfully

解决办法

sudo apt-get install librdmacm-dev

四、测试 hello word!

在终端打开python,运行如下代码

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))

结果:

Hello, TensorFlow!