# NVIDIA driver / CUDA installation
###### tags: `linux` `NVIDIA` `driver` `PyTorch` `CUDA`
linux ubuntu 16.04
CUDA 10.0 + cuDNN7.3
## NVIDIA Driver
For ubuntu 16, if the NVIDIA driver isn't installed well, it may have the problem with login loop.
[download the NVIDIA driver](https://drive.google.com/file/d/1i-JS0JCUiChTro0lnT-2nDW0IlZ8NHP3/view?usp=sharing)
Let the run file executable:
```
sudo chmod +x NVIDIA-Linux-x86_64-430.40.run
```
1. [uninstall nvidia](https://yylin1.github.io/2018/12/01/install-nvidia-driver-problem/)
To make sure that the nvidia driver in the computer has totally removed.
```=
sudo apt-get remove --purge nvidia-*
sudo apt-get autoremove
sudo nvidia-uninstall
```
check by dpkg
```=+
sudo dpkg -l 'nvidia'
sudo dpkg --remove nvidia-{name}
```
reboot
```=+
reboot
```
2. install
Press Alt+Ctrl+F1 to enter text mode
```=
sudo service lightdm stop
sudo ./NVIDIA-Linux-***.run
sudo reboot
```
Option choose the default value.
3. check
```
nvidia-smi
```

### Installation failed
During Nvidia driver installation, if encountering following error message:
> The Nouvean kernel derive is currently in use by your system
Just turning off the Nouveau by **[steps 2-4](#CUDA-install) in following section: CUDA install**, reboot, and install Nvidia driver again.
## CUDA install
[download the cuda driver](https://drive.google.com/file/d/18y3GIMn5PcoKEinxUV9JEp9gHABQrqZk/view?usp=sharing) or you can download the driver from the [website](https://developer.nvidia.com/cuda-10.0-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1604&target_type=runfilelocal)
> You can choose other CUDA driver version you want if you don't need to use the cuDNN and just want to use the CUDA to speed up the program.Just change the correct file name below and follow the same steps.
Let the run file executable:
```
sudo chmod +x cuda_10.0.130_410.48_linux.run
```
1. Follow the [pre-install step](https://docs.nvidia.com/cuda/archive/10.0/cuda-installation-guide-linux/index.html#pre-installation-actions)
2. Turn off the Nouveau driver(內顯)
```
lsmod | grep nouveau
```
3. Create a file at /etc/modprobe.d/blacklist-nouveau.conf with the following contents
```
sudo vim /etc/modprobe.d/blacklist-nouveau.conf
```
```=
blacklist nouveau
options nouveau modeset=0
```
4. Regenerate the kernel initramfs
```
sudo update-initramfs -u
```
5. Check Nouveau driver again
```
lsmod | grep nouveau
```
6. Install CUDA driver
Press Ctrl Alt F1 to enter text mode
**Remember don’t install nvidia driver again in this part !!!!!**
```c=
sudo service lightdm stop
sudo sh cuda_10.0.130_410.48_linux.run --no-opengl-libs
// remember don’t install nvidia driver again in this part
// there is only one option that you need to type NO to refuse install nvidia driver again
sudo reboot
```
7. Add the following contents into ~/.bashrc file to setup env variable for cuda
```=
vim ~/.bashrc
```
```=+
export PATH=/usr/local/cuda-10.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
```
```=+
source ~/.bashrc
```
8. Check the CUDA install
```c=
cd ~/NVIDIA_CUDA-10.0_Samples/
sudo make
// After build it should create a binanry file in following directory
cd ~/NVIDIA_CUDA-10.0_Samples/bin/x86_64/linux/release/
./deviceQuery
// If you see the message below in the end it means you install CUDA successfully:
/*
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.1, CUDA Runtime Version = 10.0, NumDevs = 1
Result = PASS
*/
```
9. Other ways to check the CUDA install
```
nvcc -V
```
```
cat /usr/local/cuda/version.txt
```

## CuDNN install
To use the cuda on pytorch, we need to install the CuDNN.
To download the [Ubuntu+CUDA10.0 driver](https://drive.google.com/file/d/1UM4VqyVYwmCwrRr1J9ub5Hssz_Hvdx8K/view?usp=sharing) or other version from the [website](https://developer.nvidia.com/rdp/cudnn-download)( need to register the account).
1. Unzip

2. copy file
```=
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/lib* /usr/local/cuda/lib64/
```
3. make link
```=
cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.7.6.5
sudo ln -sf libcudnn.so.7.6.5 libcudnn.so.7
sudo ln -sf libcudnn.so.7 libcudnn.so
sudo ldconfig
```

4. Check CuDNN
```
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
```

## pytorch cuda
If you have installed the pytorch before installing the CUDA and CuDNN, please uninstall the pytorch first.
### Uninstall pytorch
```python=
conda uninstall pytorch
conda uninstall pytorch-nightly
conda uninstall cuda10.0 # whatever version you have (can be checked by conda list)
# do twice
pip uninstall pytorch
pip uninstall pytorch
```
### Install pytorch
```=
conda install -c pytorch pytorch
conda install -c fragcolor cuda10.0
conda install -c pytorch torchvision
conda install -c anaconda pytorch-gpu
```
### Check cuda
```python=
print(torch.cuda.is_available())
print(torch.version.cuda)
print(torch.randn(2,2).cuda())
```

## Reference
1. [Ubuntu 16.04 上安裝 CUDA 9.0 詳細教程](https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/571419/)
2. [Ubuntu 16.04 安裝CUDA 10.0 + cuDNN 7.3](https://medium.com/@zihansyu/ubuntu-16-04-%E5%AE%89%E8%A3%9Dcuda-10-0-cudnn-7-3-8254cb642e70)
3. [[NV] How to check CUDA and cuDNN version](https://medium.com/@changrongko/nv-how-to-check-cuda-and-cudnn-version-e05aa21daf6c)
4. [How To Install Pytorch 1.0 With Cuda 10.0](https://medium.com/@_willfalcon/how-to-install-pytorch-1-0-with-cuda-10-0-169569c5b82d)