安装过程参考TensorFlow的官网和这个博客(https://alliseesolutions.wordpress.com/2016/09/08/install-gpu-tensorflow-from-sources-w-ubuntu-16-04-and-cuda-8-0-rc/)
因为现在CUDA Toolkit 8.0的官方版本最高支持到ubuntu 16.04, 为了减少麻烦和不浪费时间,选择安装ubuntu 16.04 作为操作系统。
1. 安装必须的包
sudo apt-get install openjdk-8-jdk git python-dev python3-dev python-numpy python3-numpy build-essential python-pip python3-pip python-virtualenv swig python-wheel libcurl3-dev
2. 安装cuda-Toolkit 8.0
下载地址https://developer.nvidia.com/cuda-downloads
要下载 deb(local), 然后按照官网的要求安装,不要使用runfile(local), 使用runfile安装几次,均因为boot安全启动设置而安装失败
在安装之前,要用chmod +x cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb,修改文件的执行权限
sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
3. 下载cuDNN v5.1
https://developer.nvidia.com/cudnn 在下载之前,需要注册,然后选择下载cuDNN v5.1 Library for Linux
tar -xzvf cudnn-8.0-linux-x64-v5.1.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*
4. 修改 bash文件
gedit ~/.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
5.安装Bazel
先安装 curl: sudo apt install curl
echo "deb [arch=amd64]http://storage.googleapis.com/bazel-aptstable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg| sudo apt-key add -
sudo apt-get update
sudo apt-get install bazel
sudo apt-get upgrade bazel
6. 安装Python 虚拟环境
参考:http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/
sudo pip install virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
source~/.bashrc
mkvirtualenv py27 -p python2
deactivate
mkvirtualenv py35 -p python3
7. 安装TensorFlow
克隆TensorFlow: git clonehttps://github.com/tensorflow/tensorflow
./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3.5
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
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 the XLA just-in-time compiler (experimental)? [y/N] N
Found possible Python library paths:
/usr/local/lib/python3.5/dist-packages
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 which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to use system default]: 5
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
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: "3.5,5.2"]: 3.0
Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished
编译(支持GPU)
bazel build -c opt --config=cuda /tensorflow/tools/pip_package:build_pip_package
生产安装包和安装
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.0.0-cp35-cp35m-linux_x86_64.whl
测试,参见官网