教程:ubuntu16.04深度学习环境anaconda2+tendorflow(gpu)配置(GTX950M)

96
远行的土狗
2017.05.16 10:24* 字数 2921

ubuntu16.04+anaconda2+cuda8.0+cudnn5.1+tensorflow(gpu)的下载请百度,务必通过官网下载

没有GPU的的同学请忽略3/4/5/7这几个步骤,直接安装tensorflow(cpu)版本


1.制作ubuntu启动盘,

u盘安装ubuntu16.04+win10双系统制作教程百度很多,很方便安装ubuntu之前先在windows下分出一个空盘(磁盘管理显示是黑色的),安装时ubuntu会自动识别出此盘并将系统安装在此进入boot界面设置u盘为第一启动盘看到几个选项大家应该都会选,主要是安装前,我选择的是和window共存,自动分了两个盘


2.ubuntu安装之后稍微整理了一下系统(看个人选择,可以跳过。参考http://blog.csdn.net/skykingf/article/details/45267517)

先到<系统设置>--<软件和更新>--<ubuntu软件:下载自中国的服务器改为:mirror.aliyun.com>

再:

$ sudo apt-get update

$ sudo apt-get upgrade

2.1.删除libreoffice

$ sudo apt-get remove libreoffice-common

2.2.删除Amazon的链接

$ sudo apt-get remove unity-webapps-common  

3.删掉基本不用的自带软件

$ sudo apt-get remove thunderbird totem rhythmbox empathy brasero simple-scan gnome-mahjongg aisleriot gnome-mines cheese transmission-common gnome-orca webbrowser-app gnome-sudoku  landscape-client-ui-install 

$ sudo apt-get remove onboard deja-dup  

这样系统就基本上干净了。

2.4.安装Vim

$ sudo apt-get install vim 

2.5.安装chrome在终端中,输入以下命令:        

$ sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/

$ wget -q -O - https://dl.google.com/linux/linux_signing_key.pub  | sudo apt-key add -

如果顺利的话,命令将返回“OK”

$ sudo apt-get update$ sudo apt-get install google-chrome-stable

2.6.安装搜狗输入法

2.7.安装wps office

2.8.安装unrar

$ sudo apt-get install unrar 可以用命令解压rar压缩包

$ unrar x text.rar


3.安装NVIDIA驱动到<系统设置>--<软件更新>--<附加驱动>

选择NVIDIA Corporation:GM107M[GeForce GTX950M]

*使用NVIDIA binary driver -verison375.** 来自nvidia-375(专有)

应用更改安装完成后重启电脑,搜索NVIDIA,可以看到会有一个NVIDIA X Server Setting软件

此时NVIDIA驱动安装完成在终端输入nvidia-smi检测一下

出现这个界面则证明驱动安装成功


4.降级gcc和g++(参考http://blog.csdn.net/ice_moyan/article/details/50504798)

有些新版本cuda支持最新的5.版本的gcc,g++,可以忽略此步骤

由于Cuda不支持新版本的gcc和g++,所以建议先降级到4版本

我降级安装gcc/g++版本为4.8.x

(1). 下载gcc/g++ 4.8.x

$ sudo apt-get install -y gcc-4.8

$ sudo apt-get install -y g++-4.8

(2). 链接gcc/g++实现降级

$ cd /usr/bin

$ sudo rm gcc

$ sudo ln -s gcc-4.8 gcc

$ sudo rm g++

$ sudo ln -s g++-4.8 g++

此时在终端中输入gcc --version可以看到gcc版本已经为4.8.*,g++也一样


5.安装CUDA8.0

cd到cuda安装包所在位置

$ sudo sh cuda_8.0.61_375.26_linux.run

有一个很长的文件要阅读,按住回车不放一直到100%

直到出现

Do you accept the previously read EULA?accept/decline/quit:accept

(!!!注意)下面这个问你是否安装NVIDIA驱动一定要选择nInstall NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.26?(y)es/(n)o/(q)uit:n

Install the CUDA 8.0 Toolkit?(y)es/(n)o/(q)uit: y

Enter Toolkit Location[ default is /usr/local/cuda-8.0 ]: (直接按回车)

Do you want to install a symbolic link at /usr/local/cuda?(y)es/(n)o/(q)uit: y

Install the CUDA 8.0 Samples?(y)es/(n)o/(q)uit: y

Enter CUDA Samples Location [ default is /home/jam ]:(直接按回车)

 Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...

最后安装完成以后显示

============ Summary ============

Driver:       Not Selected

Toolkit:      Installed in /usr/local/cuda-8.0

Samples:  Installed in /home/jam, but missing recommended libraries

Please make sure that 

-  PATH includes /usr/local/cuda-8.0/bin

 -  LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.To install the driver using this installer, run the following command, replacingwith the name of this run file:    sudo.run -silent -driver

Logfile is /tmp/cuda_install_9480.log

忽略那个警告即可

安装完成以后添加cuda环境变量

在终端输入以下命令:

$ sudo gedit ~/.bashrc

在打开的文件的最后一行输入以下内容,然保存并退出:

export PATH=/usr/local/cuda-8.0/bin:$PATH

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:LD_LIBRARY_PATH

最后通过:source ~/.bashrc 命令来更新环境变量

测试cuda是否安装成功

在终端输入:nvcc -V,显示类似如下内容,则表示cuda安装成功:

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2016 NVIDIA Corporation

Built on Tue_Jan_10_13:22:03_CST_2017

Cuda compilation tools, release 8.0, V8.0.61


6.python环境安装(部分参考http://python.jobbole.com/86236/)

这里我们直接通过安装anaconda2来安装python环境,同样进入到anaconda文件存放目录,然后执行以下安装命令

$ bash Anaconda2-4.3.1-Linux-x86_64.sh

中间选项全部选默认

在最后

Do you wish the installer to prepend the Anaconda2 install location

to PATH in your /home/jam/.bashrc ? [yes|no]

[no] >>> yes

重启终端输入python即可看到使用的是基于anaconda的python

jam@jam:~$ python

Python 2.7.13 |Anaconda 4.3.1 (64-bit)| (default, Dec 20 2016, 23:09:15)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

Anaconda is brought to you by Continuum Analytics.

Please check out: http://continuum.io/thanks and https://anaconda.org

>>>

在安装包之前我们先为anaconda设置国内镜像如果需要安装很多packages,你会发现conda下载的速度经常很慢,因为Anaconda.org的服务器在国外。所幸的是,清华TUNA镜像源有Anaconda仓库的镜像,我们将其加入conda的配置即可。

# 添加Anaconda的TUNA镜像

$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

注意这是一句命令 channels后面有一个空格

# TUNA的help中镜像地址加有引号,需要去掉

# 设置搜索时显示通道地址

$ conda config --set show_channel_urls yes

执行完上述命令后,会生成~/.condarc(Linux/Mac)或C:UsersUSER_NAME.condarc文件,记录着我们对conda的配置,直接手动创建、编辑该文件是相同的效果。

接着我们可以通过conda添加一个新的环境安装tensorflow包,可以避免默认环境中包过多导致的不必要的错误

在终端直接输入:conda create -n tensorflow python=2.7

此时会创建一个名为tensorflow的新环境,python也版本为2.7,如果你需要其他版本的python,可以在创建环境时将Python版本改为3.5或3.6 例如 conda create -n python35 python=3.5  此时创建的新环境名为pytohn35里Python版本为3.5。

创建完成以后

激活创建的环境:source activate tensorflow

取消激活回到默认环境:source deactivate tensorflow

可以通过 conda info -e 命令查看安装的有几个环境

# conda environments:

#

tensorflow              /home/jam/anaconda2/envs/tensorflow

root                  *  /home/jam/anaconda2

其中带*的为当前环境

如果还要添加其它的python包,则可以通过以下两种命令来安装:

pip install [包名]


7.配置cudnn5.1

进入到存放cudnn目录,先执行解压命令:

$ tar -jvx cudnn-8.0-linux-x64-v5.1-ga.tgz

解压后会得到一个cuda的文件夹,先进入到cuda/include目录中,执行以下命令,将cudnn头文件添加到头文件目录中:

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*

#检测是否复制成功

添加cudnn环境

在终端输入:sudo gedit ~/.bashrc

在打开的文档末端添加

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"

(这里也是一句话 export 后面有个空格,可以参考其他环境格式)

export CUDA_HOME=/usr/local/cuda

保存

再在终端输入source ~/.bashrc


8.安装tensorflow(gpu)

建议将tensorflow下载下来用pip安装,在线安装因为网络原因可能会突然终中断要重新安装好几次

进入到tenforflow包所在文件,右键点击从找终端打开

如果要安装在你创建的新环境中,先激活你的环境再安装tensorflow

source avtivate tensorflow

pip install tensorflow_gpu-1.0.0-cp27-cp27mu-manylinux1_x86_64.whl

安装完成用pip list 可以列出所安装的所有包,可以看到tensorflow-gpu在其中

测试tensorflow

(tensorflow) jam@jam:~$ source deactivate tensorflow

jam@jam:~$ source activate tensorflow

(tensorflow) jam@jam:~$ python

Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:09:15)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

Anaconda is brought to you by Continuum Analytics.

Please check out: http://continuum.io/thanks and https://anaconda.org

>>> import tensorflow

>>> import tensorflow as tf

>>> hello = tf.constant('hello jam ')

>>> sess = tf.Session()

2017-05-14 17:46:02.142470: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.

2017-05-14 17:46:02.142497: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.

2017-05-14 17:46:02.142524: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

2017-05-14 17:46:02.142534: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.

2017-05-14 17:46:02.142542: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.

2017-05-14 17:46:02.236787: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:901] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero

2017-05-14 17:46:02.237025: I tensorflow/core/common_runtime/gpu/gpu_device.cc:887] Found device 0 with properties:

name: GeForce GTX 950M

major: 5 minor: 0 memoryClockRate (GHz) 1.124

pciBusID 0000:01:00.0

Total memory: 1.96GiB

Free memory: 1.49GiB

2017-05-14 17:46:02.237051: I tensorflow/core/common_runtime/gpu/gpu_device.cc:908] DMA: 0

2017-05-14 17:46:02.237058: I tensorflow/core/common_runtime/gpu/gpu_device.cc:918] 0:  Y

2017-05-14 17:46:02.237069: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 950M, pci bus id: 0000:01:00.0)

>>> print(sess.run(hello))

hello jam

>>>

其中的警告The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations等可以忽略

这些是warnings,不是error。这些warings的意思是说:你的机器上有这些指令集可以用,并且用了他们会加快你的CPU运行速度,但是你的TensorFlow在编译的时候并没有用到这些指令集。

至此,tensorflow环境已经配置完成,这是一艘船,一架飞机而已,要走向远方,这才刚刚开始。加油,青年(已不是少年啊~)。

日记本