【深度学习基础环境-下】ubuntu16.04.3安装Bazel+源码编译安装tensorflow(CUDA9.0)

前言

上一篇文章中搭建了CUDA9.0 + cuDNN7.0的基础环境.

先简单说下即将要进行的步骤以及原因。

1. 通过源码编译tensorflow指定CUDA版本

然而在我使用pip安装tensorflow-gpu之后,在import tensorlfow时报错"libcudart.so.8.0 cannot open shared object file: No such file or directory"。然后我到cuda的路径下查看,相关文件的版本都是libcudart.so.9.0xxxxx,再到网上查看相关资料发现,pip安装的tensorflow不支持CUDA9,但是通过源码编译tensorflow,可以指定CUDA版本。

所以通过源码编译的形式安装tensorflow GPU版。

2. 编译tensorflow需要bazel( 0.5.4以上版本)

config tensorflow时报错需要bazel支持,这里需要安装bazel.

这里也是一把辛酸泪.

bazel有两种安装方式,官网提供。我之前贪图省事选择的第二种.sh文件直接安装,然而官网下载的特别慢,我就到某网站下载了0.5.2的版本,并且按照官网的步骤安转完毕,一切顺利。但是config tensorflow时提示要0.5.4以上版本,所以只能卸载重新安装,因为找了很多资料还是不很清楚upgradd方法。这里卸载的时候步骤:

                  (1).直接删掉~/bin文件夹

                  (2).修改环境变量,去掉之前添加的

                  (3).删掉~/.bazel文件夹

卸载完之后使用官网提供的第一种方式来安装bazel以及相关文件,即Using Bazel custom APT repository 。

下面正文


一、安装Bazel

Using Bazel custom APT repository (recommended)

1. Install JDK 8

Install JDK 8 by using:

sudo apt-get install openjdk-8-jdk

On Ubuntu 14.04 LTS you'll have to use a PPA:

sudo add-apt-repository ppa:webupd8team/java

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

2. Add Bazel distribution URI as a package source (one time setup)


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 -

If you want to install the testing version of Bazel, replace stable with testing.

3. Install and update Bazel


sudo apt-get update&&sudo apt-get install bazel

Once installed, you can upgrade to a newer version of Bazel with:

sudo apt-get upgrade bazel


其实都是官网的安装步骤,亲身实践有效。


二、源码编译安装Tensorflow


1.克隆tensorflow源码

git clone--recurse-submodules https://github.com/tensorflow/tensorflow

2.配置tensorflow

进入到tensorflow目录下,有一个configure文件

输入:

./configure

3.配置选项

按如下规则配置tensorflow


You have bazel 0.6.1 installed.

Please specify the location of python. [Default is /usr/bin/python]:/usr/bin/python3

Found possible Python library paths:

/home/shengchun/tensorflow/models/

/bin

/usr/local/cuda/bin

/sbin

/home/shengchun/tensorflow/models/slim

/usr/bin

/usr/local/sbin

/usr/local/games

/usr/lib/python3/dist-packages

/usr/games

/home/shengchun/mxnet-ssd/mxnet/python

/usr/sbin

/usr/local/bin

/usr/local/lib/python3.5/dist-packages

Please input the desired Python library path to use.  Default is [/home/shengchun/tensorflow/models/]/usr/local/lib/python3.5/dist-packages

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]:[Enter]

jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]:[Enter]

Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]:[Enter]

Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]:[Enter]

No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]:[Enter]

No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]:[Enter]

No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL support? [y/N]:[Enter]

No OpenCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]:y

CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]:9.0

Please specify the location where CUDA 9.0 toolkit is

installed. Refer to README.md for more details. [Default is

/usr/local/cuda]:[Enter]

Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]:7.0.3

Please specify the location where cuDNN 7.0.3 library is

installed. Refer to README.md for more details. [Default is /usr/local/cuda]:[Enter]

**Please specify a list of comma-separated Cuda compute capabilities you want to build with.

You can find the compute capability of your device at:https://developer.nvidia.com/cuda-gpus.

Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0]**[Enter]

Do you want to use clang as CUDA compiler? [y/N]:[Enter]

nvcc will be used as CUDA compiler.

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:

Do you wish to build TensorFlow with MPI support? [y/N]:[Enter]

No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation

when bazel option “–config=opt” is specified [Default is -march=native]:[Enter]

Add “–config=mkl” to your bazel command to build with MKL support.

Please note that MKL on MacOS or windows is still not supported.

If you would like to use a local MKL instead of downloading,

please set the environment variable “TF_MKL_ROOT” every time before

build.

Configuration finished


4.编译tensorflow, 开启 GPU 支持:

bazelbuild--config=opt--config=cuda//tensorflow/tools/pip_package:build_pip_package

时间很长。






5.生成whl文件

bazel编译命令建立了一个名为build_pip_package的脚本。运行如下的命令将会在 /tmp/tensorflow_pkg路径下生成一个.whl文件:

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

6.pip安装tensorflow

sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp35-cp35m-linux_x86_64.whl

7.验证安装

打开任意一个新的终端,注意不要在tensorflow的安装路径下,运行

python3

输入以下代码

import tensorflow as tf

hello = tf.constant(‘Hello,TensorFlow!’)

sess = tf.Session()

print(sess.run(hello))

tensorflow源码编译安装完成。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容