Install Nvidia Driver in Ubuntu/Kali Linux(2021)

Kaushal Agarwal
4 min readJun 5, 2021

Hey guys! It’s been a long time since my last post related to installation process of nvidia-driver in Ubuntu. Those who are using Ubuntu, I will recommend you to follow this story instead, since it’s pretty simple -> https://kaushalagarwal-73962.medium.com/nvidia-driver-installation-in-ubuntu-18-04-7748385ede33

Now, people who are struggling with installation process of nvidia-driver in Kali Linux or probably any other OS, can continue reading this story. If you are not interested in having Graphical Display with Nvidia in Kali Linux then you can simply follow https://www.kali.org/docs/general-use/install-nvidia-drivers-on-kali-linux/

Follow the below steps in order to install/re-install Nvidia-Driver successfully with GUI:
Note: Please keep backup of each and every file before making changes so that you can always revert your changes if anything goes wrong. Also please do not update/reboot your system when you are modifying files. Do all these steps in one go.

Perks of having Graphical Display in Linux -> Play windows games easily with wine32 & wine64 installed in Linux. I have managed to play GTA-SanAndreas, GTA-ViceCity, Tom Clancy Blacklist, Blur :).

  1. Login to your system and open a terminal.

Execute the following below commands:
apt update && apt full-upgrade (This will update and fully upgrade your system)
apt install linux-headers-$(uname -r) (This will install linux kernel headers)
apt install nvidia-driver nvidia-xconfig (This will install latest nvidia driver with xconfig tool required in your system)
apt install gdm3 OR apt install lightdm (Please check which one is already present in your system and accordingly install. I will highly suggest you to proceed with gdm3)

2. Now create/overwrite nvidia-blacklists-nouveau.conf file in /etc/modprobe.d directory with the given text:

# You need to run "update-initramfs -u" after editing this file.# see #580894
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0
alias nouveau off
alias lbm-nouveau off

3. Now create/overwrite xorg.conf in /etc/X11 directory with the given text:

Section "ServerLayout"
Identifier "layout"
Screen 0 "nvidia"
Inactive "intel"
EndSection
Section "Device"
Identifier "nvidia"
Driver "nvidia"
BusID "PCI:1:0:0"
EndSection
Section "Screen"
Identifier "nvidia"
Device "nvidia"
Option "AllowEmptyInitialConfiguration"
EndSection
Section "Device"
Identifier "intel"
Driver "modesetting"
EndSection
Section "Screen"
Identifier "intel"
Device "intel"
EndSection

Replace the BusID in the above file with your system’s BusID. In order to get your system’s BusID, execute the below command in terminal:

nvidia-xconfig --query-gpu-info | grep 'BusID : ' | cut -d ' ' -f6

4. Now navigate to /usr/share/gdm/greeter/autostart/ directory in terminal, check which filename.desktop is present. Modify that file with given text, you should be concerned for modifying only these keys [Exec, X-GNOME-AutoRestart, X-GNOME-Autostart-Phase](This is the trickiest part, you need to use your own brain a little while modifying this file). Don’t remove anything which doesn’t concern you, add any key if not present. Giving two examples below for lightdm & gdm:

# For lightdm
[Desktop Entry]
Type=Application
Name=xbrlapi
Exec=sh -c "xrandr --setprovideroutputsource modesetting NVIDIA-0; xrandr --auto"
NoDisplay=true
X-GNOME-AutoRestart=true
X-GNOME-Autostart-Phase=DisplayServer
# For gdm
[Desktop Entry]
Type=Application
Name=Orca screen reader
Exec=sh -c "xrandr --setprovideroutputsource modesetting NVIDIA-0; xrandr --auto"
NoDisplay=true
AutostartCondition=GSettings org.gnome.desktop.a11y.applications screen-reader-enabled
X-GNOME-Autostart-Phase=DisplayServer

5. After modifying the above file, copy(do not move) the same file in /etc/xdg/autostart/ directory. Now simply logout and press ctrl+alt+f2 in order to open a terminal session without GUI.

6. Now execute systemctl stop lightdm OR systemctl stop gdm OR both depending on your situation. Your major goal is to stop all graphical drivers from bringing up GUI.

7. Execute update-initramfs -u in order to create boot image with modified files. This will ensure your system adapts the changed configuration that you have recently done.

8. Now any one gdm OR lightdm graphical driver, based on which you have changed configuration using systemctl start lightdm OR systemctl start gdm

9. If everything works out good, you should now be able to see your GUI back. In order to verify that your graphic card is working fine, you need to install nvidia-smi tool.
Execute apt install nvidia-smi and then you can execute this nvidia-smi. You will notice some amount of GPU being used. Reboot & Enjoy.

nvidia-smi(85MiB being used for GUI rendering)

10. If you are stuck with black screen, then you can again press ctrl+alt+f2 and stop your lightdm/gdm display manager using systemctl command given above. Now trying executing startx in terminal session. Hopefully now you will be able to view GUI. Now the only disadvantage will be that you have to use your terminal session everytime to login and logout as well as start your GUI session. You can disable your display manager using systemctl disable lightdm/gdm if you are proceeding ahead with this approach. Reboot & Enjoy.

Last but not the lease, please use your backup files if you messed up anything in order to revert your system. Once you have replaced the modified files with back up files then you can follow steps 7 & 8 to get back your previous session.
That’s it guys! Please do leave your feedback in comments. Please do clap if you like this post. Thank you so much for giving your considerable amount of time in reading this story.

--

--