在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

Cuda官方文档:https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements

 

出现任何问题,先去官方文档看是不是哪一步没有按照文档去做!!!

 

同时博主是在root下进行的安装,如果是非root用户,其中很多命令应该需要加sudo执行!!!

 

前提机器上面有支持CUDA的Nvidia GPU,查看支持CUDA的GPU列表: https://developer.nvidia.com/cuda-gpus

 

lspci | grep -I nvidia

 

如果没有lspci这个命令,需要安装 yum install pciutils (注意要拥有root权限才可以安装)

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

验证系统是否是受支持的Linux版本

 

uname -m && cat /etc/redhat-release

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

到这里查看受支持的Linux版本:http://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#system-requirements

 

验证系统是否有GCC编译环境

 

gcc -v

 

没有的话需要先安装GCC

 

yum group install "Development Tools"

 

可以安装gcc, g++, make

 

yum install man-pages

 

可以安装使用GUN/Linux用于开发的主要的工具

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

验证系统是否安装了正确的内核头文件和开发包

 

因为在安装驱动的过程中会自动地尝试安装development packages和kernel headers, 如果没有事先安装好的话会导致直接安装最新的development packages和kernel headers, 很可能导致和系统的内核版本不匹配,这可能会导致之后 nvidia-smi 时候出现"NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.” 还有一点是如果更新了系统内核导致了显卡驱动出现问题的,也需要更改development packages和kernel headers。

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

unman -r 可以查看系统内核版本

 

rpm -qa | grep kernel 查看所有的kernel相关的组件

 

yum install kernel-devel-$(uname -r) kernel-headers-$(uname -r)

 

如果发现上述命令执行的时候出错,说找不到对应的kernel-devel 和 kernel-headers那么可以考虑直接升级内核,这里提供了两种方式一个是小版本升级一个是大版本升级,对于安装驱动来说一般做个小驱动升级就可以了,一般会升级到最新的版本,就可以解决找不到devel和headers的问题了,接着再按照好devel和headers,反正这里一定要保证内核的版本和devel和headers的版本是一致的,效果图如下。

 

centos7升级内核至最新 https://www.cnblogs.com/ding2016/p/10429640.html

 

首先查看内核 yum list kernel

 

然后更新内核 yum update kernel -y

 

然后重启就好了,不过这是小版本升级,大版本升级要麻烦的多。

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

[Linux]CentOS7.6更新内核 http://www.gaussli.com/2019/03/06/linux-centos7-6%E6%9B%B4%E6%96%B0%E5%86%85%E6%A0%B8/

 

这里还有一个更新内核的教程,属于大版本更新。

 

安装内核源,官网 http://elrepo.org/tiki/tiki-index.php

 

这个地址应该会不断更新的,因此每一次安装之前都要去看一下 

 

yum install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm

 

查看是否有关于内核的源,elrepo(RPM repository for Enterprise Linux package),其实是个企业级Linux的仓库,这里只用到内核包,所以称为内核源

 

yum repolist

 

安装完后重新执行yum repolist就能看到内核源已经安装好了

 

但是大版本更新在这里是没有必要的,看看就好。

 

到这里把所有的准备工作都做好了,就可以安装驱动和cuda了。

 

安装驱动

 

这里很多教程都说要先禁用nouveau,但是如果直接使用rpm进行安装的话其实安装程序会自动去做这个工作,只要最后重启一下就好。

 

GPU 云服务器正常工作需安装正确的基础设施软件,对 NVIDIA 系列 GPU 而言,有两个层次的软件包需要安装:

* 驱动 GPU 工作的硬件驱动程序。

* 上层应用程序所需要的库。

若把 NVIDIA GPU 用作通用计算,需要安装 Tesla Driver + CUDA。

 

rpm 包安装

 

1.打开 NVIDIA 驱动下载链接 http://www.nvidia.com/Download/Find.aspx

 

2.选择支持 RPM 包的操作系统,并获取该 RPM 包的下载链接。例如:选择 CentOS 7.x, 得到下载链接:https://www.nvidia.com/content/DriverDownload-March2009/confirmation.php?url=/tesla/396.82/nvidia-diag-driver-local-repo-rhel7-396.82-1.0-1.x86_64.rpm&lang=us&type=Tesla

 

这里有一个点就是centos直接选择rhel就好,因为系统选项里没有centos,而centos其实就是rehl免费版本的copy而以(可以这么理解)。

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

然后按照官网上提供的操作指南进行安装就好了。

 

使用rpm命令安装 rpm 包。

rpm -i nvidia-diag-driver-local-repo-rhel7-396.82-1.0-1.x86_64.rpm

 

使用yum命令清除缓存。

yum clean all

 

使用yum命令安装驱动。

yum install cuda-drivers

 

重启机器

reboot

 

运行nvidia-smi能输出正确信息代表驱动安装成功

 

这时注意看一下nouveau是否关闭,下面的命令没有输出就代表关闭了,如果没有关闭的话,那就去官方文档找一下怎么关闭就好了

 

lsmod | grep nouveau

 

关闭方法:

 

1. #新建一个配置文件

2. vim /etc/modprobe.d/blacklist-nouveau.conf

3. #写入以下内容

4. blacklist nouveau

5. options nouveau modeset=0

6. #保存并退出

7. :wq

8. #备份当前的镜像

9. mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak

10. #建立新的镜像

11. dracut /boot/initramfs-$(uname -r).img $(uname -r)

12. #重启

13. reboot

14. #最后输入上面的命令验证

15. lsmod | grep nouveau

 

安装 CUDA

 

CUDA (Compute Unified Device Architecture) 是显卡厂商 NVIDIA 推出的运算平台。 CUDA™ 是一种由 NVIDIA 推出的通用并行计算架构,该架构使 GPU 能够解决复杂的计算问题。 它包含了 CUDA 指令集架构(ISA)以及 GPU 内部的并行计算引擎。 开发人员现在可以使用 C 语言, C++ , FORTRAN 来为 CUDA™ 架构编写程序,所编写出的程序可以在支持 CUDA™ 的处理器上以超高性能运行。

GPU 云服务器采用 NVIDIA 显卡,需要安装 CUDA 开发运行环境。

 

1.先在 https://developer.nvidia.com/cuda-toolkit-archive 选择一下cuda版本

 

2.选择操作系统和安装包

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

按照下载地址下载到服务器上

 

3.在 CUDA 安装包所在目录下运行如下命令:

rpm -i cuda-repo-rhel7-9-2-local-9.2.148-1.x86_64.rpm

yum clean all

yum install cuda

 

4.在 /usr/local/cuda/samples/1_Utilities/deviceQuery 目录下,执行 make 命令,可以编译出 deviceQuery 程序。

 

5.执行 deviceQuery 正常显示如下设备信息,此刻认为 CUDA 安装正确。

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

安装cudnn

 

cudnn官方文档:https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html

 

If you choose the .tgz method, everything you need is contained in the single .tgz archive "cuDNN v7.0.5 Library for Linux", and right off the bat we can make the observation that if you were using some Linux OS besides Ubuntu, this is the (only) way to go. This zip file contains the compiled libraries, the header files needed for development against the cuDNN API, and other things you will need to compile/link code against cuDNN.

If you choose the package manager method, this is currently only available for the listed OS's (Ubuntu 16.04 and Ubuntu 14.04 in this example). You have some independent choices:

  • Runtime Library - this is sufficient if the only thing you will be doing with cuDNN is running another binary that is already compiled to use that version of cuDNN on that version of Ubuntu. For example, if you have installed Tensorflow (TF) via a pip method, so you get precompiled binaries that are ready to go, and those precompiled binaries are set up for Ubuntu 16.04 and cuDNN 7.0.5, then you could just install the cuDNN runtime library (follow the steps in the install guide) and that would be sufficient to begin using your precompiled TF.

  • Developer Library - this will include header files and other things that are needed to compile and build codes against this particular version of the cuDNN API. So if you wanted to build or rebuild TF (for CUDA GPU usage), or you simply were working on your own API usage of cuDNN, you would want this option.

  • Code Samples and User Guide - In the .tgz install method, all of these components, including documentation and code samples, are included in the single .tgz archive. In the package manager method, even the Code samples and API user guide are broken out into a separate .deb install, so you don't have to download it if you don't need it (for example if you were just rebuilding TF against cuDNN 7.0.5, you would want the Developer Library but would not need the Code Samples or API User Guide).

UPDATE: With more recent versions of CUDNN, the .tgz file/method no longer contains the sample codes or documentation. The documentation (user guide and install guide) is here. The samples are only available in the .deb installer(s).

 

我们安装cudnn不用管什么runtime和development,因为我们是使用tgz压缩包安装,直接把压缩包下载下来就好了。

 

https://developer.nvidia.com/rdp/form/cudnn-download-survey

 

首先会有一个surey,必须要做不能跳过,做完之后需要注册为开发者方可下载,这里使用wget下载还会出现一点问题

 

解压后执行

cp cuda/include/cudnn.h /usr/local/cuda/include

cp cuda/lib64/libcudnn* /usr/local/cuda/lib64

chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

 

验证

git clone https://github.com/wilhelmguo/cudnn_samples_v7.git

cd cudnn_samples_v7/mnistCUDNN

make

./mnistCUDNN

 

如果已经正确安装则会输出以下提示

 

在Centos7上安装Nvidia的驱动以及Cuda,Cudnn (亲测,出坑!!!)

 

如果输出以下提示:

./mnistCUDNN: error while loading shared libraries: > libcudnn.so.7: cannot open shared object file: No > > such file or directory

首先用ldconfig -v 确认libcudnn.so.7情况,如果不是软链的话那就ln -sf手动新建一个软链,然后执行 ldconfig /usr/local/cuda/lib64 后重新运行即可。

 

参考:

https://wilhelmguo.cn/blog/post/william/Centos-7-%E5%AE%89%E8%A3%85-Nvidia-GPU-%E9%A9%B1%E5%8A%A8%E5%8F%8A-CUDA

https://www.cnblogs.com/evan-blog/p/10328187.html