原文鏈接:
http://blog./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+
使用虛擬環(huán)境(可選):Python虛擬環(huán)境(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./tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp27-none-linux_x86_64.whl
-
- # Ubuntu/Linux 64-bit, CPU only, Python 3.4
- $ export TF_BINARY_URL=https://storage./tensorflow/linux/cpu/tensorflow-0.11.0rc1-cp34-cp34m-linux_x86_64.whl
-
- # Ubuntu/Linux 64-bit, CPU only, Python 3.5
- $ export TF_BINARY_URL=https://storage./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(GPU支持)
安裝NVidia顯卡驅(qū)動,你可以在Ubuntu內(nèi)置的附加驅(qū)動中安裝。
- $ sudo add-apt-repository ppa:graphics-drivers/ppa
- $ sudo apt update
安裝CUDA:
- #下載安裝:
- #https://developer./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./cudnn
- <a target="_blank" href="https://developer./cudnn" style="border-width:0px 0px 1px; border-bottom-style:solid; border-color:rgb(46,34,86); font-family:inherit; font-style:inherit; font-weight:inherit; margin:0px; outline:0px; padding:0px; vertical-align:baseline; color:rgb(46,34,86); text-decoration:none"># 下載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*</a>
在~/.bashrc文件中添加環(huán)境變量:
- export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
- export CUDA_HOME=/usr/local/cuda
使環(huán)境變量生效:
下載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./bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
- $ curl https://storage./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
構(gòu)建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./versions/r0.11/get_started/os_setup.html#download-and-setup
-
tensorflow源代碼:https://github.com/tensorflow/tensorflow
Share the post "Ubuntu 16.04 安裝 Tensorflow(GPU支持)"
|