本文記錄Ubuntu 16.04安裝Tensorflow步驟,也包括怎麼從原始碼編譯安裝Tensorflow。
要想安裝Tensorflow GPU版本,你需要有一個新一點的Nvidia顯示卡。
Tensorflow CPU版本的安裝
|
$sudo apt-getinstall Python-pip python-dev python-virtualenv # python 2.7
$sudo apt-getinstall 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, CPU only, 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, CPU only, 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 shcuda_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 chmoda+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
|
使環境變數生效:
下載tensorflow原始碼:
|
$cd~
$git clonehttps://github.com/tensorflow/tensorflow
|
安裝一些編譯和依賴工具:
|
$sudo apt-getinstall 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-getupdate
$sudo apt-getinstall bazel
$sudo apt-getupgrade 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-copt--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
|
參考:
本文永久更新連結地址:http://www.linuxidc.com/Linux/2017-04/143115.htm