How to Install CUDA ToolKit 11.0, and Nvidia Display Driver on Ubuntu 20.04

Beyond State-of-the-Art
8 min readAug 24, 2020

Also explains how to fix the brightness control not working problem seen in Ubuntu with the Nvidia display drivers.

Sample output to verify the Cuda installation

This instruction guide is for Ubuntu 20.04 and CUDA Toolkit 11.0.3

Majority of the instructions are taken according to the official documentation from Nvidia: Installation Guide Linux

Contents in short detail:

  1. Check for previous installations.
  2. Pre-installation requirements.
  3. Install Display Driver, CUDA Toolkit (Runfile installation method).
  4. Post-installation requirements.
  5. Uninstall and revert back to the previous state.
  6. What to do if you mess up (you see a full or partial black screen)
  7. Fix buggy brightness control

The hardware I have used to test the installation is — Lenovo Legion — Intel Core i7 9th Gen. — Nvidia GTX 1650 — Ubuntu 20.04 x86_x64 bit version.

Some basic prior knowledge in Ubuntu / Linux is assumed but not necessary to read and understand this webpage. Make sure you backup your system before you perform any actions explained in this webpage. So let’s start with the installation instructions in detail.

1. Check for previous installations:

Before we begin, it is required to check if there is any Nvidia driver is already installed in your Ubuntu 20.04 system.

Go to Softwares & Updates from the All Application window.

Select Additional Drivers from the tab and check which option is active in your PC.

  • In case ‘Using X.Org X server -Nouveau driver from xserver-xorg-video-nouveau(open source)’ is active, keep it that way.
  • If any of the proprietary drivers are in use, for example, if ‘Using NVIDIA driver metapackage from nvidia-driver-440(proprietary, tested)’ is selected, then there is already an Nvidia display driver installed. But this driver may conflict with our CUDA installation in the future. To avoid this, first, we need to change this to Using X.Org X server -Nouveau driver from xserver-xorg-video-nouveau(open source) option.
  1. You can do this by simply changing the option and click on ‘Apply Changes’. Or use the command-line to install the nouveau driver:
$ sudo apt install xserver-xorg-video-nouveau

Restart the system to take effect.

We also need to uninstall the already installed version of Nvidia Display Driver. So follow the instructions:

2. See all installed Nvidia packages

$ dpkg -l | grep -i nvidia

3. Remove Nvidia driver

$ sudo apt-get --purge remove "*nvidia*"

4. Optional step if above didn’t work(Clean everything and enable nouveau driver):

sudo apt-get remove --purge '^nvidia-.*'
sudo apt-get install ubuntu-desktop
sudo rm /etc/X11/xorg.conf
echo 'nouveau' | sudo tee -a /etc/modules

2. Pre-installation requirements:

  1. Check if you have a CUDA-Capable GPU
$ lspci | grep -i nvidia

2. Check gcc is installed

$ gcc --version

3. Check kernel headers and development packages are installed

$ sudo apt-get install linux-headers-$(uname -r)

4. If you have installed Cuda Toolkit or Driver remove them using:

Uninstall a Toolkit runfile installation:
$ sudo /usr/local/cuda-X.Y/bin/uninstall_cuda_X.Y.pl
Uninstall a Driver runfile installation:
$ sudo /usr/bin/nvidia-uninstall
Uninstall a RPM/Deb installation (also explained in section 1):
$ sudo apt-get --purge remove <package_name>

5. Disabling Nouveau

Create a file 'blacklist nouveau' at /etc/modprobe.d
$ sudo gedit /etc/modprobe.d/blacklist-nouveau.conf
  1. Add the following contents to the file:
blacklist nouveau
options nouveau modeset=0

2. Regenerate the kernel initramfs

$ sudo update-initramfs -u

You should not restart/shutdown the system after this step. If you do, then you may end in a black screen because your pc does not have a display driver.

Nvidia documentation says The reboot is required to completely unload the Nouveau drivers and prevent the graphical interface from loading. The CUDA driver cannot be installed while the Nouveau drivers are loaded or while the graphical interface is active.

The text was taken from Nvidia Cuda installation guide Linux

But in my experience, it is a total chaos situation and you might get stuck at a black screen, and then you will have to hold your Power Button to Reset your PC. Still to save your PC from this state, you have to go to recovery mode and enable nouveau driver again using root command-line:

$ sudo apt install xserver-xorg-video-nouveau

and then reboot back again normally. A detailed explanation is given under 6. What to do if you mess up (you see a full or partial black screen).

Even if Nouveau drivers were loaded, I was able to install CUDA and Nvidia Driver without any problems. I suggest not to do this specific step from Nvidia documentation. I installed everything while the graphical interface was active.

3. Install Display Driver, CUDA Toolkit ( Runfile installation method ):

Get the official CUDA Toolkit from https://developer.nvidia.com/cuda-downloads

For Ubuntu 20.04

  1. Download
$ wget https://developer.download.nvidia.com/compute/cuda/11.0.3/local_installers/cuda_11.0.3_450.51.06_linux.run

2. Run

$ sudo sh cuda_11.0.3_450.51.06_linux.run

Follow the on-screen instructions to install the software.

The installer should begin without any warnings or errors if everything went correct. If there is a notice to remove installed version of Nvidia package, please perform the section 1 again and check if there anymore package left to remove.

So that's it. You have successfully installed Nvidia driver and CUDA Toolkit. Now you need to perform post-installation actions.

4. Post-installation requirements:

  1. The PATH variable should include $ export PATH=/usr/local/cuda-11.0/bin${PATH:+:${PATH}}. To add this:
$ export PATH=/usr/local/cuda-11.0/bin${PATH:+:${PATH}}

2. Change the environment variables

$ export LD_LIBRARY_PATH=/usr/local/cuda-11.0/lib64\
${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Note: Above paths change if opted for a custom install path with the runfile installation method.

3. NVIDIA Persistence Daemon can be started by

$ sudo /usr/bin/nvidia-persistenced — verbose

4. Enable write permission for samples

$ sudo cuda-install-samples-11.0.sh /usr/local/cuda-11.0/samples

5. Verify Installation

$ cat /proc/driver/nvidia/version

6. Compile the example

$ cd ~/NVIDIA_CUDA-11.0_Samples/5_Simulations/nbody
$ make
$ ./nbody

This should give you the same result as shown in the video given at the start of this guide.

or

$ cd ~/NVIDIA_CUDA-11.0_Samples/1_Utilities/deviceQuery
$ make
$ ./deviceQuery

The result should be something similar to the following:

The output from running CUDA on NVIDIA GTX 1650

This confirms, your installation is complete and is perfectly working.

7. Optional: Install 3rd-party libraries

Install third-party libraries which may not be installed by default:

$ sudo apt-get install g++ freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libglu1-mesa libglu1-mesa-dev

Now your PC is equipped with CUDA Toolkit and Display Driver. Reboot your system to take effect.

5. Uninstall and revert back to the previous state:

As per the documentation of Nvidia the following steps are needed to Uninstall CUDA and Driver.

  1. Remove CUDA Toolkit:
$ sudo apt-get --purge remove "*cublas*" "*cufft*" "*curand*" \
"*cusolver*" "*cusparse*" "*npp*" "*nvjpeg*" "cuda*" "nsight*"

2. Remove NVIDIA Drivers:

$ sudo apt-get --purge remove "*nvidia*"

3. Enable nouveau driver

$ sudo apt-get install ubuntu-desktop
$ sudo rm /etc/X11/xorg.conf
$ echo 'nouveau' | sudo tee -a /etc/modules
$ sudo apt install xserver-xorg-video-nouveau

6. What to do if you mess up (you see a full or partial black screen):

So there can occur a situation when your PC screen goes black because you uninstalled the display driver and the system now does not know how to display and you are unable to see anything or perform a shutdown or restart.

Here is how you can possibly save your PC:

If you don't see a GNU GRUB bootloader when you switch on your PC, read the following instruction guide from Ubuntu to troubleshoot.

  1. Select ‘Advanced options for Ubuntu’ from the GNU GRUB bootloader menu. If you
  2. Select line ending with ‘recovery mode’ — usually 2nd option and press ENTER.
  3. From the recovery menu select ‘root’ — Drop to root shell prompt.
  4. At the command line perform
$ sudo apt install xserver-xorg-video-nouveau

5. Once done, type exit to return back to the recovery menu.

6. Select the ‘resume’ — Resume normal boot option and hit ENTER.

You should now reboot back with the nouveau display driver as default.

7. Guide to fix the buggy brightness control found in Ubuntu with the Nvidia display drivers:

I understand how annoying it is when you have a buggy display driver. Here is the legendary solution to fix this problem. For the reference, you can look at the following Nvidia website for more information.

https://docs.01.org/clearlinux/latest/tutorials/nvidia.html#brightness-control

  1. Add a kernel parameter for the nvidia driver: NVreg_EnableBacklightHandler=1.
$ sudo mkdir -p /etc/kernel/cmdline.d
$ echo "nvidia.NVreg_EnableBacklightHandler=1" | sudo tee /etc/kernel/cmdline.d/nvidia-backlight.conf

2. Add the EnableBrightnessControl=1 options to the Device section of your Xorg config

$ sudo mkdir -p /etc/X11/xorg.conf.d/
$ sudo tee /etc/X11/xorg.conf.d/nvidia-brightness.conf > /dev/null <<'EOF'
Section "Device"
Identifier "Device0"
Driver "nvidia"
Option "RegistryDwords" "EnableBrightnessControl=1"
EndSection
EOF

3. Make a configuration file with name ‘nvidia-drm-outputclass.conf’ at location /usr/share/X11/xorg.conf.d/

$ cd /usr/share/X11/xorg.conf.d/
$ sudo gedit nvidia-drm-outputclass.conf

4. Add the following text to the file nvidia-drm-outputclass.conf

Section “OutputClass”
Identifier “nvidia”
MatchDriver “nvidia-drm”
Driver “nvidia”
Option “RegistryDwords” “EnableBrightnessControl=1”
EndSection

5. Log Out and Log in to your PC

A log out is sometimes necessary because the above configuration snippet configures the X server to automatically load the nvidia X driver when it detects a device driven by the nvidia-drm.ko kernel module. Please note that this only works on Linux kernels version 3.9 or higher with CONFIG_DRM enabled, and only if the nvidia-drm.ko kernel module is loaded before the X server is started.

Now you can control brightness using Function hotkeys or using GUI of Ubuntu.

Disclaimer

The information contained in this webpage is for general information purposes only. You should not rely upon the material or information on this webpage as a basis for making any business, legal or any other decisions. I make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability, validity or availability of any information, products, services, or related graphics contained on the webpage for any purpose. Any reliance you place on such information is therefore strictly at your own risk. I will not be liable for any errors, omissions, or any losses, injuries, or damages arising from its display or use.

In no event will I be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this webpage.

Through this webpage, you are able to link to other websites which are not under my control. I have no control over the nature, content and availability of those sites. The inclusion of any links does not necessarily imply a recommendation or endorse the views expressed within them.

Because the information on this webpage is based on my personal opinion and experience, it should not be used as a piece of professional advice or recommendation.

--

--

Beyond State-of-the-Art

Articles on life, science, media and everything in between.