# Install NVidia driver and cuda on ubuntu
[HiPac 3 Release Note and informations](https://hackmd.io/2deAUP8zQ-u1giVioCOdaQ)
Remove current cuda + Nvidia driver
```
sudo apt-get purge '^nvidia-.*'
sudo apt-get autoremove
sudo apt-get purge '^cuda-.*'
sudo apt-get purge '^libcuda-.*'
sudo apt-get autoremove
sudo rm -rf /usr/local/cuda*
sudo rm -rf /usr/local/cuda
sudo rm -rf /usr/local/cuda-*
sudo rm -rf /etc/systemd/system/nvidia*
sudo rm -rf /var/lib/dkms/nvidia*
sudo rm -rf /usr/share/doc/nvidia*
sudo rm -rf /usr/share/man/man1/nvidia*
#update ram
sudo update-initramfs -u
```
Verify Cuda + Nvidia driver is deleted
```
dpkg -l | grep nvidia
dpkg -l | grep cuda
```
Check for available Nvidia driver
```
ubuntu-drivers devices
```
Check the version that cuda toolkit require.
[imgsrc](https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html)

After you know the Hardware Generation, go to [here](https://docs.nvidia.com/deploy/cuda-compatibility/index.html) and find a matrix under this article `Which GPUs are supported by the driver?`, find `Current Minimum Driver in Support` (or ctrl + f and type in `Current Minimum Driver in Support`), do not download driver lower than that.
```
sudo apt install nvidia-driver-535
```
Verify Nvidia driver installation
```
nvidia-smi
```
If failed, reboot and try again.
```
sudo reboot
```
:::info
`nvidia-smi`
```
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 555.58.02 Driver Version: 555.58.02 CUDA Version: 12.5 |
|-----------------------------------------+------------------------+----------------------+
| 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 xxxxxxxxxxxx ... Off | 00000000:01:00.0 Off | N/A |
| N/A 40C P0 23W / 115W | 15MiB / 8192MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 3099 G /usr/lib/xorg/Xorg 4MiB |
+-----------------------------------------------------------------------------------------+
```
:::
:::info
```
dpkg -l | grep nvidia
dpkg -l | grep cuda
```
```
ii nvidia-firmware-560-560.35.03 560.35.03-0ubuntu0~gpu24.04.4 amd64 Firmware files used by the kernel module
ii nvidia-kernel-common-560 560.35.03-0ubuntu0~gpu24.04.4 amd64 Shared files used with the kernel module
ii nvidia-kernel-source-560 560.35.03-0ubuntu0~gpu24.04.4 amd64 NVIDIA kernel source package
ii nvidia-prime 0.8.17.2 all Tools to enable NVIDIA's Prime
```
Make sure there are only 1 NVidia driver (`libnvidia-compute-*:amd64` doesn't matter).
:::
Cuda Download at [here](https://developer.nvidia.com/cuda-toolkit-archive)..
Choose the version `nvidia-smi` say (12.6).

Select `runfile(local)`, don't choose `deb(local)`, it will install other driver for you, which make the problem of two driver on same machine.

Uncheck `Driver` option by `Enter` and `up arrow` `down arrow` for choose.

Add environment variable
:::success
`vim ~/.bashrc`
```
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
```
:::
`source ~/.bashrc`
:::info
Verify cuda installation
`nvcc --version`
```
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Tue_Oct_29_23:50:19_PDT_2024
Cuda compilation tools, release 12.6, V12.6.85
Build cuda_12.6.r12.6/compiler.35059454_0
```
:::