Installing CUDA 10.1 on Ubuntu 20.04

NOTE: This also works for Ubuntu 18.04

Do not get frustrated.

When I was installing CUDA and CuDNN on my new laptop, I encountered so many bugs, uninstalls, reinstalls, and error messages, that it literally took me two days.

You shouldn’t have to do that.

If you want to install CUDA on your machine, and you’re running Ubuntu 20.04 (Focal Fossa) OR Ubuntu 18.04, just follow these instructions, and you’ll be set in 5 minutes.

1. Clean up

(a) Open a terminal window and type the following three commands to get rid of any NVIDIA/CUDA packages you may already have installed:

sudo rm /etc/apt/sources.list.d/cuda*
sudo apt remove --autoremove nvidia-cuda-toolkit
sudo apt remove --autoremove nvidia-*

(b) Purge any remaining NVIDIA configuration files and the associated dependencies that they may have been installed with.

sudo apt-get purge nvidia*
sudo apt-get autoremove
sudo apt-get autoclean

(c) Remove any existing CUDA folders you may have in /usr/local/

This threw me for a loop the first several times I tried installing CUDA. There shouldn’t be any folders with the name “cuda” or “cuda-anything” in usr/local/ at this point!

sudo rm -rf /usr/local/cuda*

2. Install

(a) Setup your CUDA PPA. (Read here if you’re curious about what a PPA is.)

Essentially, we’re adding CUDA to our sources.list, which is the file that’s referenced any time we use the apt package manager to download stuff in the terminal with a command like “sudo apt update”

sudo apt update
sudo add-apt-repository ppa:graphics-drivers
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pubsudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda_learn.list'

(b) Install CUDA 10.1 packages, including the CuDNN library

sudo apt update
sudo apt install cuda-10-1
sudo apt install libcudnn7

3. Add CUDA to PATH

(a) After installing, we need to add CUDA to our PATH, so that the shell knows where to find CUDA. To edit our path, open up the ‘.profile’ file using vim.

sudo vim ~/.profile

(b) Finally, add these lines to the end of the file.

# set PATH for cuda 10.1 installation
if [ -d "/usr/local/cuda-10.1/bin/" ]; then
export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
fi

4. Reboot

(a) Reboot your computer (as you should after any driver install)

5. Final Check

Now, let’s check to make sure everything’s set up!

(a) Check NVIDIA Drivers:

nvidia-smi 

(b) Check CUDA:

nvcc --version

(c) Check CuDNN

/sbin/ldconfig -N -v $(sed ‘s/:/ /’ <<< $LD_LIBRARY_PATH) 2>/dev/null | grep libcudnn

As you long as these three checks didn’t throw you any nasty error messages, you’re all set!