在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權限纔可以安裝)

 

 

驗證系統是否是受支持的Linux版本

 

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

 

 

到這裏查看受支持的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用於開發的主要的工具

 

 

驗證系統是否安裝了正確的內核頭文件和開發包

 

因爲在安裝驅動的過程中會自動地嘗試安裝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。

 

 

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

 

然後重啓就好了,不過這是小版本升級,大版本升級要麻煩的多。

 

 

 

[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而以(可以這麼理解)。

 

 

然後按照官網上提供的操作指南進行安裝就好了。

 

使用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.選擇操作系統和安裝包

 

 

 

按照下載地址下載到服務器上

 

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 安裝正確。

 

 

安裝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

 

如果已經正確安裝則會輸出以下提示

 

 

如果輸出以下提示:

./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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章