注:官方已經不支持ubuntu17.04了,因此將環境遷移到ubuntu16.04下,一樣的編譯環境編譯成功。java
1、詳細的安裝環境:python
一、硬件環境:Intel酷睿3代i5(3210m-cpu)/Geforce GT 640m-LE
二、軟件環境:Ubuntu17.04(64bit)/gcc6.x/bazel0.9.0/CUDA9.0/CUDAnn7.05/tensorflow1.5/Anaconda5.01(python3.6/spyder3.2.6)linux
三、支持指令集:sse三、sse4.一、sse4.二、avxgit
2、下載並安裝NVIDIA CUDA Tookit 9github
一、下載: 因爲官方下載版本更新爲9.1,此新版本本人沒有測試過是否被tensorflow1.5所支持,因此仍用9.0版本,下載地址爲:https://developer.nvidia.com/cuda-90-download-archiveweb
另提供9.0網盤分享地址供下載:https://pan.baidu.com/s/1qZNJurE 密碼:p3nqubuntu
二、安裝:下載後的文件名爲cuda-repo-ubuntu1704-9.0-local_9.0.176-1_amd64.deb,打開終端模擬器,輸入:(經測試,cuda-ubuntu17.04版安裝在ubuntu16.04上沒影響,編譯後的tensorflow是否有問題還有待測試)vim
sudo dpkg -i cuda-repo-ubuntu1704-9.0-local_9.0.176-1_amd64.debwindows
sudo apt-key add /var/cuda-repo-ubuntu1704-9.0-local_9.0.176-1_amd64/7fa2af80.pubbash
sudo apt-get update
sudo apt-get install cuda
安裝的過程當中可能會更新NV顯卡驅動,按提示更新便可。
三、更新個性化設置文件.bashrc:
在終端模擬器輸入:vim ~/.bashrc
將如下內容粘貼到文件結尾:
export PATH="/usr/local/cuda/bin:$PATH:+:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
export CUDA_HOME="/usr/local/cuda:$CUDA_HOME"
而後輸入:wq ,保存退出,最後運行 source ~/.bashrc使配置生效。
3、下載並安裝cuDNN7.05
一、下載:下載cuDNN的地址(網盤中也有分享):
https://developer.nvidia.com/cudnn
下載後文件名爲cudnn-9.0-linux-x64-v7.tgz
二、安裝:打開終端模擬器,進入cudnn-9.0-linux-x64-v7.tgz文件所在的目錄,依次執行如下命令:
tar -zxvf cudnn-9.0-linux-x64-v7.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp -a cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
4、安裝Anaconda
一、下載:由於ubuntu17.04自帶python2.7,可是咱們要用3.6版的,因此要安裝Anaconda面向python3.6的版本。到https://www.anaconda.com/download/#linux下載linux安裝包,對應python3.6
下載後文件名爲Anaconda3-5.0.1-Linux-x86_64.sh
二、安裝:在終端模擬器內輸入如下命令,增長可執行權限,並安裝:
chmod a+x ./Anaconda3-5.0.1-Linux-x86_64.sh
./Anaconda3-5.0.1-Linux-x86_64.sh
三、安裝步驟須要注意:
1)彈出 「In order to continue the installation process, please review the license agreement.」 時,點回車看license,再拉到底,點Yes。
2)回車接受default install location。
3)提示 「Do you wish the installer to prepend the Anaconda<2 or 3> install location to PATH in your /home/<user>/.bashrc ?」 時,輸入Yes
4)提示「Thank you for installing Anaconda3!」時表示安裝成功。
5)安裝程序自動在~/.bashrc里加入了PATH環境變量,讓配置生效,終端執行:
source ~/.bashrc
6)更新setuptools,終端執行:
pip install --upgrade -I setuptools
不然,安裝後tensorflow後,執行import tensorflow as tf時,會出現
ImportError: No module named platflom 錯誤。
5、安裝Bazel(Bazel須要java8以上的環境,ubuntu17.04默認Java8)
這裏咱們安裝java9:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java9-installer
一、下載:下載Bazel0.9的sh版(bazel-0.9.0-installer-linux-x86_64.sh)下載連接:
https://github.com/bazelbuild/bazel/releases
二、安裝:終端運行:
sudo chmod +x bazel-0.9.0-installer-linux-x86_64.sh
sudo ./bazel-0.9.0-installer-linux-x86_64.sh
6、 編譯並安裝Tensorflow1.5(GPU版)
這有我本人根據本身的環境編譯好的版本:https://pan.baidu.com/s/1jJNV8BO 密碼: 4as4
一、下載:終端中執行:(若是沒有安裝git則執行:sudo apt-get install git安裝git)
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout r1.5
二、配置編譯選項:
./configure
Please specify the location of python. [Default is /home/ceiec/anaconda3/envs/tensorflow/bin/python]:
Found possible Python library paths:
/home/ceiec/anaconda3/envs/tensorflow/lib/python3.6/site-packages
Please input the desired Python library path to use. Default is [/home/ceiec/anaconda2/envs/tensorflow/lib/python3.6/site-packages]
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: Y (也能夠選n,我選的Y)
jemalloc as malloc support will be enabled for TensorFlow.
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 Amazon S3 File System support? [Y/n]: n
No Amazon S3 File System support will be enabled for TensorFlow.
Do you wish to build TensorFlow with XLA JIT support? [y/N]: N
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with GDR support? [y/N]: N
No GDR support will be enabled for TensorFlow.
Do you wish to build TensorFlow with VERBS support? [y/N]: N
No VERBS support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL support? [y/N]:N
No OpenCL support will be enabled for TensorFlow.
(若是選opencl,則configure會檢查opencl相關的文件,這裏不須要,因此選擇No)
Do you wish to build TensorFlow with CUDA support? [y/N]:Y (這裏是必須選Y)
CUDA support will be enabled for TensorFlow.
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]:9.0
Please specify the location where CUDA 9.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 default to cuDNN 6.0]:7
(上面輸入7不能是7.0,不然會報錯並提示你從新選)
Please specify the location where cuDNN 7 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.0]3.0
(兼容性等級填3.0)
Do you want to use clang as CUDA compiler? [y/N]:n (咱們用gcc編譯,不用clang編譯,因此選擇n)
nvcc will be used as CUDA compiler.
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Do you wish to build TensorFlow with MPI support? [y/N]: N
No MPI support will be enabled for TensorFlow.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Add "--config=mkl" to your bazel command to build with MKL support.
Please note that MKL on MacOS or windows is still not supported.
If you would like to use a local MKL instead of downloading, please set the environment variable "TF_MKL_ROOT" every time before build.
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: N
Not configuring the WORKSPACE for Android builds.
Configuration finished
若是選擇錯了,就中斷再來配置一遍。
三、編譯:
bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --config=opt //tensorflow/tools/pip_package:build_pip_package
(或者:bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma --config=opt //tensorflow/tools/pip_package:build_pip_package
增長更多的cpu指令集,我只選了sse三、sse4.一、sse4.二、avx,由於個人cpu不支持avx2和fma指令集因此沒加這兩個選項)
注:這步操做比較耗時。結束後,會在路徑tensorflow/tools/pip_package下產生一個腳本build_pip_package。這個腳本是用於產生「.whl」包文件的。
四、打包:
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
注:上面命令執行結束後,會在目錄/tmp/tensorflow_pkg下產生文件:tensorflow-1.5.0-cp36-cp36m-linux_x86_64.whl
五、安裝:
先在anaconda下建立一個tensorflow環境(也能夠打開anaconda圖形配置界面來建立環境,參看七)
source ~/anaconda3/bin/activate tensorflow
而後在這個tensorflow下安裝tensorflow
pip install /tmp/tensorflow_pkg/tensorflow-1.5.0-cp36-cp36m-linux_x86_64.whl
安裝spyder3圖形開發環境
pip install spyder
啓動spyder
spyder
7、anaconda圖形界面安裝配置tensorflow環境
一、啓動anaconda:
打開終端模擬器輸入命令:
anaconda-navigator
啓動後界面以下:
選擇左側欄第二選項(Environments)
因爲我已經配置過了,因此有兩個選項anaconda3和tensorflow。第一次打開只有一個root選項。
選擇中間欄目下邊的帶加號圖標的Create按鈕,建立新的環境:
在彈出的窗口輸入tensorflow而後點Create,等待配置完成,建立完以後就是第二張圖的樣子了
而後鼠標左鍵點擊tensorflow,在右側欄上邊第一個選擇框那選擇Not installed:
用鼠標滾輪向下找到spyder,點擊在前面的選擇框,右下角點擊apply安裝spyder,安裝完將其升級到最新版3.2.6。
之後使用spyder開發環境就能夠直接在終端輸入spyder。