Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Anakiev2/8d62e261c66554d3012bc7ff855a22a7 to your computer and use it in GitHub Desktop.
Save Anakiev2/8d62e261c66554d3012bc7ff855a22a7 to your computer and use it in GitHub Desktop.
How to install the Nvidia legacy driver (340xx or 390xx) on Debian 13 Trixie

How to install the Nvidia legacy driver (340xx or 390xx) on Debian 13 Trixie

This guide will show you how to compile and install the legacy Nvidia driver on Debian. The steps bellow are compatible with both nvidia-legacy-340xx-driver and nvidia-legacy-390xx-driver and can be installed on Debian 13 (Trixie) as well as Debian 12 (Bookworm) or Debian 11 (Bullseye) in both 64-bit and 32-bit version. Execute the lines one by one and wait for every line to finish before proceeding to next. Only the lines starting with sudo should be executed as root. The lines with sudo pbuilder will take a lot of time.

To install the nvidia-legacy-390xx-driver version replace 340xx with 390xx everywhere bellow.
To install both 64-bit and 32-bit libraries for wine/steam you should run Step 2. twice.

Step 1. Prepare:

Update, upgrade and install the required packages.
If you use 32-bit system do not install linux-headers-amd64.
If you plan on using both 64-bit and 32-bit libraries check if your mirror list support both amd64 and i386. If not select one which does before you install pbuilder.

sudo apt update
sudo apt upgrade
sudo apt install pbuilder linux-headers-amd64

Edit your /etc/apt/sources.list by adding contrib to the end of every line which starts with deb (if it's not already there).
Add this line too.

deb-src http://httpredir.debian.org/debian unstable main non-free contrib

Download the source code from Debian Sid.

sudo apt update
mkdir "$HOME/nvidia-340xx" "$HOME/nvidia-settings"
cd "$HOME/nvidia-340xx"
apt source --download-only nvidia-legacy-340xx-driver
cd "$HOME/nvidia-settings"
apt source --download-only nvidia-settings-legacy-340xx

Edit /etc/apt/sources.list and remove this line.

deb-src http://httpredir.debian.org/debian unstable main non-free contrib

Step 2. Compile:

To compile the 64-bit version replace ???? with amd64.
To compile the 32-bit version replace ???? with i386.
In case you want both amd64 and i386 libraries on a 64-bit system run Step 2. twice.

sudo apt update
sudo pbuilder create --distribution trixie --architecture ????
cd "$HOME/nvidia-340xx"
sudo pbuilder build nvidia-graphics-drivers-legacy-340xx_340.108-25.dsc
cd "$HOME/nvidia-settings"
sudo pbuilder build nvidia-settings-legacy-340xx_340.108-7.dsc

Optional: If you no longer need the files created during this process.

sudo rm /var/cache/pbuilder/aptcache/*
sudo rm /var/cache/pbuilder/base.tgz
rm -r "$HOME/nvidia-340xx" "$HOME/nvidia-settings"

Step 3. Install:

Create a local repository for automated install with apt.
The -m flag is only required if you want both amd64 and i386 versions.

cd /var/cache/pbuilder/result
sudo sh -c 'dpkg-scanpackages -m . > Packages'

Edit your /etc/apt/sources.list once more to add this line.

deb [trusted=yes] file:/var/cache/pbuilder/result ./

Optional: Run the next step only if you are on 64-bit system and want both 64-bit and 32-bit libraries.

sudo dpkg --add-architecture i386

Before you proceed make sure that you have matching linux-headers for every single linux-image version on your system.

sudo apt update
sudo apt install nvidia-legacy-340xx-driver nvidia-settings-legacy-340xx

Optional: I recommend to create /etc/X11/xorg.conf file.

Section "Device"
    Identifier     "Device0"
    Driver         "nvidia"
    VendorName     "NVIDIA Corporation"
EndSection

Section "ServerFlags"
    Option         "IgnoreABI" "1"
EndSection

FAQ and Troubleshooting:

-> Was this tested?
Yes, it was tested on Debian 13 and Debian 12 with lxde and bspwm.
But I won't be surprised if modern desktop environments like GNOME or KDE Plasma don't work.

-> Does suspend to RAM work?
Yes, it does.

-> Which display manager works?
I've tested only lxdm and lightdm but should work with others as well.

-> Why should I even bother with this when I can just install from Debian Sid?
The drivers in Sid are compiled with newer version libraries which are not compatible with your current system. Installing them may look fine at first glance but will also install new incompatible packages and will overwrite some of your existing packages with ones from Sid. This may result in insatiability or even in unrecoverable system.

-> Is Wayland supported?
No.

-> My mouse and keyboard work in tty but not in Xorg.
Install xserver-xorg-input-all.

-> Some applications don't start. Especially the GTK4 ones.
GTK4 is not supported by this driver. But there is a workaround. Open a terminal-emulator and prepend GSK_RENDERER=cairo before the application's name, like this.

GSK_RENDERER=cairo pavucontrol
GSK_RENDERER=cairo ghb

Do not put GSK_RENDERER=cairo in /etc/environment or in any other way as environment variable. Because this will force almost all GUI programs to run in Cairo fallback renderer.

-> VDPAU doesn't work in mpv.
The version of mpv in the repositories doesn't include the x11 backend.
It can be added by recompiling mpv with this rule enabled -Dgl-x11=enabled. Then start mpv like this.

mpv --gpu-context=x11 --hwdec=vdpau

-> I've followed every single step but it doesn't work.
Run these and see if you have matching linux-headers for every linux-image.

apt list --installed | grep linux-image
apt list --installed | grep linux-headers

If there are missing ones, install them and go to the next question.

-> I've installed the linux-headers (after the Nvidia driver) but doesn't work. How to fix it?
Installing the headers after the driver doesn't trigger dkms. Run this.

sudo apt reinstall nvidia-legacy-340xx-kernel-dkms

-> Still doesn't work.
There might be a problem with /etc/X11/xorg.conf. Delete this file and try without it.
If doesn't work try to delete everything in /etc/X11/xorg.conf.d/ except 20-nvidia-legacy-340xx.conf.
Another possible solution is to generate new Xorg configuration with nvidia-xconfig.

-> How to use the deb822-style format sources?
If you prefer to use this format append the @macflav snipped to /etc/apt/sources.list.d/debian.sources.
I haven't tested this method yet.

# added for building nvidia-legacy driver
# deb-src http://httpredir.debian.org/debian unstable main non-free contrib
Types: deb-src
URIs: http://httpredir.debian.org/debian/
Suites: unstable
Components: main non-free contrib
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
#
# deb [trusted=yes] file:/var/cache/pbuilder/result ./
Types: deb
URIs: file:/var/cache/pbuilder/result/
Suites: trixie
Components: main
Trusted: yes
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg

Then you have to move all amd64 packages from /var/cache/pbuilder/result to /var/cache/pbuilder/result/dists/trixie/main/binary-amd64/.
If you have any i386 they go to /var/cache/pbuilder/result/dists/trixie/main/binary-i386/.

-> Why pbuilder complains that either i386 or amd64 packages were not found even though I've updated my mirror list?
Modifying /etc/apt/sources.list after the pbuilder installation doesn't affect pbuilder. Fix your mirrors then run.

sudo apt purge pbuilder
sudo apt install pbuilder

-> I have Nvidia Optimus what should I do?
I don't have such hardware to test but this has been discussed in my previous guide.

-> In my case apt didn't automatically install the i386 packages.
Try to install them like this.
The third line is optional.

sudo dpkg --add-architecture i386
sudo apt install nvidia-legacy-340xx-driver-libs-i386
sudo apt install libnvidia-legacy-340xx-cuda1-i386

-> Should I keep the source?
Yes, you will have to recompile the driver once in a while.
Some day the Debian team will deprecate these drivers and delete the source from the servers.
Then you will have hard time finding the source elsewhere.

-> How often should I recompile the driver?
I don't know the exact answer but I have few assumptions.

Every time any of the dependencies is updated.
Every time GCC is updated.
If the driver stops working.
If dkms fails.

Before you start recompiling you can run these just in case.

sudo rm /var/cache/pbuilder/base.tgz
sudo rm /var/cache/pbuilder/result/*

-> Can I somehow make pbuilder faster?
Yes, if you use HDD and you have at least 8GB RAM you can significantly speed the process up.
If you use SSD there won't be much difference.
I can't find this method documented so I'm not sure if I can recommended it or not.

sudo mount -t tmpfs -o size=4G tmpfs /var/cache/pbuilder/build

When you finish using pbuilder run.
Everything in /var/cache/pbuilder/build will be instantly deleted after this command.

sudo umount /var/cache/pbuilder/build

-> Wine/Steam doesn't work. Failed to load libGL: libGL.so.1:... No such file or directory...
Read the very first comment in the comments section below.

-> I see RIP warnings in the kernel log.
I was able to remove them (except one), by the following...
The nvidia-340xx source doesn't include this patch, while nvidia-390xx does.

--- a/nv-drm.c
+++ b/nv-drm.c
@@ -168,6 +168,9 @@
     .poll = drm_poll,
     .read = drm_read,
     .llseek = noop_llseek,
+#if defined(FOP_UNSIGNED_OFFSET)
+    .fop_flags   = FOP_UNSIGNED_OFFSET,
+#endif
 };
 
 static struct drm_driver nv_drm_driver = {

The easiest way to install it is to apply the patch directly to /usr/src/nvidia-legacy-340xx-340.108/nv-drm.c then run.

sudo apt reinstall nvidia-legacy-340xx-kernel-dkms

Uninstalling these, since I don't use them, removed the rest RIP warnings.

libnvidia-legacy-340xx-cuda1
libnvidia-legacy-340xx-nvcuvid1
libnvidia-legacy-340xx-encode1

-> I have other video drivers should I keep them?
These are not necessary.
Purging xserver-xorg will also delete /etc/X11/xorg.conf. Recreate it if necessary.

xserver-xorg
xserver-xorg-video-all
xserver-xorg-video-amdgpu
xserver-xorg-video-ati
xserver-xorg-video-dummy
xserver-xorg-video-fbdev
xserver-xorg-video-nouveau
xserver-xorg-video-vesa
xserver-xorg-video-vmware

-> My question is not listed here.
I've made an earlier version of this guide here where a lot has been discussed. You might find something useful reading there.

Useful logs and tools for troubleshooting:

Replace lxdm with your display manager.

systemctl status lxdm.service
cat /var/log/Xorg.0.log
journalctl
dmesg

lsmod | grep nouveau
lsmod | grep nvidia
modprobe -r nouveau
modprobe nvidia
nvidia-settings
nvidia-xconfig
nvidia-smi
vdpauinfo
glxgears
glxinfo
lspci
@djordjevics1998
Copy link

djordjevics1998 commented Sep 12, 2025

Thank you for the guide! I succeeded in using it for my old optimus (GT540m) laptop. I had to tweak around X11 conf, i could manage to make dGPU the primary GPU, however, that would turn off my laptop's display. I succeeded in enabling it by pasting the conf from NVIDIA Optimus - Debian Wiki and adding one additional entry on top of it (in another file such as /etc/X11/xorg.conf.d/intel.conf ):

Section "Device"
  Identifier             "Screen0"
  Driver                 "intel"
  BusID                  "PCI:0:2:0"
EndSection

Usage of dGPU as the primary causes a phantom display to appear in settings. I fixed it simply by setting it to mirror my laptop's internal display. It seems it can be managed to disappear altogether with a correct X11 conf, however, a wrong one can freeze laptop GUI (tried several times before i solved the main issue, maybe it can be turned off successfully). Also, once when I tried to hide it, it switched it's name (VGA-0) to another one (LGD-0 or something like that).

I succeeded in playing Stronghold 2 on Steam with these settings (I had to use PROTON_USE_WINED3D=1 since it doesn't have Vulkan capabilities).

Edit: If you still cannot manage to use your GPU, ensure nvidia-drm params are set both in GRUB_CMDLINE_LINUX_DEFAULT (with update-grub2) and nvidia options conf (update-initramfs -u)

@Anakiev2
Copy link
Author

Here https://github.com/flydiscohuebr/nvidia-304 I've found something for xfce:

XFCE or XFWM4 showing black screen with cursor only:
Run the command below or disable the window composer before installing the driver

xfconf-query -c xfwm4 -p /general/vblank_mode -s xpresent

If the above command fails, run this

xfconf-query -c xfwm4 -p /general/vblank_mode -t string -s "xpresent" --create

There is more useful information there.

@xcap2000
Copy link

@macflav about using nvidia or nouveau, following what you discovered alongside @Anakiev2 I created and tested two scripts, one for disabling nvidia (you must run this first as it changes some files) when you followed this article thus use nouveau and one for enabling it back thus disabling nouveau:

enable-nouveau.sh

#!/bin/bash
mv /etc/modprobe.d/nvidia.conf /etc/modprobe.d/nvidia.conf.disable
mv /etc/modprobe.d/nvidia-options.conf /etc/modprobe.d/nvidia-options.conf.disable
mv /etc/modprobe.d/nvidia-blacklists-nouveau.conf /etc/modprobe.d/nvidia-blacklists-nouveau.conf.disable
sudo sed 's/Driver         "nvidia"/Driver         "nouveau"/g' -i /etc/X11/xorg.conf
update-alternatives --set glx /usr/lib/mesa-diverted
shutdown -r now

enable-nvidia.sh

#!/bin/bash
mv /etc/modprobe.d/nvidia.conf.disable /etc/modprobe.d/nvidia.conf
mv /etc/modprobe.d/nvidia-options.conf.disable /etc/modprobe.d/nvidia-options.conf
mv /etc/modprobe.d/nvidia-blacklists-nouveau.conf.disable /etc/modprobe.d/nvidia-blacklists-nouveau.conf
sudo sed 's/Driver         "nouveau"/Driver         "nvidia"/g' -i /etc/X11/xorg.conf
update-alternatives --set glx /usr/lib/nvidia
shutdown -r now

Make them executable

$ chmod + x enable-nouveau.sh
$ chmod + x enable-nvidia.sh

When running use ctrl+alt+f1 to go to tt1, make sure to logout any graphical environment not to loose work and run the script you want as root, example:

$ sudo bash enable-nouveau.sh

I gave detailed instructions in case someone getting started can avoid all the tests we did :-)

@macflav
Copy link

macflav commented Sep 13, 2025

@macflav about using nvidia or nouveau, following what you discovered alongside @Anakiev2 I created and tested two scripts, one for disabling nvidia (you must run this first as it changes some files) when you followed this article thus use nouveau and one for enabling it back thus disabling nouveau:

I had done something similar. Thank you for sharing your solution. I'm sure it will help someone.

On another note, I decided to go back to Bookworm for the time being. Nouveau was not working for me on Trixie (I was constantly getting drm: failed to create ce channel, -22 and g84_gr_tlb_flush timeout, leading to random freezing), and I wasn't happy that GTK4 was not working with Nvidia 340.108. Let's hope the current stable release will improve support for old hardware in the future.

@xcap2000
Copy link

@macflav you are welcome. Next version of MX Linux base on Debian 13 will probably support 340 drivers, lets see how it will work.

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