Install TensorFlow from Source on Ubuntu 16.04(備忘錄)

安裝過程參考TensorFlow的官網(wǎng)和這個博客(https://alliseesolutions.wordpress.com/2016/09/08/install-gpu-tensorflow-from-sources-w-ubuntu-16-04-and-cuda-8-0-rc/

因為現(xiàn)在CUDA Toolkit 8.0的官方版本最高支持到ubuntu 16.04, 為了減少麻煩和不浪費時間,選擇安裝ubuntu 16.04 作為操作系統(tǒng)。

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), 然后按照官網(wǎng)的要求安裝,不要使用runfile(local), 使用runfile安裝幾次,均因為boot安全啟動設(shè)置而安裝失敗

在安裝之前,要用chmod +x cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb,修改文件的執(zhí)行權(quán)限

? ? ? ? ? ? ? ? ? ? ? ?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 虛擬環(huán)境

參考: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

生產(chǎn)安裝包和安裝

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

測試,參見官網(wǎng)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容