Linux下的CUDA9.0和cuDnn7.1环境下TensorFlow源码安装

参考官网教程https://tensorflow.google.cn/install/source

1. 安装依赖包

    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_applications==1.0.6 --no-deps
    pip install -U --user keras_preprocessing==1.0.5 --no-deps
   

2. 安装Bazel

    2.1 安装需要的包

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python

   2.2 下载Bazel

          点击bazel-0.25.3-installer-linux-x86_64.sh下载

   2.3  运行sh文件(命令中对应版本号)

chmod +x bazel--installer-linux-x86_64.sh
./bazel--installer-linux-x86_64.sh --user

   2.4 设置环境变量

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

3. 下载TensorFlow源代码

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

4. 配置编译系统

./configure

其中部分选项如下:

Please specify the location of python. [Default is /home/jerry/anaconda3/bin/pyt                          hon]: /usr/bin/python2.7


Found possible Python library paths:
  /usr/lib/python2.7/dist-packages
  /usr/local/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/lib/pytho                          n2.7/dist-packages]

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 OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm 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.

Do you wish to build TensorFlow with TensorRT support? [y/N]: n
No TensorRT support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to                           CUDA 10]: 9.0


Please specify the cuDNN version you want to use. [Leave empty to default to cuD                          NN 7]: 7.1


Please specify the locally installed NCCL version you want to use. [Leave empty                           to use http://github.com/nvidia/nccl]:


Please specify the comma-separated list of base paths to look for CUDA libraries                           and headers. [Leave empty to use the default]:

Please specify a list of comma-separated CUDA compute capabilities you want to b                          uild 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, and that TensorFlow only supports compute capabilit                          ies >= 3.5 [Default is: 6.1,6.1]:


Do you want to use clang as CUDA compiler? [y/N]:
nvcc will be used as CUDA compiler.

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


Do you wish to build TensorFlow with MPI support? [y/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 -Wno-sign-compare]:


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:                          
Not configuring the WORKSPACE for Android builds.

这里要注意CUDA和cudnn的配置问题

参考https://blog.csdn.net/ture_dream/article/details/52677619

如果之前没配置好,这里可能会出现如下找不到对应的cudnn的问题

Could not find any libcudnn.so.7* in any subdirectory:
        ''
        'lib64'
        'lib'
        'lib/*-linux-gnu'
        'lib/x64'
        'extras/CUPTI/*'
of:
        '/lib/x86_64-linux-gnu'
        '/usr'
        '/usr/lib'
        '/usr/lib/x86_64-linux-gnu'
        '/usr/lib/x86_64-linux-gnu/libfakeroot'
        '/usr/lib/x86_64-linux-gnu/mesa'
        '/usr/lib/x86_64-linux-gnu/mesa-egl'
        '/usr/local/cuda'
Asking for detailed CUDA configuration...

5. 编译pip包

bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package --local_resources 2048,.5,1.0 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"

这里可能需要很久很久

6. 编译软件包

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

7. 安装软件包(注意这里的tensorflow-version-tags.whl应该对应上一步编译好的whl包的名字)

    pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl
 

到这里就安装完成了!!

你可能感兴趣的:(linux)