Ubuntu17.04(16.04)+Nvidia GT 640LE+CUDA9.0+cuDNN7.05+Tensorflow1.5(GPU)+Anaconda5.01(python3.6)配置安装
注:官方已经不支持ubuntu17.04了,所以将环境迁移到ubuntu16.04下,同样的编译环境编译成功。
一、详细的安装环境:
1、硬件环境:Intel酷睿3代i5(3210m-cpu)/Geforce GT 640m-LE
2、软件环境:Ubuntu17.04(64bit)/gcc6.x/bazel0.9.0/CUDA9.0/CUDAnn7.05/tensorflow1.5/Anaconda5.01(python3.6/spyder3.2.6)
3、支持指令集:sse3、sse4.1、sse4.2、avx
二、下载并安装NVIDIA CUDA Tookit 9
1、下载: 由于官方下载版本更新为9.1,此新版本本人没有测试过是否被tensorflow1.5所支持,所以仍用9.0版本,下载地址为:https://developer.nvidia.com/cuda-90-download-archive
另提供9.0网盘分享地址供下载:https://pan.baidu.com/s/1qZNJurE 密码:p3nq
2、安装:下载后的文件名为cuda-repo-ubuntu1704-9.0-local_9.0.176-1_amd64.deb,打开终端模拟器,输入:(经测试,cuda-ubuntu17.04版安装在ubuntu16.04上没影响,编译后的tensorflow是否有问题还有待测试)
sudo dpkg -i cuda-repo-ubuntu1704-9.0-local_9.0.176-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu1704-9.0-local_9.0.176-1_amd64/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda
安装的过程中可能会更新NV显卡驱动,按提示更新即可。
3、更新个性化设置文件.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使配置生效。
三、下载并安装cuDNN7.05
1、下载:下载cuDNN的地址(网盘中也有分享):
下载后文件名为cudnn-9.0-linux-x64-v7.tgz
2、安装:打开终端模拟器,进入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 -acuda/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*
四、安装Anaconda
1、下载:因为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
2、安装:在终端模拟器内输入以下命令,增加可执行权限,并安装:
chmod a+x ./Anaconda3-5.0.1-Linux-x86_64.sh
./Anaconda3-5.0.1-Linux-x86_64.sh
3、安装步骤需要注意:
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//.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错误。
五、安装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
2、安装:终端运行:
六、编译并安装Tensorflow1.5(GPU版)
这有我本人根据自己的环境编译好的版本:https://pan.baidu.com/s/1jJNV8BO 密码: 4as4
1、下载:终端中执行:(如果没有安装git则执行:sudo apt-get install git安装git)
git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout r1.5
2、配置编译选项:
./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
如果选择错了,就中断再来配置一遍。
3、编译:
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指令集,我只选了sse3、sse4.1、sse4.2、avx,因为我的cpu不支持avx2和fma指令集所以没加这两个选项)
注:这步操作比较耗时。结束后,会在路径tensorflow/tools/pip_package下产生一个脚本build_pip_package。这个脚本是用于产生“.whl”包文件的。
4、打包:
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
5、安装:
先在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
七、anaconda图形界面安装配置tensorflow环境
1、启动anaconda:
打开终端模拟器输入命令:
anaconda-navigator
启动后界面如下:
选择左侧栏第二选项(Environments)
由于我已经配置过了,所以有两个选项anaconda3和tensorflow。第一次打开只有一个root选项。
选择中间栏目下边的带加号图标的Create按钮,创建新的环境:
在弹出的窗口输入tensorflow然后点Create,等待配置完成,创建完之后就是第二张图的样子了
然后鼠标左键点击tensorflow,在右侧栏上边第一个选择框那选择Not installed:
用鼠标滚轮向下找到spyder,点击在前面的选择框,右下角点击apply安装spyder,安装完将其升级到最新版3.2.6。
以后使用spyder开发环境就可以直接在终端输入spyder。