Skip to content

Instantly share code, notes, and snippets.

@ifsheldon
Last active June 20, 2021 14:55
Show Gist options
  • Save ifsheldon/aeb58c2a455c2ea3d7322c53e27a7cf9 to your computer and use it in GitHub Desktop.
Save ifsheldon/aeb58c2a455c2ea3d7322c53e27a7cf9 to your computer and use it in GitHub Desktop.
Quick installation of CUDA on Ubuntu

Quick installation of CUDA on Ubuntu

The process of installing CUDA is pretty simple. Here are a few tips:

  1. 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
  1. 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.
  1. On the download page, you should be able to select your OS, architecture, distribution (for Linux only), OS version and installer type.
  2. 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.
  1. If everything goes well, the installation should complete in ~10 minutes depending on your network condition.
  2. Reboot your machine
  3. After rebooting, you should add paths and dynamic linking path to CUDA to your PATH and LD_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"
  1. source your shell, woola, all done

Troubleshooting

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).

CUDA install unmet dependencies...

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

Installed the wrong 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment