ubuntu16.04.6 LTS配置CUDA10.0+cuDNN7.6+Tensorflow2.0

ubuntu16.04.6LTS 配置CUDA10.0+cuDNN7.6+Tensorflow2.0

-------电脑配置-------

1、系统:ubuntu16.04.6 LTS

dhh@dhh-HP-PC:~$ cat /etc/issue
Ubuntu 16.04.6 LTS \n \l

2、内核版本

dhh@dhh-HP-PC:~$ uname -r
4.4.0-169-generic

系统默认内核为linux-image-4.15.0-70-generic ,因要与CUDA10.0版本对应,后修改内核为linux-image-4.4.0-169-generic

dhh@dhh-HP-PC:~$ sudo dpkg --get-selections |grep linux-image
[sudo] dhh 的密码: 
linux-image-4.15.0-29-generic			deinstall
linux-image-4.15.0-70-generic			install
linux-image-4.4.0-169-generic			install
linux-image-generic				install
linux-image-generic-hwe-16.04			install
linux-image-unsigned-4.15.0-29-generic		install

3、显卡型号
显卡:Nvidia Gefore 830M
驱动:Driver Version: 440.31
在NVIDIA官网下载的最新驱动,手动安装
4、gcc版本

dhh@dhh-HP-PC:~$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

5、python版本

dhh@dhh-HP-PC:~$ python --version
Python 2.7.12

-------安装过程-------

一、查看nvidia driver/CUDA/cudnn之间的配置版本对应关系

CUDA官网 CUDA Toolkit v10.0.130的安装指导
1、与CUDA10.0对应的ubuntu16.04的内核需要Kernel* 4.4, GCC版本为5.4.0
在这里插入图片描述
2、与CUDA10.0对应的nvidia驱动的版本为 >=410.48
在这里插入图片描述
3、CUDA10.0对应的cudnn版本

  1. CUDA And NVIDIA Hardware For cuDNN
  2. cuDNN 下载

在这里插入图片描述

二、安装驱动

1、更改内核
查看当前正在使用的内核

dhh@dhh-HP-PC:~$ uname -r
4.15.0-70-generic

查看当前安装的内核版本

dpkg -l|grep linux-image

查看可以更新的内核版本,有低板本的4.4.0-169,直接切换

dhh@dhh-HP-PC:~$ dpkg -l|grep linux-image
rc  linux-image-4.15.0-29-generic                   4.15.0-29.31~16.04.1                                  amd64        Signed kernel image generic
ii  linux-image-4.15.0-70-generic                   4.15.0-70.79~16.04.1                                  amd64        Signed kernel image generic
ii  linux-image-4.4.0-169-generic                   4.4.0-169.198                                         amd64        Signed kernel image generic
ii  linux-image-generic                             4.4.0.169.177                                         amd64        Generic Linux kernel image
ii  linux-image-generic-hwe-16.04                   4.15.0.70.90                                          amd64        Generic Linux kernel image
ii  linux-image-unsigned-4.15.0-29-generic          4.15.0-29.31~16.04.1                                  amd64        Linux kernel image for version 4.15.0 on 64 bit x86 SMP
    4.4.0-77-generic

重启按Esc选择Ubuntu高级选项,进入4.4的内核,
可以卸载不要的内核

sudo apt-get purge linux-image-xx-xx-generic linux-image-extra-xx-xx-generic

更新grub引导

sudo update-grub

导致的缺点是每次进系统都要选择高级选项,再选择4.4的内核才进入系统,有点麻烦

2、去官网下载适合自己的显卡驱动
NVIDIA 驱动程序下载
下图是我的显卡对应的驱动,我直接下载陆最新版440.31,符合>=410.48 这一条件
下载完最好放到一个容易寻找的英文目录下,例如 /home/dhh/download
在这里插入图片描述在这里插入图片描述3、ubuntu 16.04默认安装了第三方开源的驱动程序nouveau,安装nvidia显卡驱动首先需要禁用nouveau,不然会碰到冲突的问题,导致无法安装nvidia显卡驱动。我之前使用的是384.130,直接在设置中修改的,此次仍想直接在设置中更换显卡驱动,试过其他版本的,但是更换之后nvdia-smi报错。故使用下载nvidia驱动,并关闭图形化界面安装的方式。

  • 添加nvidia驱动的源
sudo add-apt-repository ppa:graphics-drivers/ppa && sudo apt update

在这里插入图片描述4、禁用驱动程序nouveau
编辑文件blacklist.conf

sudo gedit /etc/modprobe.d/blacklist.conf

(当然可以使用vim:sudo vim /etc/modprobe.d/blacklist.conf,这里只是我习惯用gedit)
在文件最后部分插入以下两行内容
blacklist nouveau
options nouveau modeset=0

更新系统

sudo update-initramfs -u

重启系统(关机重启)

sudo reboot

验证nouveau是否已禁用,若没有任何输出,则为禁用成功

lsmod | grep nouveau

5、安装nvidia驱动
Ctrl-Alt+F1 进入命令行界面
关闭当前图形环境令

sudo /etc/init.d/lightdm stop 

给驱动run文件赋予执行权限

sudo chmod a+x NVIDIA-Linux-x86_64-375.66.run 

安装驱动,记得下载的驱动存放的文件路径

sudo /home/dhh/download/NVIDIA-Linux-x86_64-440.31.run --no-x-check --no-nouveau-check --no-opengl-files  # 这句一定要加参数,不然就会循环登录。

重新打开图形化界面

sudo service lightdm start

重启reboot
验证显卡驱动的版本,显示确实为440.31

dhh@dhh-HP-PC:~$ cat /proc/driver/nvidia/version 
NVRM version: NVIDIA UNIX x86_64 Kernel Module  440.31  Sun Oct 27 02:19:35 UTC 2019
GCC version:  gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) 

二、安装CUDA10.0

1、下载CUDA 10.0
官网下载 CUDA 10.0 我下载的是deb格式的,也可以下载runfile安装
在这里插入图片描述2、安装,在下载按钮旁边有Installation Instructions说明,按照步骤来
软件包管理器安装deb文件

sudo dpkg -i cuda-repo-ubuntu1604-10-0-local-10.0.130-410.48_1.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-<version>/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda

3、添加环境变量

sudo gedit ~/.bashrc

打开 .bashrc文件在最后添加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.0/lib64
export PATH=$PATH:/usr/local/cuda-10.0/bin
export CUDA_HOME=$CUDA_HOME:/usr/local/cuda-10.0

刷新环境

sudo gedit ~/.bashrc

4、测试

输入nvcc -V,显示cuda版本信息

dhh@dhh-HP-PC:~$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130
dhh@dhh-HP-PC:~$ 

运行测试

cd /usr/local/cuda-10.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery

有结果如下,说明安装成功

dhh@dhh-HP-PC:/usr/local/cuda-10.0/samples/1_Utilities/deviceQuery$ sudo ./deviceQuery 
[sudo] dhh 的密码: 
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce 830M"
  CUDA Driver Version / Runtime Version          10.2 / 10.0
  CUDA Capability Major/Minor version number:    5.0
  Total amount of global memory:                 2004 MBytes (2101870592 bytes)
  ( 2) Multiprocessors, (128) CUDA Cores/MP:     256 CUDA Cores
  GPU Max Clock rate:                            1150 MHz (1.15 GHz)
  Memory Clock rate:                             900 Mhz
  Memory Bus Width:                              64-bit
  L2 Cache Size:                                 1048576 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 1 copy engine(s)
  Run time limit on kernels:                     No
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device supports Compute Preemption:            No
  Supports Cooperative Kernel Launch:            No
  Supports MultiDevice Co-op Kernel Launch:      No
  Device PCI Domain ID / Bus ID / location ID:   0 / 10 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.2, CUDA Runtime Version = 10.0, NumDevs = 1
Result = PASS

三、配置cuDNN7.6

1、下载cuDNN
官网下载cuDNN
选择与CUNDA 10.0匹配的
Download cuDNN v7.6.5 (November 5th, 2019), for CUDA 10.1
/home/dhh/桌面/111下载之后,进入存放的文件夹之中,将文件解压得到cuda文件夹,进入cuda文件夹,在终端中打开,将解压出的文件中的两个.h头文件拷贝到cuda-10.0/lib64和cuda-10.0/include下面,#添加可执行权限
(/usr/local/cuda 是 /usr/local/cuda-10.0的链接文件)

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*

2.查看cudnn版本

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

输出代码,表示的是安装的7.6.5版本的cudnn

dhh@dhh-HP-PC:~$ cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
#define CUDNN_MAJOR 7
#define CUDNN_MINOR 6
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

四、配置Tensorflow2.0

1、安装
换源,清华的源,下载网速快

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

更新pip

pip install pip -U 

安装gpu版本

pip install tensorflow-gpu==2.0.0-alpha0

2、测试
调用python

dhh@dhh-HP-PC:~$ python
Python 2.7.12 (default, Oct  8 2019, 14:14:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

输入

import tensorflow as tf
tf.test.is_gpu_available()

显示True,安装成功

dhh@dhh-HP-PC:~$ python
Python 2.7.12 (default, Oct  8 2019, 14:14:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.test.is_gpu_available()
2019-11-25 08:39:29.375244: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-11-25 08:39:29.589940: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcuda.so.1
2019-11-25 08:39:30.164080: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:1009] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-11-25 08:39:30.215039: I tensorflow/compiler/xla/service/service.cc:162] XLA service 0x44d2060 executing computations on platform CUDA. Devices:
2019-11-25 08:39:30.215077: I tensorflow/compiler/xla/service/service.cc:169]   StreamExecutor device (0): GeForce 830M, Compute Capability 5.0
2019-11-25 08:39:30.432162: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2195115000 Hz
2019-11-25 08:39:30.432663: I tensorflow/compiler/xla/service/service.cc:162] XLA service 0x453d1a0 executing computations on platform Host. Devices:
2019-11-25 08:39:30.432738: I tensorflow/compiler/xla/service/service.cc:169]   StreamExecutor device (0): <undefined>, <undefined>
2019-11-25 08:39:30.455756: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1467] Found device 0 with properties: 
name: GeForce 830M major: 5 minor: 0 memoryClockRate(GHz): 1.15
pciBusID: 0000:0a:00.0
totalMemory: 1.96GiB freeMemory: 1.93GiB
2019-11-25 08:39:30.455806: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1546] Adding visible gpu devices: 0
2019-11-25 08:39:30.455881: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library libcudart.so.10.0
2019-11-25 08:39:30.472790: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1015] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-11-25 08:39:30.472834: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1021]      0 
2019-11-25 08:39:30.472845: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1034] 0:   N 
2019-11-25 08:39:30.482293: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1149] Created TensorFlow device (/device:GPU:0 with 1752 MB memory) -> physical GPU (device: 0, name: GeForce 830M, pci bus id: 0000:0a:00.0, compute capability: 5.0)
True
>>> 

参考文献

1、 ubuntu16.04 + NVIDIA驱动 + CUDA + cuDNN + tensorflow-gpu + keras安装
2、 ubuntu16.04+1080ti+cuda10.0+cudnn环境配置
3、 Ubuntu16.04安装CUDA10.0、CUDNN7.6、TensorFlow2.0
4、 Ubuntu16.04+CUDA8.0+caffe配置

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值