24

There seem be be several options to install CUDA on Ubuntu 20.10: It is pre-bundled with 20.10, there are various installers at the official NVIDIA page, etc.

Question: What is a recommended way to install CUDA 11.X on Ubuntu 20.10, and how do I verify the installation?

B0rk4
  • 1,673

2 Answers2

29

Install the NVIDIA driver

This might be an optional step, but it is always good to first remove potential previously installed NVIDIA drivers:

sudo apt-get purge *nvidia*
sudo apt autoremove

Next, let's install the latest driver:

sudo apt install nvidia-driver-455

After this, we need to restart the computer to finalize the driver installation. Next we can verify whether the drive was succesfully installed:

nvidia-smi

This should contain the following or similar:

NVIDIA-SMI 455.28 Driver Version: 455.28

Install CUDA Toolkit

Next we can install the CUDA toolkit:

sudo apt install nvidia-cuda-toolkit

We also need to set the CUDA_PATH. Add this

export CUDA_PATH=/usr

at the end of your .bashrc and run

source ~/.bashrc

Now your CUDA installation should be complete, and

nvidia-smi

should indicate that you have CUDA 11.1 installed.

Test the CUDA toolkit installation /configuration

One of the best way to verify whether CUDA is properly installed is using the official "CUDA-sample". Ubuntu does not package them as part of "nvidia-cuda-toolkit" but we can download them directly from NVIDIA's github page:

wget https://github.com/NVIDIA/cuda-samples/archive/v11.1.tar.gz
tar xvf v11.1.tar.gz 
cd cuda-samples-11.1

For whatever reason, NVIDIA did not chose to include a modern build system (e.g. cmake), but ships a plain old Makefile instead. If just running "make" does not work for you, carefully read the error messages and see whether e.g. some required dependencies are not installed.

In order to help the build process a little, it might be advisable to specify the compute architecture of your GPU.

  1. You can find out your GPU by running nvidia-smi. Mine is a Quadro RTX 3000.
  2. Next google your GPU to find out the corresponding compute architecture. For the Quadro RTX 3000, it is "turing", version 7.5.
  3. Specify the architecture version when running make, e.g.
make SMS="75"

If the compilation was succesful, you can try out one of the samples. For instance:

./bin/x86_64/linux/release/immaTensorCoreGemm 

You should see the following or similar output:

M: 4096 (16 x 256)
N: 4096 (16 x 256)
K: 4096 (16 x 256)
Preparing data for GPU...
Required shared memory size: 64 Kb
Computing... using high performance kernel compute_gemm_imma 
Time: 6.030176 ms
TOPS: 22.79
B0rk4
  • 1,673
  • This all is great, but the first command (sudo apt purge *nvidia*) will fail. Use apt-get instead. – N0rbert Nov 01 '20 at 08:40
  • Thanks, corrected. – B0rk4 Nov 02 '20 at 14:07
  • 2
    You might want to quote the "*nvidia*" so any files in your current working directory with "nvidia" in their names do not mess up the command. – ubfan1 Nov 06 '20 at 17:16
  • libcuda is not installed errors on make SMS="61" – Sephethus Dec 11 '20 at 14:06
  • 2
    This installs nvcc of a lower version than the rest of the installation. This is not without problems, e.g. https://github.com/NVIDIA/cuda-samples/issues/57 where the older nvcc can't compile the newer (v11.2) code. – Giorgos Sfikas Feb 02 '21 at 07:26
  • I hit the same issue as @GiorgosSfikas - it looks like cuda v11.0 is in the ubuntu repos for 20.10, so I just downloaded the matching sample code to make it work. People should also check nvcc -V for the appropriate version. – Neal Fultz Mar 27 '21 at 22:18
  • How do you check the architecture version? My GPU is a GeForce GTX 980, Maxwell architecture, but I seem to be unable to find the version to call make SMS="XX" – plr Apr 29 '21 at 21:12
  • 7
    sudo apt install nvidia-cuda-toolkit installs cuda 10.1 on Ubuntu 20.04. Not Cuda 11 – rob Apr 30 '21 at 08:47
11

since all of the explanations i found so far were not satisfying, here are the steps i came up with to install the latest nvidia driver (465) with cuda 11.3

first you have to uninstall all cuda and nvidia related drivers and packages

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

then

reboot

then (if not already done) disable nouveau as described here: https://askubuntu.com/a/951892/634048 and reboot again

download cuda and install.

wget https://developer.download.nvidia.com/compute/cuda/11.3.1/local_installers/cuda_11.3.1_465.19.01_linux.run
sudo sh cuda_11.3.1_465.19.01_linux.run

Follow the post-installation instructions found on the CUDA Toolkit Installation Guide for Linux. This involves updating the PATH and environment variables:

export PATH=/usr/local/cuda-11.3/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64\
                 ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

IMPORTANT if you need 32bit support - there are several applications only running with 32-bit drivers (like steam)

opt out of installation of nvidia drivers for cuda installation and install drivers from here: https://www.nvidia.com/download/driverResults.aspx/171980/en-us also check if driver is compatible for your model! (in general that should be the case)

sudo sh 'NVIDIA-Linux-x86_64-465.19.01.run' 

and opt in for 32-bit support when asked

done

I did NOT test it for any other versions than 20.04, but it should work for 18.04 to 21.04

YAMM
  • 211
  • 2
    Thanks for the input, it works well! However 11.4 version is out already, so would be good to remove version-specific advice from your post. And there are some more install methods except runfile, which could be found here https://developer.nvidia.com/cuda-downloads.

    Also adding export variables to ~/.bashrc prevents them from being lost upon reboot

    – fiction Jul 27 '21 at 09:36
  • As of August 2021, version 470 does not work reliably on Ubuntu (by my own experience), so I think the 465 recommendation is appropriate. Here's a link to install instructions for all versions in case people need to downgrade: https://developer.nvidia.com/cuda-toolkit-archive – Benjamin Bray Aug 07 '21 at 06:13
  • Awesome thanks man ! This worked for me but removing my existing nvidia display drivers install meant that my second monitor stopped displaying signal. and I had to do the additional steps

    make sure nvidia-prime is installed (sudo apt install nvidia-prime) switch to nvidia (sudo prime-select nvidia) remove stray blacklist files (sudo rm /lib/modprobe.d/blacklist-nvidia.conf /etc/modprobe.d/blacklist-nvidia.conf) update the initrd (sudo update-initramfs -u) reboot

    Context: Laptop with dual integrated AMD and dedicated Nvidia, connected Nvidia directly to external monitor.

    – Charles Schleich Mar 27 '22 at 18:10
  • I was having trouble installing cuda 11.3 which turned out to be outdated version for my kernel 5.13.0-40-generic. I had to downgrade my kernel to 5.8.18. You can find the compatibility of kernel and cuda in system-requirement section here https://docs.nvidia.com/cuda/archive/11.3.1/cuda-installation-guide-linux/index.html – user_3pij May 09 '22 at 22:25
  • In the last, don't forget to remove the latest kernel – user_3pij May 09 '22 at 23:40
  • I like that this works if you want a specific version of cuda - you can go to the nvidia website, and look for runfile (local) of the version you want, and you're good to go (e.g. https://developer.nvidia.com/cuda-11-7-0-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=18.04&target_type=runfile_local) . Most guides only work for the latest version of cuda. That said, this also horked my screen resolution after step 1. :P – old_warbaker Dec 12 '22 at 00:52