Install CUDA on Ubuntu 20.04
- 1. Pre-procedure before install
- 1.1 Verify the system has a CUDA-capable GPU.
- 1.2 Verify the system is running a supported version of Linux.
- 1.3 Verify the system has gcc installed.
- 1.4 Verify the system has the correct kernel headers and development packages installed.
- 1.5 Remove previously installed CUDA toolkit
- 1.6 Remove previously installed CUDA driver
- 1.7 Remove possible files
- 2. Install CUDA
To record my experience on installing CUDA on Ubuntu 20.04.
1. Pre-procedure before install
(More details can be found in [1] [2])
1.1 Verify the system has a CUDA-capable GPU.
lspci | grep -i nvidia
If your graphics card is from NVIDIA and it is listed in https://developer.nvidia.com/cuda-gpus, your GPU is CUDA-capable.
1.2 Verify the system is running a supported version of Linux.
uname -m && cat /etc/*release
1.3 Verify the system has gcc installed.
gcc --version
1.4 Verify the system has the correct kernel headers and development packages installed.
uname -r
1.5 Remove previously installed CUDA toolkit
sudo apt-get --purge remove "*cublas*" "cuda*" "nsight*"
1.6 Remove previously installed CUDA driver
sudo apt-get --purge remove "*nvidia*"
1.7 Remove possible files
sudo rm -rf /usr/local/cuda*
2. Install CUDA
2.1 Choose your CUDA version and installation method
Go to official website and follow their instructions to download corresponding version of CUDA and installation method. Local and network are both recommended. For example, CUDA Toolkit 11.6 deb[network].
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
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda
2.2 Reboot
sudo reboot
Reboot and then set security key. When using the command to reboot, choose MOK.
2.3 First-round Check
nvdia-smi
‘nvdia-smi’ should show information about your GPU.
nvcc --version
If it also returns the correct version of CUDA, you have completed.
If ‘nvcc --version’ can not found anything or raise an error, use the following command
whereis nvcc
If it returns ‘nvcc:’, don’t worry and please do the remaining steps.
2.4 Add CUDA to path
sudo nano ~/.bashrc
Open the file and add the following texts to the last of this file
export PATH="/usr/local/cuda-11.4/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH"
Remember to change ‘11.4’ to the version you want to install.
2.5 Second-round Check
Both of the following commands should work now.
nvdia-smi
nvcc --version