Score:37

ubuntu-drivers "UnboundLocalError: local variable 'version' referenced before assignment" when installing nvidia drivers

bl flag

I am running into trouble reinstalling my nvidia drivers after the latest kernel upgrade. Normally, ubuntu-drivers handles it, but not this time.

I've done sudo apt purge -y nvidia-*, which often fixes problems when reinstalling, but that didn't help.

When I run ubuntu-drivers install, I get this error:

Traceback (most recent call last):
  File "/usr/bin/ubuntu-drivers", line 513, in <module>
    greet()
  File "/usr/lib/python3/dist-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/bin/ubuntu-drivers", line 413, in install
    command_install(config)
  File "/usr/bin/ubuntu-drivers", line 187, in command_install
    UbuntuDrivers.detect.nvidia_desktop_pre_installation_hook(to_install)
  File "/usr/lib/python3/dist-packages/UbuntuDrivers/detect.py", line 839, in nvidia_desktop_pre_installation_hook
    with_nvidia_kms = version >= 470
UnboundLocalError: local variable 'version' referenced before assignment

Is the with_nvidia_kms message at the bottom related at all to DKMS? I had numerous problems in the past getting dkms to work in the past, before switching to ubuntu-drivers.

This was all working until a recent upgrade, which involved a kernel.

Andrew Marshall avatar
cn flag
See https://bugs.launchpad.net/ubuntu/+source/ubuntu-drivers-common/+bug/1993019
Score:68
gl flag

Had the same issue today. Fixed it by editing the /usr/lib/python3/dist-packages/UbuntuDrivers/detect.py" file and replace line 835 with this line:

version = int(package_name.split('-')[-2])

The only change I'm bringing is -2 instead of -1. Otherwise it raises a ValueError inside the try block and just doesn't give any value to the version variable.

Check out https://bugs.launchpad.net/ubuntu/+source/ubuntu-drivers-common/+bug/1993019 if that was confusing

igormcoelho avatar
cn flag
Thanks for this precise fix. Changed here from -1 to -2 and it worked (Ubuntu 22.04): `sudo ubuntu-drivers install nvidia`
hafiz031 avatar
aq flag
A safer approach may be keeping it inside another `try` block inside `except ValueError:` block.
Nathaniel Hoyt avatar
fi flag
this also worked for me with ubuntu 22.04.
Vikram Saraph avatar
np flag
+1 working for me as well as of this writing. Ubuntu 22.04.
cc flag
Or you may just avoid the nvidia drivers with names ending in "-open". Some scripts are looking for a version as the last thing in the name.
Gal Avineri avatar
ph flag
A fix has been released (as stated in the link in the answer). updating via the Software Updater GUI solved this for me.
Artem S. Zhelonkin avatar
pk flag
If version has something like `-server-open`, then we need change it to `version = int(package_name.split('-')[-3])`
mangohost

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.