Matthias Cremieux

Matthias Cremieux

Instalación de Ubuntu en dual boot, los controladores de Nvidia, Cuda y CudNN para GPU 3070 RTX en un portátil

a dialogue box to finish an installation

Installing Ubuntu and Setting Up CUDA Drivers for RTX 3070 GPU

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 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 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 this link : 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 controladores o no (si lo está, la salida será “nvidia”) . Si lo está, muchas cosas podrían haber salido mal: su kernel podría ser incompatible con la versión del Nvidia controlador y quizás quiera probar a recompilar versiones anteriores para ver si soluciona su problema. De lo contrario, es posible que haya confundido su instalación con una de sistema operativo heredado (donde el arranque seguro está deshabilitado) y, por lo tanto, haya pasado por alto la instalación específica de UEFI descrita anteriormente, por lo que debería intentarlo (aunque parece poco probable si logró registrar MOK).

Solo su monitor externo es reconocido al arrancar

Arranca y siente que su instalación se ha congelado, cuando en realidad, si conecta su segundo monitor vía HDMI, ¡puede ver la pantalla perfectamente! Puede diagnosticar esto escribiendo prime-select intel en la línea de comandos y reiniciando para ver si ahora solo se reconoce la pantalla de su portátil. Significa que cuando se instaló el controlador, probablemente sobrescribió su archivo xorg.conf y lo configuró para que funcionara solo con su segundo monitor. Para solucionar esto, escriba

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

Y con esto debería bastar después de un reinicio. Si no tiene xorg.conf en su carpeta /etc/X11, simplemente cree uno vacío y las cosas deberían solucionarse.

Instalación de controladores CUDA

Para descargar e instalar el controlador correcto

Realice una búsqueda manual:

  • Vaya a Nvidia
  • Busque la configuración de su sistema y elija 'runfile'.

Esta URL le proporciona el archivo correcto para descargar y ejecutar mediante

wget

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

Se le preguntará qué desea instalar. Seleccione solo la Cuda biblioteca, sin ejemplos ni documentación (porque no instalará los controladores). Es muy importante que no seleccione los controladores.

Una vez finalizada la instalación, edite su .zshrc / .bashrc para añadir :

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

al final del archivo. Guarda el archivo y reinicia tu terminal.

Verifica que la instalación funcionó

Ahora nvcc –version debería mostrar algo similar a :

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

Instalando cuDNN

Para descargar e instalar los controladores correctos

Descarga los instaladores .deb desde el sitio web:

  • Ve a Nvidia
  • Tendrás que iniciar sesión en tu Nvidia cuenta de desarrollador
  • Descarga la biblioteca de tiempo de ejecución y desarrollador (deb) para tu SO x86_64 en el subdirectorio “Archived releases”

Ahora, instala los dos archivos usando 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

Verifica la instalación general

Instalaremos TensorFlow en un venv y verificaremos que tenga acceso a la GPU.

crea un venv

útil solo si aún no tienes una herramienta virtualenv

sudo apt-get install python3-venv

asegúrate de no tener otro venv con el mismo nombre en la misma ubicación

python3 -m venv venv

instala TensorFlow en el venv

source venv/bin/activate
pip install tensorflow

asegúrate de que pueda acceder a tu GPU

Python

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

>>> # también puedes usar tf.config.list_physical_devices(‘GPU’) y ver dispositivos CPU y GPU

La última línea en Python debería devolver True.

Conclusión

¡Felicidades! Si has seguido este tutorial paso a paso y (con suerte) no has encontrado ningún otro problema aparte de los mencionados anteriormente, tu Ubuntu instalación debería estar lista para usar con los controladores adecuados de Nvidia y Cuda !

Si alguna vez te encuentras atascado porque un paso que intentaste reproducir no funcionó como esperabas (muy probablemente debido a las diferencias en el hardware), intenta buscar en AskUbuntu, el Reddit de Ubuntu, el Reddit de Nvidia, o Stack Overflow que contienen muchos conocimientos valiosos.

Acerca de

Desde fotos sencillas hasta PDF complejos o archivos manuscritos, la API de Mindee convierte los datos de tus documentos en JSON estructurado con alta fiabilidad. No se requiere entrenamiento de modelos. Compatible con cualquier alfabeto y cualquier idioma.

,
,

Punto clave

Punto clave