Installing NVIDIA drivers - 'sudo ubuntu-drivers autoinstall' gives an UnboundLocalError?

jp flag

The guide for a 60 minute old Ubuntu 22.04 system I am following is telling me, if I agree with the recommendation to run sudo ubuntu-drivers autoinstall. But that's giving me an error.

peter@peter-NBINF-O5-12R5N-54:~$ sudo ubuntu-drivers autoinstall
[sudo] password for peter: 
Traceback (most recent call last):
  File "/usr/bin/ubuntu-drivers", line 513, in <module>
  File "/usr/lib/python3/dist-packages/click/", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/bin/ubuntu-drivers", line 432, in autoinstall
  File "/usr/bin/ubuntu-drivers", line 187, in command_install
  File "/usr/lib/python3/dist-packages/UbuntuDrivers/", line 839, in nvidia_desktop_pre_installation_hook
    with_nvidia_kms = version >= 470
UnboundLocalError: local variable 'version' referenced before assignment

I can see the full function if I open that file with nano:

def nvidia_desktop_pre_installation_hook(to_install):
    '''Applies changes that need to happen before installing the NVIDIA drivers'''
    with_nvidia_kms = False

    # Enable KMS if nvidia >= 470
    for package_name in to_install:
        if package_name.startswith('nvidia-driver-'):
                version = int(package_name.split('-')[-1])
            except ValueError:
                with_nvidia_kms = version >= 470

    if with_nvidia_kms:

What should I do?

jp flag

The next line of the guide already basically says work around the autoinstaller:

# Use the 'recommended' driver from the output of the earlier step, running `ubuntu-drivers devices`
sudo apt install nvidia-driver-530-open

So this is basically PEBCAK (though I don't see why the autoinstaller script can't in future make a better guess about NVIDIA KMS, whatever that is, or parse the nvidia-driver strings with a -server or -open suffix correctly, perhaps by getting the [2]-th element of the split string instead of the last element, perhaps it will in future).

For additional context, my earlier step:

peter@peter-NBINF-O5-12R5N-54:~$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd000025A2sv00001D05sd00001178bc03sc00i00
vendor   : NVIDIA Corporation
model    : GA107M [GeForce RTX 3050 Mobile]
driver   : nvidia-driver-515-open - distro non-free
driver   : nvidia-driver-525 - distro non-free
driver   : nvidia-driver-525-server - distro non-free
driver   : nvidia-driver-515 - distro non-free
driver   : nvidia-driver-510 - distro non-free
driver   : nvidia-driver-530-open - distro non-free recommended
driver   : nvidia-driver-515-server - distro non-free
driver   : nvidia-driver-530 - distro non-free
driver   : nvidia-driver-525-open - distro non-free
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-470 - distro non-free
driver   : xserver-xorg-video-nouveau - distro free builtin

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.