Tutorials

Installing Ubuntu in Dual boot, the Nvidia, Cuda, and CudNN drivers for GPU 3070 RTX on a laptop14 min read

Jan 11, 2022 9 min

Installing Ubuntu in Dual boot, the Nvidia, Cuda, and CudNN drivers for GPU 3070 RTX on a laptop14 min read

Reading Time: 9 minutes

Almost every programmer has to deal with Linux at some point in their career, and will most likely end up installing it from scratch on a new machine. However, while this installation is much easier than it was earlier on, some pain points still exist, especially if you try to configure the graphical drivers. This tutorial tries to address those pain points as well as to clarify some seemingly obscure steps but fails at being exhaustive since each installation likely depends on the hardware configuration.

Disclaimer: Be wary that the install depends heavily on the hardware you have (it worked fine on AORUS Gigabyte laptops with a RTX 3070 and i7 core,  Alienware m15 r4 with an intel core i7, and a RTX 3080); what’s written here, although being general enough to target most of the problems encountered during the install, might not suffice. Be prepared to browse askUbuntu and StackOverflow, you’ll find useful resources at the end for common problems. This tutorial also assumes that your PC only has Windows10 installed with secure boot enabled by default.

Requirements

For this installation, you will need:

  • A usb stick (8Gb should suffice)
  • A laptop with Windows 10 installed 
  • A Nvidia GPU (RTX 3070 for this example)

Installing Ubuntu with secure boot enabled

Create a Bootable usb stick (on Windows)

This is the preferred method for installing Ubuntu on a laptop.

  • Follow this tutorial to learn how to create the bootable usb stick with the right ISO version of the distribution you wish to install (in our case Ubuntu 20.04) and burn the ISO using Rufus

Troubleshoot: if at this point Rufus throws an exception saying that it was unable to burn the ISO, it might be because of the poor formatting of the stick. The easy way out would then be to create a bootable USB stick using a Linux distro by following this tutorial https://ubuntu.com/tutorials/create-a-usb-stick-on-ubuntu#1-overview if you’re more comfortable with GUI, or by using the dd command on any unix system with the correct parameters: you can follow this tutorial.

Once you’ve got your USB stick ready, it’s time to change your Bios setting so as to be able to install the Ubuntu distro.

Setting the right BIOS options

Just a quick reminder, the BIOS is the low-level software responsible for the initialization of the hardware of the computer at boot time. It’s here that you can specify the boot priority.

  • Type F12 at boot time to enter BIOS setup
  • Browse the options and change TPM 2 to TPM 1_2. This ensures that the install of Ubuntu won’t fail because of compatibility issues with Windows 11.

To clarify the last point, TPM comes from the name Trusted Platform Module and serves with authentication, which is used by the UEFI (Unified Extensible Firmware Interface) to validate the boot integrity. Among other things, TPM 2 supports more hashing algorithms and doesn’t have exactly the same architecture as TPM 1_2.
At this point, the only thing left to do on Windows is to free the amount of space you want for Ubuntu.

Partitioning

Currently, your computer contains a hard drive that’s fully occupied by Windows. Partitioning means that you’re dividing it into independent sections, and on one of those you’ll format it in ext4 and install Ubuntu.

Free Space on Windows

  • Type “partition” in the search bar and click on the Windows partitioning tool that pops up.
  • Select the C:// disk (where Windows is installed)
  • Right click on it and select “shrink volume”
  • Shrink the volume up to the required size

The volume has now been shrunk and some space has been freed for Ubuntu to be installed, but it has not been partitioned yet.

Partitioning with Ubuntu

  • Boot on the USB stick freshly created (on the 3.0 USB port since some users have experienced issues booting on the 2.0 port for some reason)
  • Select “install Ubuntu”
  • Select “Something else” once prompted the screen that specifies the way you want to install it.
  • Once prompted the partition table, select the “free space” slot and tick the checkbox. Tick the default suggested options (primary, Ext4 filesystem). You’ve now successfully created a usable partition where you’ll install Ubuntu
  • Backtrack and select “Normal Installation”
  • Follow the straightforward instructions

Following those instructions should lead you to the point where your pc needs to reboot in order to complete the installation.

Side Note: Don’t check the options to register third-party software (namely the drivers) because it’ll require the setup of Secure Boot and you don’t want to do that just yet.
If you’re curious about the ext4 filesystem,
this Wikipedia page nicely exposes its features.
This step could most likely be avoided by partitioning the space that was freed directly on Windows, however, I did not try it that way.

Troubleshoot

If at this point the device reboots and can’t load/is stuck on the loading screen

  • Select “Recovery mode” in the “Advanced options” of the boot menu,
  • Then “Repair broken packages with dpkg” and “Resume normal boot”.

When it’s done its job you should be able to boot into your Ubuntu distro. Also given your configuration, it’s most likely due to an issue with the nouveau driver (https://nouveau.freedesktop.org/) which is the default OSS driver on Ubuntu.

Nvidia & CUDA install for RTX 3000 series

The following instructions were used to add GPU drivers for a running installation of TensorFlow 2.6 with GPU support. This was tested on a fresh install of Ubuntu 20.04 with a GPU Nvidia Geforce RTX 3080. A lot of things can go wrong at that point and you have every chance of needing to reinstall your whole Ubuntu distro (and even brick your device if you fail too hard) so be prepared and brace yourself.

The extreme case being your Intel chipset and RTX are too advanced for the built-in open-source driver nouveau, which will directly cause your system to crash with an exception looking like nouveau: … mthd 128 … UNRESOLVABLE_HANDLE .

Clean your system from previous installations

Run this step if you are not starting with a fresh install of Ubuntu.

sudo /usr/local/cuda/bin/cuda-uninstallersudo /usr/bin/nvidia-uninstall
sudo apt-get remove --purge nvidia*
sudo apt-get remove --purge cuda*
sudo apt-get autoremove

And reboot your system.

Note: If the first line does not work, it simply means that Cuda is not installed so no worries if you get an exception.

Installing Nvidia drivers

We have two ways to install the drivers, either by GUI(Graphical installation) or manually. They differ by the GUI and mostly by the fact that you get to see what’s happening in the case you choose to install them manually.

Warning: Nvidia drivers can be installed manually. However, not doing it correctly often breaks the boot options of Ubuntu and can lead to further problems. 

Secure Boot is Enabled (UEFI)

For this case, I’d highly recommend directly changing the default grub options to see what’s going on when you start as it can help troubleshoot bad behaviors.

(Optional) Changing your boot settings to verbose logging

Type the following commands 

cd /etc/default/
sudo vim grub

In this file do the following and save

GRUB_CMDLINE_LINUX_DEFAULT=""

type /!\ In Vim, to save your changes and quit

then type either

:wq
OR
zz

Note: “:” puts you in command mode, “w” makes you write your changes and “q” makes you quit. If ever you’re stuck with changes you want to discard and you’re panicking not knowing how to get out, “:q!” will do the trick! A small link to why you should learn Vim and why it’s great!

After quitting vim

sudo update-grub
sudo reboot

This will ensure that you see everything that’s going on at the beginning and spot potential problems.

Verify that the kernel version installed matches the headers (some users have experienced kernel version changes with certain Nvidia drivers for some reason), otherwise it’ll probably fail at some point, type the following :

uname -r
cd /usr/src

Ideally, you want to have the same version for the Linux headers as your kernel or at least headers that match the version of your kernel, it’s okay to have other versions as well since they won’t interfere with your install anyways.

2.1.1 GUI Install

If you prefer the Graphical User Interface (GUI) installation, you can follow these steps.

  • Search for Software & Update.
  • Go to the tab ‘additional drivers’.
  • Select the latest driver metapackage which is ‘proprietary, tested’ and apply changes.
  • Reboot your machine.

Error: blank screen at reboot

Warning: You may get a blank screen after rebooting. In that case, you can get a terminal by pressing Ctrl+Alt+F3 and log into your account. Then, edit your grub file:

sudo vi /etc/default/grub
edit the file:

sudo update-grub

edit the following line:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
to become:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash nomodeset"
and reboot.

Note: Quiet splash means that you hide what the computer is doing in the background during the loading time of Ubuntu (typically the ‘splashscreen’)

Verify that drivers work

In a terminal, the command nvidia-smi should output something like:

Manual Install

Run the following commands replacing the xx with the corresponding driver version:

sudo add-apt-repository ppa:graphics-drivers/ppa
do apt install nvidia-driver-4xx

Note: if any problem arises at that point, go to https://gist.github.com/bitsurgeon/b0f4440984c9e60dcd8fe8bbc346c029 : the gist is really well done and might be helpful.

Secure Boot is Enabled (UEFI) – Registration (needed no matter how you installed the drivers)

After installing the drivers, you have to register a secure boot key for it to be able to work on your device. Beware, the password is critical because if you fail to remember it, you may have to reinstall the drivers from scratch. The password has to be between 8 and 16 characters.  Be careful,  your keyboard layout will change to QWERTY on that screen. Once the password is set, follow these steps :

  • Reboot
  • Select “Enroll MOK”
  • Select “Continue”
  • Select “Yes”
  • Type your password
  • Reboot
  • Test your configuration by typing  “nvidia-smi” in a command prompt and plug an external monitor to see if the display is right.

Troubleshooting: If you get the error “NVIDIA-SMI has failed because it couldn’t communicate with the Nvidia driver. Make sure that the latest Nvidia driver is installed and running”

Start by typing “prime-select query” in a command prompt to see if you’re using the Nvidia drivers or not (if you are, the output will be “nvidia”) . If you are, a lot of things could have gone wrong: your kernel might be incompatible with the version of the Nvidia driver and you might want to test recompiling older versions of it to see if it fixes your problem. Otherwise, you might have mistaken your install with a Legacy OS one (where secure boot is disabled) and therefore have overlooked the UEFI-specific install described above, so you should try it (although it seems unlikely if you managed to enroll MOK).

Only your external monitor is recognized at boot time

You boot and you feel like your install has frozen while in reality if you plug in your second monitor via HDMI, you can see the screen just fine! You can diagnose this by typing prime-select intel in the command line and rebooting to see if only your laptop screen is recognized now. It means that when the driver was installed, it likely overwrote your xorg.conf file and set it only to work with your second monitor. To fix this, type

sudo rm /etc/X11/xorg.conf
touch /etc/X11/xorg.conf

And this should do the trick after a reboot. If you don’t have xorg.conf in your /etc/X11 folder, simply create an empty one and things should be fixed.

Installing CUDA drivers

To download and install the correct driver

Perform a manual search:

This URL gives you the correct file to download and run by running

wget https://developer.download.nvidia.com/compute/cuda/11.4.1/local_installers/cuda_11.4.1_xxx.xx.xx_linux.run

sudo sh cuda_11.4.1_xxx.xx.xx_linux.run

You will then be prompted about what to install. Select only the Cuda library, without samples or doc (because you won’t install the drivers). It’s really important that you do not select the drivers.

Once the installation is over, edit your .zshrc / .bashrc to add :

export PATH="/usr/local/cuda-11.4/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.4/lib64:$LD_LIBRARY_PATH"

at the end of the file. Save the file and restart your terminal.

Verify that the installation worked

Now nvcc –version should yield something similar to :

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Wed_Jul_14_19:41:19_PDT_2021
Cuda compilation tools, release 11.4, V11.4.100
Build cuda_11.4.r11.4/compiler.30188945_0

Installing cuDNN

To download and install the correct drivers

Download the .deb installers from the website:

Now, install the two files using dpkg.

cd ~/Downloadssudo dpkg -i libcudnn8_x.x.x-1+cudax.x_amd64.deb
sudo dpkg -i libcudnn8-dev_8.x.x.x-1+cudax.x_amd64.deb

Verify the overall installation

We will install TensorFlow in a venv and verify that it has access to the GPU.

create a venv

useful only if you do not a virtualenv tool yet

sudo apt-get install python3-venv

make sure you do not have another venv with the same name in the same location

python3 -m venv venv

install tensorflow in the venv

source venv/bin/activate
pip install tensorflow

ensure that it can access your GPU

python

>> import tensorflow as tf
>> tf.test.is_gpu_available()

>>> # you can also use tf.config.list_physical_devices(‘GPU’) and see CPU and GPU devices

The last line in Python should return True.

Conclusion

Congratulations! If you’ve followed this tutorial step by step and (hopefully) haven’t encountered any other issues than those mentioned above, your Ubuntu installation should be ready to use with the right Nvidia and Cuda drivers!

If ever you find yourself stuck because a step that you tried to reproduce didn’t work as expected (most likely due to the differences in the hardware), try to browse through AskUbuntu, The Ubuntu Reddit, The Nvidia Reddit, or Stack Overflow that contain a lot of great insights. 

Your email address will not be published.