Ubuntu 20.04中安裝nvidia-driver-460版 & CUDA-11.4.2版 & cuDNN|Install nvidia-driver-460 & CUDA-11.4.2 & cuDNN in Ubuntu 20.04

yuhsi chen
11 min readOct 16, 2021

--

這篇當中使用的是Windows 10系統,這次嘗試在Ubuntu 20.04中安裝好nvidia driver, CUDA, 以及cuDNN,在此紀錄並供大家參考。

nvidia driver安裝

0️⃣輸入以下指令顯示GPU資訊並查詢支援的CUDA版本

sudo lshw -numeric -C display
sudo lshw -numeric -C display

1️⃣清除既有nvidia driver

sudo apt-get purge nvidia*
sudo apt-get purge nvidia*

2️⃣加入GPU ppa

sudo add-apt-repository ppa:graphics-drivers
sudo add-apt-repository ppa:graphics-drivers

3️⃣packages更新

sudo apt-get update
sudo apt-get update
sudo apt upgrade
sudo apt upgrade

4️⃣列出支援的GPU driver版本

ubuntu-drivers list
ubuntu-drivers list

5️⃣nvidia-driver-460版本安裝

sudo apt install nvidia-driver-460
sudo apt install nvidia-driver-460 (545 for 2204)

6️⃣nvidia-driver-460安裝完成後重新啟動

reboot
reboot

7️⃣輸入以下指令檢查nvidia driver版本

nvidia-smi
nvidia-smi (disable secure boot in bios)

CUDA-11.4.2安裝

1️⃣依序輸入網址內指令

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
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
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
wget https://developer.download.nvidia.com/compute/cuda/11.4.2/local_installers/cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-4-local_11.4.2-470.57.02-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu2004-11-4-local/7fa2af80.pub
sudo apt-key add /var/cuda-repo-ubuntu2004-11-4-local/7fa2af80.pub
sudo apt-get update
sudo apt-get update
sudo apt-get -y install cuda
sudo apt-get -y install cuda
$ vim ~/.bashrc
>> export PATH=/usr/local/cuda/bin:$PATH
>> export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
$ source ~/.bashrc

2️⃣輸入以下指令檢查CUDA版本

nvcc -V
nvcc -V

cuDNN v8.2.2安裝

1️⃣進入nvidia cudnn網站下載支援ubuntu 20.04版本的檔案並解壓縮

cuDNN Library for Linux[x86_64]
cd Downloads/
cd Downloads/
tar -xzvf cudnn-11.4-linux-x64-v8.2.4.15.tgz
tar -xzvf cudnn-11.4-linux-x64-v8.2.4.15.tgz

2️⃣複製檔案至安裝CUDA的資料夾中

sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*

3️⃣點選以下三個載點並依序下載

cuDNN Runtime Library for Ubuntu20.04 x86_64 (Deb)
cuDNN Developer Library for Ubuntu20.04 x86_64 (Deb)
cuDNN Code Samples and User Guide for Ubuntu20.04 x86_64 (Deb)

下載完成畫面

4️⃣對下載好的檔.deb檔案進行安裝 — “dpkg -i fileName.deb”

sudo dpkg -i  libcudnn8_8.2.0.53-1+cuda11.3_amd64.deb
sudo dpkg -i libcudnn8_8.2.0.53–1+cuda11.3_amd64.deb
sudo dpkg -i libcudnn8-dev_8.2.0.53-1+cuda11.3_amd64.deb
sudo dpkg -i libcudnn8-dev_8.2.0.53–1+cuda11.3_amd64.deb
sudo dpkg -i libcudnn8-samples_8.2.0.53-1+cuda11.3_amd64.deb
sudo dpkg -i libcudnn8-samples_8.2.0.53–1+cuda11.3_amd64.deb

5️⃣使用以下指令檢查至此各版本是否相容

nvidia-smi
nvidia-smi

6️⃣測試cuDNN否成功安裝 — 使用sample檔案檢查

cp -r /usr/src/cudnn_samples_v8/ $HOME
cp -r /usr/src/cudnn_samples_v8/ $HOME
cd $HOME/cudnn_samples_v8/mnistCUDNN/
cd $HOME/cudnn_samples_v8/mnistCUDNN/
make clean && make
make clean && make

在輸入make clean && make指令之後報錯如下

FreeImage.h: No such file or directory

原因是因為沒有cuDNN驗證用的檔案,故需執行以下指令進行安裝

sudo apt-get install libfreeimage3 libfreeimage-dev
sudo apt-get install libfreeimage3 libfreeimage-dev
make clean && make
make clean && make
./mnistCUDNN
./mnistCUDNN

如果可以顯示Test passed!的話代表成功安裝

Test passed!

References

--

--