注:官方已经不支持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。