How to install Nvidia driver & CUDA?

It is more complex than ordinary packages, but still not hard.

Nvidia Driver

Before anything started, we may update our system. It is optional.

For CentOS/Fedora users, the command may seems as follow:

yum update -y

For CentOS/Fedora user, we may install dkms first. This is optional as well.

yum install kernel-devel epel-release -y
yum install dkms -y

And then, we should make sure the OS is running on the latest kernel installed.

How could we check the kernel version?

We can use command

# uname -r
3.10.0-514.26.2.el7.x86_64

To get the kernel version we are using.

And then, we can list the images in /boot folder:

# ls -l /boot/vmlinuz-*
-rwxr-xr-x. 1 root root 5392080 Jun 11 2017 /boot/vmlinuz-0-rescue-8d13a50988cc5c4972347415eddf7d47
-rwxr-xr-x 1 root root 5397552 May 26 2017 /boot/vmlinuz-3.10.0-514.21.1.el7.x86_64
-rwxr-xr-x 1 root root 5397008 Jul 4 2017 /boot/vmlinuz-3.10.0-514.26.2.el7.x86_64
-rwxr-xr-x 1 root root 5878368 Oct 21 2017 /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64
-rwxr-xr-x 1 root root 6242208 Aug 15 06:02 /boot/vmlinuz-3.10.0-862.11.6.el7.x86_64

In generally, ls will print the results in lexical order, if our version is not in the last line, we may reboot first.

If we want, we can lock the kernel version on yum. A simple way is add one line “exclude=kernel*” in /etc/yum.conf

[main]
cachedir=/var/cache/yum/$basearch/$releasever
....
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
exclude=kernel*

And then, we can go to Nvidia’s Driver list. If we have no special requirement, we may simply pick the first one.

Currently, the latest long lived branch version is 410.78.

chmod +x NVIDIA-Linux-x86_64-410.78.run
./NVIDIA-Linux-x86_64-410.78.run

After it got finished, we may check it using command

nvidia-smi

CUDA

CUDA’s installation is pretty easy as well.

Go to the cuda download page, and choose our requirement, and follow the commands one by one.

nvidia-container-runtime

If we wish to use nvidia in docker environment. A simple way is to add a nvidia-container-runtime

Install the repository for your distribution by following the instructions here.

Install the nvidia-container-runtime package:

sudo apt-get install nvidia-container-runtime -y

Or

sudo yum install nvidia-container-runtime -y

Add a daemon.json, use nvidia as default runtime.

# make sure the folder did exists
mkdir -p /etc/docker

# backup daemon.json
[ -f /etc/docker/daemon.json ] && cp /etc/docker/daemon.json{,.save}

cat > /etc/docker/daemon.json << 'EOF'
{
"runtimes": {
"nvidia": {
"path": "/usr/bin/nvidia-container-runtime",
"runtimeArgs": []
}
},
"default-runtime": "nvidia"
}
EOF

And finally, we can restart docker service to apply it.