ubuntu22にnvidia driver と cuda 12.2を入れる

## 環境
ubuntu 22.04
## インストールするもの
nvidia-driver-535
cuda 12.2

## nvidia driver install
```
$ ubuntu-drivers devices


== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00001F06sv00001462sd00008D93bc03sc00i00
vendor   : NVIDIA Corporation
model    : TU106 [GeForce RTX 2060 SUPER]
driver   : nvidia-driver-525 - distro non-free
driver   : nvidia-driver-470 - distro non-free
driver   : nvidia-driver-535-open - distro non-free
driver   : nvidia-driver-450-server - distro non-free
driver   : nvidia-driver-535 - distro non-free
driver   : nvidia-driver-535-server-open - distro non-free recommended
driver   : nvidia-driver-525-open - distro non-free
driver   : nvidia-driver-525-server - distro non-free
driver   : nvidia-driver-535-server - distro non-free
driver   : nvidia-driver-470-server - distro non-free
driver   : xserver-xorg-video-nouveau - distro free builtin
```


nvidia-driver-535がちゃんと表示されれば、それをinstallする。高いものにしておけばOKだと思う。
```
$ sudo apt install nvidia-driver-535
```

終わったら
```
$ reboot
```
再起動の際、Secure bootはオフにする
再起動後、インストールされているか確認
```
$ nvidia-smi
```
以下が出ることを確認
```
Mon Jul 10 19:19:40 2023       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.54.03              Driver Version: 535.54.03    CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA GeForce RTX 2060 ...    Off | 00000000:01:00.0  On |                  N/A |
| 40%   36C    P8              16W / 184W |    320MiB /  8192MiB |      6%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A      1173      G   /usr/lib/xorg/Xorg                          206MiB |
|    0   N/A  N/A      1452      G   /usr/bin/gnome-shell                         56MiB |
|    0   N/A  N/A      2425      G   gnome-control-center                          2MiB |
|    0   N/A  N/A      4000      G   ...57794657,4500763611716079524,262144       53MiB |
+---------------------------------------------------------------------------------------+
```

## cuda install
[これを参考にする](https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=deb_local)
最後はsudo apt-get -y install cuda-12.2としてあげたほうがいいと思います。（他のバージョンもしかり）
```
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda-repo-ubuntu2204-12-2-local_12.2.0-535.54.03-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-12-2-local_12.2.0-535.54.03-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-12-2-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda-12.2
```

パスを通す
```
export PATH="/usr/local/cuda/bin:$PATH"

export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
```


確認
```
nvcc -V
```
以下が出ることを確認
```
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Jun_13_19:16:58_PDT_2023
Cuda compilation tools, release 12.2, V12.2.91
Build cuda_12.2.r12.2/compiler.32965470_0
```

## （おまけ）cuda バージョン切り替え
筆者は試していないですが、cudaのバージョンを切り替えたいときは`update-alternatives`を使うとできるらしい。追加でcuda入れるときはちゃんと`sudo apt-get -y install cuda-XX.X`のように指定する。
[参考サイト](https://www.nemotos.net/?p=2374)