Ubuntu16.04+Tensorflow(GPU)安装教程

安装NVIDIA驱动

1、  通过GPU-Z等途径查看自己显卡型号

2、  通过NVIDIA官网查看自己显卡对应的驱动

网址:http://www.nvidia.com/Download/index.aspx?lang=en-us


3、  执行如下语句,安装

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

sudo apt-get update

sudo apt-get install nvidia-384

sudo apt-get install mesa-common-dev

sudo apt-get install freeglut3-dev

4、 重启系统

sudo reboot

5、 检查是否已经安装完毕

Nvidia-smi

Nvidia-settings

安装CUDA

1、 在官网下载CUDA的安装文件

网址:https://developer.nvidia.com/cuda-downloads

2、 配置环境

sudo vim /etc/profile

在文件末尾添加如下语句

export PATH=/usr/local/cuda-9.0/bin:$PATH

export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64$LD_LIBRARY_PATH

3、 重启电脑

sudo reboot

4、 测试CUDA的samples

cd /usr/local/cuda-9.0/samples/1_Utilities/deviceQuery

sudo make

./deviceQuery

如果显示GPU的相关信息,则说明安装成功

安装cuDNN

1、 官网下载cuDNN(需要注册以及填表)

网址:https://developer.nvidia.com/rdp/cudnn-download


2、 解压文件

tar –zxvf cudnn-9.0-linux-x64-v7.tgz

3、 复制文件

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

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

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

4、 生成符号链接

cd /usr/local/cuda/lib64

sudo rm –rf libcudnn.so libcudnn.so.7

sudo ln -s libcudnn.so.7.0.3 libcudnn.so.7

sudo ln -s libcudnn.so.7 libcudnn.so

5、 配置环境变量

vim ~/.bash_profile

在文件末尾添加

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

export CUDA_HOME=/usr/local/cuda

在terminal输入

Source ~/.bash_profile

安装Bazel

1、  安装Bazel依赖

英文版教程:https://www.bazel.io/versions/master/docs/install.html

sudo add-apt-repository ppa:webupd8team/java

sudo apt-get update

sudo apt-get install oracle-java8-installer

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

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

2、  下载Bazel源码并安装

网址:https://github.com/bazelbuild/bazel/releases

chmod +x bazel-0.7.0-installer-linux-x86_64.sh

./bazel-0.7.0-installer-linux-x86_64.sh --user

3、  安装第三方库

sudo apt-get install python-numpy swig python-dev python-wheel

sudo apt-get install git

git clone git://github.com/numpy/numpy.git numpy

安装Tensorflow

1、  下载Tensorflow

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

2、  配置Tensorflow

查看显卡计算能力:https://developer.nvidia.com/cuda-gpus

[email protected]:~/software/tensorflow$ ./configure

You have bazel 0.7.0 installed.

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

 

Found possible Python library paths:

 /usr/local/lib/python2.7/dist-packages

  /usr/lib/python2.7/dist-packages

Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]

 

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

jemalloc as malloc support will be enabled for TensorFlow.

 

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

No Google Cloud Platform support will be enabled for TensorFlow.

 

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

No Hadoop File System support will be enabled for TensorFlow.

 

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

No Amazon S3 File System support will be enabled for TensorFlow.

 

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

No XLA JIT support will be enabled for TensorFlow.

 

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

No GDR support will be enabled for TensorFlow.

 

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

No VERBS support will be enabled for TensorFlow.

 

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

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. [Leaveempty to default to CUDA 8.0]: 9.0

 

Please specify the location where CUDA 9.0 toolkit is installed. Refer toREADME.md for more details. [Default is /usr/local/cuda]:

 

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

 

Please specify the location where cuDNN 7.0.3 library is installed. Referto README.md for more details. [Default is /usr/local/cuda]:

 

Please specify a list of comma-separated Cuda compute capabilities youwant 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 significantlyincreases your build time and binary size. [Default is: 5.0]

 

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

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]: n

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]:

 

Add "--config=mkl" to your bazel command to build with MKLsupport.

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 setthe environment variable "TF_MKL_ROOT" every time before build.

Configuration finished

3、  编译目标程序,开启GPU支持

bazel build -c opt --config=cuda//tensorflow/cc:tutorials_example_trainer

bazel-bin/tensorflow/cc/tutorials_example_trainer--use_gpu

4、  创建pip包并安装

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

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

# .whl 文件的实际名字与你所使用的平台有关

pip install/tmp/tensorflow_pkg/tensorflow-0.5.0-cp27-none-linux_x86_64.whl

测试

从源代码树的根路径执行

cd tensorflow/examples/tutorials/mnist

python fully_connected_feed.py