Skip to content

Instantly share code, notes, and snippets.

@bitsurgeon
Last active October 12, 2024 11:24
Show Gist options
  • Save bitsurgeon/b0f4440984c9e60dcd8fe8bbc346c029 to your computer and use it in GitHub Desktop.
Save bitsurgeon/b0f4440984c9e60dcd8fe8bbc346c029 to your computer and use it in GitHub Desktop.
install Nvidia driver on Ubuntu with Secure Boot

Install Nvidia Driver on Ubuntu 18.04.3

Secure Boot

This section applies to machines with Secure Boot, such as ThinkPad.

  1. Before installation, switch to "Discrete Graphics" in BIOS, if both Intel and Nvidia graphics are present.
  2. During installation, make sure to select the "Install third-party software for graphics and Wi-Fi hardware and addition media formats" in "Updates and other software" screen.
  3. Select "Configure Secure Boot", and set password.
  4. Continue Ubuntu installation as normal.
  5. During the first reboot, "Perform MOK management" screen will showup. Select "Enroll MOK" option.
  6. Select "Continue", then, "Yes".
  7. "Enroll the key(s)?" screen will present. Enter the password from Step 3.
  8. "OK" to reboot.
  9. Once login to the Desktop, do the following to update the Nvidia driver.

Dual Boot with Windows 10

Select existing Window 10 boot partition as Ubuntu EFI target. GRUB will look after the rest.

Update to latest Nvidia Driver

## check display card and driver status
sudo lshw -c display
# or sudo lshw -c video

## check loaded display card and driver
lsmod | grep nvidia
# or nvidia-smi
# or lsmod | grep nouveau

## remove old Nvidia driver
sudo apt purge nvidia-*
# or sudo apt remove nvidia-*

## add driver repository
sudo add-apt-repository ppa:graphics-drivers/ppa

## identify suitable driver version
sudo ubuntu-drivers devices
## if ubuntu-drivers command not found
sudo apt install ubuntu-drivers-common

## install drivers from ppa database
sudo apt install nvidia-driver-430
# sudo apt install nvidia-XYXYX nvidia-settings
# v430 was the recommended and the latest by writing this gist

## this installs drivers from Nvidia
wget http://us.download.nvidia.com/tesla/418.67/NVIDIA-Linux-x86_64-418.67.run
# or wget http://us.download.nvidia.com/XFree86/Linux-x86_64/418.74/NVIDIA-Linux-x86_64-418.74.run
# supporting packages may need to be installed before running following
sudo bash ./NVIDIA-Linux-x86_64-418.67.run --dkms

## reboot
sudo reboot

## check driver details and settings
nvidia-smi or nvidia-settings

## select driver
prime-select query
sudo prime-select nvidia or sudo prime-select intel

## display usage
dpkg -L nvidia-driver-430

External Display via HDMI

If it's not working by the above steps, try the followings.

## try 1
xrandr --setprovideroutputsource modesetting NVIDIA-0 && xrandr --auto

## try 2
grep modeset /etc/modprobe.d/*
# and
grep modeset /lib/modprobe.d/*
# to find the file containing
# options nvidia-drm modeset=1
# change it to
# options nvidia-drm modeset=0
# run
sudo update-initramfs -u
reboot
# check if the external monitor works

Note: try 2 works for ThinkPad X1 Extreme.

Timezone Difference when Dual Boot

# on Linux
timedatectl set-local-rtc 1 --adjust-system-clock

Reference


The above steps also suitable for enabling CUDA support on AWS GPU instances, such as g3 and p3 EC2 instances.

For RHEL user, please check this link.

@bitsurgeon
Copy link
Author

This process requires the secure boot set to ON. You will have the opportunity to set a password for it. Then, use it to sign the drivers.

@Kayberen
Copy link

I am using dual boot my pc and ubuntu using intel graphics. If I run "sudo apt purge nvidia-*", does it remove nvidia from my main win10 os? Please help me.

@bitsurgeon
Copy link
Author

I am using dual boot my pc and ubuntu using intel graphics. If I run "sudo apt purge nvidia-*", does it remove nvidia from my main win10 os? Please help me.

apt purge should only make changes in ur Ubuntu env, nothing do to with Win10

@anandundavia
Copy link

A newbie here

I understood everthing upto point sudo apt install nvidia-driver-430

I have a question for wget http://us.download.nvidia.com/tesla/418.67/NVIDIA-Linux-x86_64-418.67.run
What is 418.67 ?
That seems like a version to me.
If so, is that version the same across all nvidia cards?
How do I figure out which version do I need ?

( At the time of writing this, the latest recommended nvidia driver from PPA is nvidia-driver-460 )

@bitsurgeon
Copy link
Author

bitsurgeon commented Feb 20, 2021

A newbie here

I understood everthing upto point sudo apt install nvidia-driver-430

I have a question for wget http://us.download.nvidia.com/tesla/418.67/NVIDIA-Linux-x86_64-418.67.run
What is 418.67 ?
That seems like a version to me.
If so, is that version the same across all nvidia cards?
How do I figure out which version do I need ?

( At the time of writing this, the latest recommended nvidia driver from PPA is nvidia-driver-460 )

If PPA is used, there is no need to install the driver from Nvidia.

To answer your question:

  • Yes, 418.67 was the latest version of the driver when this gist was originally written. You may want to get the latest if you decided to install directly from Nvidia.
  • The driver is for Tesla product line on AWS, but should be okay for normal GPGPU acceleration.
  • The 460 from PPA looks good for Ubuntu on laptops/workstaions.

Good luck.

@MatMcT
Copy link

MatMcT commented Jul 2, 2021

Can confirm this works for my Thinkpad P53 with Thinkpad Hybrid USB-C. Ubuntu 20.04 LTS.
xrandr --setprovideroutputsource modesetting NVIDIA-0 && xrandr --auto is the magic sauce that allowed my multiple screens to work after installing the DisplayLink driver.

You've resolved my many days of frustration.

Thankyou!

@bitsurgeon
Copy link
Author

Can confirm this works for my Thinkpad P53 with Thinkpad Hybrid USB-C. Ubuntu 20.04 LTS.
xrandr --setprovideroutputsource modesetting NVIDIA-0 && xrandr --auto is the magic sauce that allowed my multiple screens to work after installing the DisplayLink driver.

You've resolved my many days of frustration.

Thankyou!

Glad it helped, and thanks for confirming.
All the best!

@etapio
Copy link

etapio commented Nov 20, 2021

Bitsurgeon, you are a lifesaver!

I have spent days trying to update to latest Nvidia Driver on my Lenovo Legion 5 Pro, running dual boot Windows 11 & Ubuntu 18.04. I have tried many different approaches, most of which are variants to what you have posted here. Nevertheless, you point out what the others have missed. Your instructions here worked like magic the first time, even for installing the very latest Nvidia driver version.

Thank you!

@bitsurgeon
Copy link
Author

Glad it is helpful.

@okapetanios
Copy link

So I only found this AFTER installing Ubuntu 20.04 as a dual boot and ignoring the mok screen. Now I try to reinstall Ubuntu and it won't take me back to that step. Do I need to completely uninstall from windows?

@bitsurgeon
Copy link
Author

@okapetanios
I haven't try that. But chances are you may need to reset the secure boot settings in BIOS and restart the process. Please let us know.
Good luck.

@ubergarm
Copy link

ubergarm commented Feb 4, 2023

@okapetanios - sorry to necro this old thread, but i did the same thing and found this command to bring back the mok screen:

sudo mokutil --import /var/lib/shim-signed/mok/MOK.der
reboot
# you will see mok screen again and have a chance to enter the password this time

References

https://askubuntu.com/questions/1122855/mok-manager-nvidia-driver-issue-after-cuda-install

@EvanEzell
Copy link

Thank you so much @bitsurgeon

@sgtcoder
Copy link

Nvidia Driver isn't signing for me with secure boot enabled on Linux Kernel 6. It is working on 5 though. Anyone else having this issue?

@erichorwath
Copy link

JFYI, after nvidia driver installation, I had to manually deactivate the default driver via nouveau.blacklist=1 in GRUB_CMDLINE_LINUX_DEFAULT.
Otherwise nvidia-settings & nvidia smi didn't work and I saw during startup: "NVRM: The NVIDIA probe routine was not called for 1 device"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment