The process of installing CUDA is pretty simple. Here are a few tips:
- DON'Ts
- Do NOT follow the NVIDIA CUDA Installation Guide for Linux, which seems weird now, but it seems the content of it has not been updated for a while even though version numbers keep bumping.
- Do NOT install CUDA by simply enter
sudo apt install nvidia-cuda-toolkit
because the installed version is very old (CUDA 9.0) if you are on Ubuntu
- Go straight towards to the official CUDA developer website and go to the download page
- the CUDA developer website is something like
https://developer.nvidia.com/zh-cn/cuda-zone
which may differ according to your region. - the CUDA download page's address is
https://developer.nvidia.com/cuda-downloads
now.
- On the download page, you should be able to select your OS, architecture, distribution (for Linux only), OS version and installer type.
- After checking all the selection box, you should see installation instructions.
- Take (Linux, x64, Ubuntu, 18.04, deb_network) as an example, I get instructions like
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
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda
- make sure you have sudo privilege.
- If everything goes well, the installation should complete in ~10 minutes depending on your network condition.
- Reboot your machine
- After rebooting, you should add paths and dynamic linking path to CUDA to your
PATH
andLD_LIBRARY_PATH
, simply insert the below two lines in your shell rc file.
export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
source
your shell, woola, all done
It is not uncommon that everything can go wrong on Linux lol. The below are some issues I found when I install CUDA with the setting (Linux, x64, Ubuntu, 20.04, deb_network).
Referring to this post, it is caused by duplicate or legacy installations of some CUDA dependencies. So, one way to solve this is to remove all nvidia packages.
Try these commands, but make sure you understand what these means:
apt clean
apt update
apt purge cuda
apt purge nvidia-*
apt autoremove
apt install cuda
If you installed the wrong CUDA by chance, say, the version for Ubuntu 18.04 on your Ubuntu 20.04. You will need to not only execute these commands but also clean your old repo links in apt
.
The below commands are for installing CUDA for Ubuntu 18.04,
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
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
sudo apt-get update
sudo apt-get -y install cuda
but if you are on 20.04 and you installed the wrong one, you will need to "undo" below two commands:
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
The first command is easy to be "undone" by simply removing the file.
The second command is "undone" by adding -r
option, which is
sudo add-apt-repository -r "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
Then apt
is ready for installing new CUDA. Don't forget to remove all old CUDA-related packages before you install the new one.