Install CUDA & cuDNN in Ubuntu20.04 OS

bolero2.dev
3 min readJul 5, 2023

--

1. Remove All !

  • Remove all of cuda* and nvidia* files
sudo apt purge nvidia*
sudo apt remove nvidia-*
sudo rm /etc/apt/sources.list.d./cuda*
sudo apt autoremove
sudo apt autoclean
sudo rm -rf /usr/local/cuda*

2. Installation

We should

  1. nvidia graphic driver
  2. CUDA
  3. cuDNN

First, When entering the nvidia-smi command, an error should occur.
Because we removed graphic driver.

2–1. Install build dependencies

sudo apt-get install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

2–2. Install nvidia driver dependencies

(If you need this packages)

#!/bin/bash

sudo apt install -y libnvidia-extra-515
sudo apt install -y nvidia-compute-utils-515
sudo apt install -y libnvidia-decode-515
sudo apt install -y libnvidia-encode-515
sudo apt install -y nvidia-utils-515

2–3. Install Nvidia Driver

#!/bin/bash

# Check VGA
lshw -numeric -C display
lspci | grep -i nvidia

ubuntu-drivers devices
sudo ubuntu-drivers autoinstall

sudo reboot

2–3–1. nvidia-smi command error issue

(After installation of nvidia driver)
Failed to initialize NVML: Driver/library version mismatch

lsmod|grep nvidia

sudo rmmod nvidia_drm
sudo rmmod nvidia_uvm
sudo rmmod nvidia_modeset
sudo rmmod nvidia

lsmod|grep nvidia

2–4. Install CUDA

I will install 11.7 version of CUDA

  1. Visit site : https://developer.nvidia.com/cuda-toolkit-archive

2. Click Version

3. check environment of your machine, and copy commands below.

4. make bash shell script (below script is about CUDA 11.5 ver.)

#!/bin/bash

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.5.0/local_installers/cuda-repo-ubuntu2004-11-5-local_11.5.0-495.29.05-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-5-local_11.5.0-495.29.05-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu2004-11-5-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

(tip) if the sudo apt update doesn’t work?

ubuntu@gpuserver:/etc/apt/sources.list.d$ cat cuda-ubuntu2004-11-5-local.list
deb file:///var/cuda-repo-ubuntu2004-11-5-local /

Check if the file exists in the /var/cuda-repo-ubuntu2004–11–5-local location and create a .list file with the above file name.

2–5. Install cuDNN

  1. Download cudnn .tar.gz file : https://developer.nvidia.com/rdp/cudnn-archive
  2. change directory which have cudnn lib/include files
ubuntu@gpuserver:~/cudnn-linux-x86_64-8.4.1.50_cuda11.6-archive$ ls
LICENSE include lib

3. Enter this command :

sudo cp -P include/cudnn* /usr/local/cuda/include/

sudo cp -P lib/libcudnn* /usr/local/cuda/lib{64?}/

sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

3. PATH setting in ~/.bashrc

1. write this paths in ~/.bashrc file

export PATH=/usr/local/cuda-11.5/bin:$PATH export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH export LD_LIBRARY_PATH=/usr/local/cuda-11.5/lib64:$LD_LIBRARY_PATH export LD_LIBRARY_PATH=/usr/local/cuda-11/lib64:$LD_LIBRARY_PATH

2. Apply changes

source ~/.bashrc

3. Install nvidia-cuda-toolkit

sudo apt install nvidia-cuda-toolkit

4. check command : nvcc -V

ubuntu@gpuserver:~/bolero$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Tue_May__3_18:49:52_PDT_2022
Cuda compilation tools, release 11.7, V11.7.64
Build cuda_11.7.r11.7/compiler.31294372_0

4. References

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

bolero2.dev
bolero2.dev

Written by bolero2.dev

ML/DL and CV Engineer & Researcher

No responses yet

Write a response