Install CUDA and NVIDIA video driver under CentOS 7

Author:

Nvidia CUDA Toolkit supports CentOS 7 and it is relatively simple to install it. Nvidia provides three types of installation – a big setup file, a big rpm file and an official Nvidia repository, which we are going to use it in this article. The Nvidia repository contains the Nvidia video driver for the Nvidia video cards like GeForce, GTX, RTX and so on. You may need CUDA Toolkit if you are a game developer or you want to build yourself some of the mining software like XMR-STAK.
In this article, we are going to use the NVIDIA official repository for CUDA and the video driver module. There are other ways to install CUDA, which are not the purpose of this article. Using an official repository is the best practice for installing software on your system.

STEP 1) Update and install the NVIDIA official repository.

yum update -y
yum install -y yum-utils
yum-config-manager --add-repo http://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo


The real output in our server:

[root@localhost ~]# yum -y update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.host.ag
 * extras: mirror.host.ag
 * updates: mirror.host.ag
No packages marked for update
[root@localhost ~]# yum install -y yum-utils
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.host.ag
 * extras: mirror.host.ag
 * updates: mirror.host.ag
Resolving Dependencies
--> Running transaction check
---> Package yum-utils.noarch 0:1.1.31-52.el7 will be installed
--> Processing Dependency: python-kitchen for package: yum-utils-1.1.31-52.el7.noarch
--> Processing Dependency: libxml2-python for package: yum-utils-1.1.31-52.el7.noarch
--> Running transaction check
---> Package libxml2-python.x86_64 0:2.9.1-6.el7_2.3 will be installed
---> Package python-kitchen.noarch 0:1.1.1-5.el7 will be installed
--> Processing Dependency: python-chardet for package: python-kitchen-1.1.1-5.el7.noarch
--> Running transaction check
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                      Arch                                 Version                                         Repository                          Size
============================================================================================================================================================================
Installing:
 yum-utils                                    noarch                               1.1.31-52.el7                                   base                               121 k
Installing for dependencies:
 libxml2-python                               x86_64                               2.9.1-6.el7_2.3                                 base                               247 k
 python-chardet                               noarch                               2.2.1-3.el7                                     base                               227 k
 python-kitchen                               noarch                               1.1.1-5.el7                                     base                               267 k

Transaction Summary
============================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 862 k
Installed size: 4.3 M
Downloading packages:
(1/4): python-chardet-2.2.1-3.el7.noarch.rpm                                                                                                         | 227 kB  00:00:00     
(2/4): yum-utils-1.1.31-52.el7.noarch.rpm                                                                                                            | 121 kB  00:00:00     
(3/4): libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm                                                                                                     | 247 kB  00:00:00     
(4/4): python-kitchen-1.1.1-5.el7.noarch.rpm                                                                                                         | 267 kB  00:00:00     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                       1.4 MB/s | 862 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python-chardet-2.2.1-3.el7.noarch                                                                                                                        1/4 
  Installing : python-kitchen-1.1.1-5.el7.noarch                                                                                                                        2/4 
  Installing : libxml2-python-2.9.1-6.el7_2.3.x86_64                                                                                                                    3/4 
  Installing : yum-utils-1.1.31-52.el7.noarch                                                                                                                           4/4 
  Verifying  : python-kitchen-1.1.1-5.el7.noarch                                                                                                                        1/4 
  Verifying  : yum-utils-1.1.31-52.el7.noarch                                                                                                                           2/4 
  Verifying  : libxml2-python-2.9.1-6.el7_2.3.x86_64                                                                                                                    3/4 
  Verifying  : python-chardet-2.2.1-3.el7.noarch                                                                                                                        4/4 

Installed:
  yum-utils.noarch 0:1.1.31-52.el7                                                                                                                                          

Dependency Installed:
  libxml2-python.x86_64 0:2.9.1-6.el7_2.3                    python-chardet.noarch 0:2.2.1-3.el7                    python-kitchen.noarch 0:1.1.1-5.el7                   

Complete!
[root@localhost ~]# yum-config-manager --add-repo http://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo
Loaded plugins: fastestmirror
adding repo from: http://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo
grabbing file http://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo to /etc/yum.repos.d/cuda-rhel7.repo
repo saved to /etc/yum.repos.d/cuda-rhel7.repo
[root@localhost ~]# yum install -y epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.daticum.com
 * extras: mirrors.daticum.com
 * updates: mirrors.daticum.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                       Arch                                    Version                                Repository                               Size
============================================================================================================================================================================
Installing:
 epel-release                                  noarch                                  7-11                                   extras                                   15 k

Transaction Summary
============================================================================================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm                                                                                                                         |  15 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                                                                                                                 1/1 
  Verifying  : epel-release-7-11.noarch                                                                                                                                 1/1 

Installed:
  epel-release.noarch 0:7-11                                                                                                                                                

Complete!
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: base cuda epel extras updates
Cleaning up list of fastest mirrors

STEP 2) Install the CUDA and the NVIDIA video driver.

As you can see the driver uses DKMS – https://en.wikipedia.org/wiki/Dynamic_Kernel_Module_Support. In simple words, the DKMS allows installing kernel modules, which source does not reside in the kernel tree. NVIDIA uses proprietary video driver and DKMS to load the kernel module and the proprietary video driver.

yum -y install nvidia-driver-latest-dkms cuda

The above line will initiate the installation of around 209 packages including GNU GCC, mesa, perl, java (openjdk), Xorg server and multiple libraries and more. The total downloaded size is 2.4 Gbytes and installed size is 4.7 Gbytes. In the end, there is a section with the full installation log.

STEP 3) Enable the NVIDIA video driver.

At this stage, we have NVIDIA Cuda installed and ready for use, but in most cases, you probably would like to use the compiled code on the same computer, so you need the NVIDIA video driver loaded. The driver is just installed but cannot be used. We have to manually compile it once and after the first compile when updating the kernel it will compile the module automatically.
First install the last dependency:

[root@localhost ~]# yum install kernel-devel
Loaded plugins: fastestmirror, nvidia
#### NVIDIA ####
Loading mirror speeds from cached hostfile
 * base: mirror.wwfx.net
 * epel: mirrors.neterra.net
 * extras: mirror.wwfx.net
 * updates: mirror.wwfx.net
Resolving Dependencies
--> Running transaction check
---> Package kernel-devel.x86_64 0:3.10.0-1062.1.1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================
 Package                                   Arch                                Version                                           Repository                            Size
============================================================================================================================================================================
Installing:
 kernel-devel                              x86_64                              3.10.0-1062.1.1.el7                               updates                               18 M

Transaction Summary
============================================================================================================================================================================
Install  1 Package

Total download size: 18 M
Installed size: 38 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
kernel-devel-3.10.0-1062.1.1.el7.x86_64.rpm                                                                                                          |  18 MB  00:00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : kernel-devel-3.10.0-1062.1.1.el7.x86_64                                                                                                                  1/1 
  Verifying  : kernel-devel-3.10.0-1062.1.1.el7.x86_64                                                                                                                  1/1 

Installed:
  kernel-devel.x86_64 0:3.10.0-1062.1.1.el7                                                                                                                                 

Complete!

Build the DKMS module:

[root@localhost ~]# dkms status
nvidia, 418.87.00: added
[root@localhost ~]# dkms build nvidia/418.87.00

Kernel preparation unnecessary for this kernel.  Skipping...

Building module:
cleaning build area...
'make' -j2 module SYSSRC=/lib/modules/3.10.0-1062.1.1.el7.x86_64/build IGNORE_XEN_PRESENCE=1 IGNORE_PREEMPT_RT_PRESENCE=1 IGNORE_CC_MISMATCH=1.....................
cleaning build area...

DKMS: build completed.
[root@localhost ~]# dkms status
nvidia, 418.87.00, 3.10.0-1062.1.1.el7.x86_64, x86_64: built

Enable the driver by uncommenting the line:

options nvidia-drm modeset=1

in file – “/usr/lib/modprobe.d/nvidia.conf”. If the line is missing just add it at the end of the file. In the same file the there is a line “blacklist nouveau” to instruct the kernel not to load the open-source module for the NVIDIA video driver.
And then reboot the machine. After reboot, the NVIDIA driver will be loaded.
Check if the driver is loaded successfully:

[root@localhost ~]# dmesg|grep -i nvidia
[    6.383444] input: HDA NVidia HDMI/DP,pcm=3 as /devices/pci0000:00/0000:00:03.1/0000:0a:00.1/sound/card0/input12
[    6.383579] input: HDA NVidia HDMI/DP,pcm=7 as /devices/pci0000:00/0000:00:03.1/0000:0a:00.1/sound/card0/input13
[    6.383826] input: HDA NVidia HDMI/DP,pcm=8 as /devices/pci0000:00/0000:00:03.1/0000:0a:00.1/sound/card0/input14
[    6.384343] input: HDA NVidia HDMI/DP,pcm=9 as /devices/pci0000:00/0000:00:03.1/0000:0a:00.1/sound/card0/input15
[    6.777798] nvidia: loading out-of-tree module taints kernel.
[    6.777804] nvidia: module license 'NVIDIA' taints kernel.
[    6.832333] nvidia: module verification failed: signature and/or required key missing - tainting kernel
[    6.890453] nvidia-nvlink: Nvlink Core is being initialized, major device number 240
[    6.994744] NVRM: loading NVIDIA UNIX x86_64 Kernel Module  418.87.00  Thu Aug  8 15:35:46 CDT 2019
[    7.044154] nvidia-uvm: Loaded the UVM driver in 8 mode, major device number 238
[    7.102688] nvidia-modeset: Loading NVIDIA Kernel Mode Setting Driver for UNIX platforms  418.87.00  Thu Aug  8 15:27:13 CDT 2019
[    7.111439] [drm] [nvidia-drm] [GPU ID 0x00000a00] Loading driver
[    7.111488] nvidia 0000:0a:00.0: irq 340 for MSI/MSI-X
[    7.661829] caller os_map_kernel_space.part.5+0x85/0xb0 [nvidia] mapping multiple BARs
[    8.353066] [drm] Initialized nvidia-drm 0.0.0 20160202 for 0000:0a:00.0 on minor 0
[root@localhost ~]# ls -altr /dev/nvidia*
crw-rw-rw-. 1 root root 195, 255 24 Sep  1,56 /dev/nvidiactl
crw-rw-rw-. 1 root root 238,   0 24 Sep  1,56 /dev/nvidia-uvm
crw-rw-rw-. 1 root root 238,   1 24 Sep  1,56 /dev/nvidia-uvm-tools
crw-rw-rw-. 1 root root 195, 254 24 Sep  1,56 /dev/nvidia-modeset
crw-rw-rw-. 1 root root 195,   0 24 Sep  1,56 /dev/nvidia0
[root@localhost ~]# ls -altr /dev/dri/
total 0
drwxr-xr-x.  2 root root        80 24 Sep  1,56 .
crw-rw----.  1 root video 226, 128 24 Sep  1,56 renderD128
crw-rw----.  1 root video 226,   0 24 Sep  1,56 card0
drwxr-xr-x. 21 root root      3580 24 Sep  1,56 ..
[root@localhost ~]# nvidia-smi 
Tue Sep 24 02:10:52 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.87.00    Driver Version: 418.87.00    CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:0A:00.0  On |                  N/A |
|  0%   46C    P8    13W / 275W |      2MiB / 11174MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

If the driver is not loaded all of the above directories in /dev would be missing and the nvidia smi would report the driver is not loaded.

Leave a Reply

Your email address will not be published. Required fields are marked *