Installation of NVIDIA driver, CUDA and CUDNN under Linux

Posted by nicx on Sun, 29 Dec 2019 13:01:59 +0100

0. Preface

Recently, I am learning how to install and use PaddlePaddle in various video card driver versions, so I am also learning how to install and uninstall CUDA and CUDNN in Ubuntu. During the learning process, I will record the learning process by the way. At the same time, we are strengthening our memory. This paper takes the unloading CUDA 8.0 and CUDNN 7.05 as an example, and the installation CUDA 10.0 and CUDNN 7.4.2 as an example.

1. Install the video card driver

  • Disable nouveau driver
sudo vim /etc/modprobe.d/blacklist.conf

Add at the end of the text:

blacklist nouveau
options nouveau modeset=0

Then execute:

sudo update-initramfs -u

After restart, execute the following command. If there is no screen output, it indicates that nouveau is disabled successfully:

lsmod | grep nouveau
  • Download driver Download address on official website: Download the corresponding version of the graphics card driver according to the situation of your own graphics card. For example, my graphics card is RTX2070: After downloading, you will get an installation package. The file names of different versions may be different:
  • Uninstall old drive The following operations need to be performed in the command interface. Execute the following shortcut keys to enter the command interface and log in: Ctrl-Alt+F2 Disable the X-Window service by executing the following command, otherwise the graphics card driver cannot be installed:
sudo service lightdm stop

Execute the following three commands to uninstall the original video card driver:

sudo apt-get remove --purge nvidia*
sudo chmod +x
sudo ./ --uninstall
  • Install new driver You can install a new driver by executing the driver file directly. By default, you can:
sudo ./

Start the X-Window service by executing the following command

sudo service lightdm start

Finally, execute the restart command to restart the system:


Note: if repeated login occurs after system restart, the wrong version of video card driver is installed in most cases. You need to download the version of the graphics card installed on your own machine.

2. Uninstall and install CUDA

  • Uninstall CUDA Why do I need to uninstall CUDA at the beginning? This is because I changed my video card RTX2070, and installed CUDA 8.0 and CUDNN 7.0.5, which can't be used normally. I need to install CUDA 10.0 and CUDNN 7.4.2, so I need to uninstall the original CUDA first, and I can directly skip without the installed partners. Note that the following commands operate under the root user.

It's easy to uninstall CUDA, just one command. It mainly executes the uninstall script that CUDA comes with. Readers should find the uninstall script according to their version of CUDA:

sudo /usr/local/cuda-8.0/bin/

After uninstalling, there are some remaining folders. CUDA 8.0 was installed before. It can be deleted at the same time:

sudo rm -rf /usr/local/cuda-8.0/

In this way, CUDA will be uninstalled.

  • Install CUDA CUDA and CUDNN versions installed:

CUDA 10.0

CUDNN 7.4.2

The next installation steps are all under the root user.

  • Download and install CUDA We can download it on CUDA10, Download CUDA that matches your system version. After the download is complete, give the file execution permission:
chmod +x

Execute the installation package and start the installation:


After you start the installation, you need to read the instructions. You can use Ctrl + C to read it directly, or use the space bar to read it slowly. Then configure it. Let me explain:

(do you agree to the terms? You must agree to continue the installation)

accept/decline/quit: accept

(do not install the driver here, because the latest driver is already installed, otherwise the old version of the video card driver may be installed, resulting in repeated login.)

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48?
(y)es/(n)o/(q)uit: n
Install the CUDA 10.0 Toolkit?(Is it installed? CUDA 10 ´╝îMust be installed here)
(y)es/(n)o/(q)uit: y
Enter Toolkit Location(Installation path, use default, just enter directly)
 [ default is /usr/local/cuda-10.0 ]:  
Do you want to install a symbolic link at /usr/local/cuda?(Agree to create soft link)
(y)es/(n)o/(q)uit: y
Install the CUDA 10.0 Samples?(No need to install and test, it's in itself)
(y)es/(n)o/(q)uit: n
Installing the CUDA Toolkit in /usr/local/cuda-10.0 ...(Start installation)

After installation, you can configure their environment variables, and add the following configuration information at the end of vim ~/.bashrc:

export CUDA_HOME=/usr/local/cuda-10.0
export PATH=${CUDA_HOME}/bin:${PATH}

Finally, use the command source ~/.bashrc to make it effective.

You can use the command nvcc -V to view the installed version information:

test@test:~$ 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
  • Test for successful installation Execute the following commands:
cd /usr/local/cuda-10.0/samples/1_Utilities/deviceQuery

Normal output:

./deviceQuery Starting...

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

Detected 1 CUDA Capable device(s)

Device 0: "GeForce RTX 2070"
  CUDA Driver Version / Runtime Version          10.0 / 10.0
  CUDA Capability Major/Minor version number:    7.5
  Total amount of global memory:                 7950 MBytes (8335982592 bytes)
  (36) Multiprocessors, ( 64) CUDA Cores/MP:     2304 CUDA Cores
  GPU Max Clock rate:                            1620 MHz (1.62 GHz)
  Memory Clock rate:                             7001 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 4194304 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 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:  1024
  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 3 copy engine(s)
  Run time limit on kernels:                     Yes
  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:            Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

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

3. Download and install CUDNN

Enter the download website of CUDNN: However, click Download to select the Download version. Of course, there is login before downloading. The version selection interface is as follows. We select cuDNN Library for Linux: After downloading is a compressed package, as follows:


Then decompress it. The command is as follows:

tar -zxvf cudnn-10.0-linux-x64-v7.4.2.24.tgz

After decompression, you can get the following files:


Use the following two commands to copy these files to the CUDA Directory:

cp cuda/lib64/* /usr/local/cuda-10.0/lib64/
cp cuda/include/* /usr/local/cuda-10.0/include/

After copying, you can view the version information of CUDNN using the following command:

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

Test installation results The installation of CUDA 10 and CUDNN 7.4.2 has been completed.

4. Test and installation

You can install the GPU version of the corresponding Python to test whether it can be used normally. The installation is as follows:

pip3 install pip3 install torchvision Then use the following procedure to test the installation:

import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim import torch.backends.cudnn as cudnn from torchvision import datasets, transforms

class Net(nn.Module): def init(self): super(Net, self).init() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10)

def forward(self, x):
    x = F.relu(F.max_pool2d(self.conv1(x), 2))
    x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
    x = x.view(-1, 320)
    x = F.relu(self.fc1(x))
    x = F.dropout(x,
    x = self.fc2(x)
    return F.log_softmax(x, dim=1)

def train(model, device, train_loader, optimizer, epoch): model.train() for batch_idx, (data, target) in enumerate(train_loader): data, target =, optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % 10 == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item()))

def main(): cudnn.benchmark = True torch.manual_seed(1) device = torch.device("cuda") kwargs = {'num_workers': 1, 'pin_memory': True} train_loader = datasets.MNIST('../data', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])), batch_size=64, shuffle=True, **kwargs)

model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)

for epoch in range(1, 11):
    train(model, device, train_loader, optimizer, epoch)

if name == 'main': main()

If the following information is output normally, it is proved that it has been installed as:

Train Epoch: 1 [0/60000 (0%)] Loss: 2.365850 Train Epoch: 1 [640/60000 (1%)] Loss: 2.305295 Train Epoch: 1 [1280/60000 (2%)] Loss: 2.301407 Train Epoch: 1 [1920/60000 (3%)] Loss: 2.316538 Train Epoch: 1 [2560/60000 (4%)] Loss: 2.255809 Train Epoch: 1 [3200/60000 (5%)] Loss: 2.224511 Train Epoch: 1 [3840/60000 (6%)] Loss: 2.216569 Train Epoch: 1 [4480/60000 (7%)] Loss: 2.181396

Topics: sudo Linux vim Ubuntu