Install CUDA in ubuntu

We can install CUDA library (for using a GPU) by the following command.

sudo apt install nvidia-cuda-toolkit

however it will install the latest release (CUDA 11) which is not supported by many research projects in machine learning yet.

from: https://docs.nvidia.com/deploy/cuda-compatibility/index.html

To install a specific version of CUDA, check the linux driver version using the command “nvidia-smi”, if not satisfy you need to update the linux driver.
Then download CUDA .deb files from
https://developer.nvidia.com/CUDA-TOOLKIT-ARCHIVE

Installation Instructions:
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600

wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804-10-2-local-10.2.89-440.33.01_1.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-10-2-local-10.2.89-440.33.01/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda-10-2
sudo apt-get -y install cuda    <- need 1 hr++ to download the upgrade packages needed for installationsudo apt-get -y install cuda-10-2
$ wget http://developer.download.nvidia.com/compute/machinelearning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-
1_amd64.deb
$ sudo apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
$ sudo apt-get update

Then, link the current CUDA version to path environment

#maybe not important to do
export CUDA_HOME=/usr/local/cuda-10.2
export CUDA_ROOT=/usr/local/cuda-10.2
export PATH=$PATH:$CUDA_HOME/bin:$CUDA_HOME/include:$CUDA_HOME
export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH:$CUDA_HOME/include
export CUDA_INC_DIR=$CUDA_INC_DIR:$CUDA_HOME:$CUDA_HOME/include

#sudo dpkg -i cuda-repo-ubuntu1804_10.2.89-1_amd64.deb
sudo dpkg -i libcudnn8_8.0.3.33-1+cuda10.2_amd64.deb
sudo dpkg -i libcudnn8-dev_8.0.3.33-1+cuda10.2_amd64.deb
sudo dpkg -i libcudnn8-samples_8.0.3.33-1+cuda10.2_amd64.deb

Download cudnn ( a GPU-accelerated library for deep neuron networks) version that match the CUDA from
https://developer.nvidia.com/rdp/cudnn-archive

Check current version of CUDAcat /usr/local/cuda/version.txt
nvcc –version
Check cudnn versioncat /usr/include/x86_64-linux-gnu/cudnn_v*.h | grep CUDNN_MAJOR -A 2
cat /usr/include/cudnn.h | grep CUDNN_MAJOR -A 2
open bashrc file.gedit .bashrc
reload bashrc, after change it content.source ~/.bashrc
check symbolic link in the folderls -l /usr/local/
create symbolic linkln -s <source_file_directory> <link_file_directory>
print environment variableenv
env | grep -i cuda
rm filename
Useful commands

cat is use for print the detail inside a file.
.bashrc is a file that contains shell configurations and variable of the system.

dpkg is a tool to install, build, remove and manage Debian packages.
-i : install
-p : purge