# TensorFlow 安装配置
---
## 现有的机器环境
> `CentOS 7 (Linux 3.10.0-327.el7.x86_64)`
> `gcc 4.8.5`
> `glibc 2.17`
> `cuda 7.5`
> 没有 `g++`、`libstdc++`、`cudnn`、`Java 1.8` 和 `bzip2`、`unzip`、`swig`、`zlib-devel`
> 因为使用 `Anaconda` 提供 `python` 环境,所以并不依赖系统的 `python 2.7.5`
## 先使用管理员权限做一些必要的准备工作
### 1. 设置新用户 danliu
```
[root ~]# useradd danliu
[root ~]# passwd danliu
```
### 2. 安装必备软件
#### g++ 4.8.5
```
[root ~]# yum install gcc-c++
```
libstdc++ 会作为依赖项一并安装
### bzip2, unzip, swig 和 Java 1.8
```
[root ~]# yum install bzip2 unzip swig zlib-devel java-1.8.0-openjdk-devel
```
### package.zip
[root ~]# cd /home/
[root ~]# unzip packages.zip
[root ~]# cd /packages/
[root ~]# mv * /home/danliu
[root ~]# cd ..
[root ~]# rm -rf packages
[root ~]# cd /home/danliu/
### bazel
bazel 作为构建工具,安装在系统里更方便。
```
[root ~]# chmod +x bazel-0.3.1-installer-linux-x86_64.sh
[root ~]# ./bazel-0.3.1-installer-linux-x86_64.sh
```
## 使用普通账户 danliu 登陆并继续接下来的安装
### 安装 Anaconda
```
[root ~]# chown -R danliu:danliu /home/danliu
[root ~]# su danliu
[danliu ~]$ chmod +x Anaconda2-4.1.1-Linux-x86_64.sh
[danliu ~]$ ./Anaconda2-4.1.1-Linux-x86_64.sh
Do you wish the installer to prepend the Anaconda2 install location
to PATH in your /home/danliu/.bashrc ? [yes|no]
[no] >>> yes
```
默认的安装目录是 `~/anaconda2/` (`/root/anaconda2`)
安装完成后
```
source ~/.bashrc
```
### 添加已有的 cuda 7.5 安装
和下面的安装 cudnn v5 一起做
### 安装 cudnn v5
```
[danliu ~]$ tar -xzvf cudnn-7.5-linux-x64-v5.0-ga.tar.gz
[danliu ~]$ mv cuda/ cudnnv5
```
把下面的内容添加到 `~/.bashrc` 里
```
# CUDA
vim ~/.bashrc
export PATH=/usr/local/cuda-7.5/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$HOME/cudnnv5/lib64:$LD_LIBRARY_PATH
```
### 修改 TensorFlow 的一处 bug
TensorFlow 下的 `third_party/gpus/crosstool/CROSSTOOL` 存在一个可能导致构建出错的问题,如果使用 Ubuntu 14.04 可以无视,使用 CentOS 7 则必须要修改
```
[danliu ~]$ unzip tensorflow-src.zip
[danliu ~]$ mv tensorflow-master tensorflow-cudnnv5
[danliu ~]$ cd tensorflow-cudnnv5
[danliu ~]$ vi third_party/gpus/crosstool/CROSSTOOL
```
在第 66 行和第 67 行之间添加一行`cxx_builtin_include_directory: "/usr/local/cuda-7.5/include"`
```
@@ -64,6 +64,7 @@
cxx_builtin_include_directory: "/usr/lib/gcc/"
cxx_builtin_include_directory: "/usr/local/include"
cxx_builtin_include_directory: "/usr/include"
+ cxx_builtin_include_directory: "/usr/local/cuda-7.5/include"
tool_path { name: "gcov" path: "/usr/bin/gcov" }
# C(++) compiles invoke the compiler (as that is the one knowing where
```
### 配置 TensorFlow
```
[danliu *]$ ./configure
```
注意 cudnn 的路径是之前设置的 `/home/danliu/cudnnv5`
> ERROR: Problem getting numpy include path. Is numpy installed?
### 编译和测试
```
# 下面的 // 不是注释是一行
[danliu *]$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer
[danliu *]$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
```
### 构建可以迁移的 tensorflow wheel 包
```
[danliu *]$ bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
[danliu *]$ bazel-bin/tensorflow/tools/pip_package/build_pip_package ./tensorflow_pkg
```
> bazel-bin/tensorflow/tools/pip_package/build_pip_package:行65: rsync: 未找到命令