UbuntuにNVIDIAドライバとCUDAをインストール

まず lspci コマンドで CUDA 対応の GPU が備わっているかどうか確認する。

lspci | grep NVIDIA

NVIDIA ドライバのインストール

  • NVIDIA ドライバの PPA を追加
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
  • NVIDIA ドライバの推奨バージョンを確認
sudo apt-get install ubuntu-drivers-common
sudo ubuntu-drivers devices
  • NVIDIA ドライバをインストール
sudo apt install nvidia-driver-510
  • NVIDIA ドライバが正しくインストールされているかどうかを確認
sudo nvidia-smi

CUDA のインストール

NVIDIA サイト をご参照ください。

Ubuntu 20.04の場合

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.6.1/local_installers/cuda-repo-ubuntu2004-11-6-local_11.6.1-510.47.03-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-11-6-local_11.6.1-510.47.03-1_amd64.deb
sudo apt-key add /var/cuda-repo-ubuntu2004-11-6-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

最後のインストールでエラーが出る場合

$ sudo apt-get -y install cuda
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 cuda : Depends: cuda-11-6 (>= 11.6.1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

これは CUDA と NVIDIA ドライバのバージョンが対応していないので、 インストールする前に、CUDA と NVIDIA ドライバのバージョン対応を確認しよう。 https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html

  • インストールされた NVIDIA ドライバを削除したい場合
sudo apt clean
sudo apt update
sudo apt purge nvidia-* 
sudo apt autoremove
  • 環境変数の設定
export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"

Updated: