原文链接:
http://blog.topspeedsnail.com/archives/tag/tensorflow本文记录Ubuntu 16.04安装Tensorflow步骤,也包括怎么从源码编译安装Tensorflow。
要想安装Tensorflow GPU版本,你需要有一个新一点的Nvidia显卡。
Tensorflow cpu版本的安装
$ sudo apt-get install python-pip python-dev python-virtualenv # python 2.7 $ sudo apt-get install python3-pip python3-dev python3-virtualenv # python 3.4+
使用虚拟环境(可选):Python虚拟环境(pyvenv、virtualenv)
$ virtualenv --system-site-packages ~/tensorflow $ source ~/tensorflow/bin/activate
# Ubuntu/Linux 64-bit,cpu only,Python 2.7 $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl # Ubuntu/Linux 64-bit,Python 3.4 $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp34-cp34m-linux_x86_64.whl # Ubuntu/Linux 64-bit,Python 3.5 $ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp35-cp35m-linux_x86_64.whl
安装Tensorflow:
# Python 2 $ sudo pip install --upgrade $TF_BINARY_URL # Python 3 $ sudo pip3 install --upgrade $TF_BINARY_URL
如果要升级Tensorflow,替换新版本的TF_BINARY_URL。https://www.tensorflow.org
编译安装Tensorflow(GPU支持)
安装NVidia显卡驱动,你可以在Ubuntu内置的附加驱动中安装。
$ sudo add-apt-repository ppa:graphics-drivers/ppa $ sudo apt update
安装CUDA:
#下载安装: #https://developer.nvidia.com/cuda-toolkit $ sudo sh cuda_8.0.44_linux.run --override # 安装位置: /usr/local/cuda # 默认仓库中的版本较旧 #$ sudo apt install nvidia-cuda-toolkit nvidia-cuda-dev # 安装位置: /usr
安装CudNN V5:https://developer.nvidia.com/cudnn
# 下载CudNN 5.1 for Cuda 8.0 $ sudo 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*
在~/.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
下载tensorflow源码:
$ cd ~ $ git clone https://github.com/tensorflow/tensorflow
安装一些编译和依赖工具:
$ sudo apt-get install default-jdk python-dev python3-dev python-numpy python3-numpy build-essential python-pip python3-pip python-virtualenv swig python-wheel libcurl3-dev
安装Bazel:
$ echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list $ curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add - $ sudo apt-get update $ sudo apt-get install bazel $ sudo apt-get upgrade bazel
配置编译选项:
$ cd ~/tensorflow $ ./configure
需要输入Python路径,默认是/usr/bin/python。如果你使用Python3,输入:/usr/bin/python3.5。
输入Python模块路径,默认是/usr/local/lib/python2.7/dist-packages。如果你使用Python3,输入:/usr/local/lib/python3.5/dist-packages。
输入Cuda SDK版本和Cudnn版本:8.0、5.1.5。
配置完成,输入如下信息:
INFO: All external dependencies fetched successfully. Configuration finished
编译tensorflow:
$ bazel build -c opt --config=cuda # GPU支持 # cpu支持 #$ bazel build -c opt
构建pip包:
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
安装pip包:
$ sudo pip install /tmp/tensorflow_pkg/tensorflow # python2 $ sudo pip3 install /tmp/tensorflow_pkg/tensorflow # python3
参考:
- https://www.tensorflow.org/versions/r0.11/get_started/os_setup.html#download-and-setup
- tensorflow源代码:https://github.com/tensorflow/tensorflow