解決 Ubuntu 中 NVIDIA GPU 驅動問題

最近有一台機器發生了找不到 NVIDIA GPU 驅動的問題。當我使用 nvidia-smi 指令時,出現了以下錯誤訊息:

$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

通常,這個問題發生在 Kernel 更新後,但驅動程式未被更新,導致不相容的情況。解決這個問題的方法是修改 GRUB 開機選單,使用舊版 Kernel 啟動系統,然後再刪除舊的 Kernel。不過,這個方法比較複雜。在這裡,我們將使用新的 Kernel,並重新生成驅動。

修正步驟:

安裝 DKMS

通常情況下,系統已經安裝了 DKMS。

DKMS(Dynamic Kernel Module Support)全名為「動態核心模組支援」,是一套由 Dell 貢獻的 Linux 驅動程式安裝管理機制。這個機制提供了一種統一的驅動程式管理方式,讓使用者無需手動尋找驅動程式原始碼並進行編譯。相反地,DKMS 可以在載入 Linux 核心時自動檢查是否需要為該核心編譯驅動程式,並自動進行必要的編譯工作。

應該注意的是,雖然 DKMS 由 Dell 貢獻,但它已成為 Linux 社群中被廣泛使用的驅動程式管理工具,不僅限於 Dell 產品。DKMS 的使用方式使得安裝和更新驅動程式變得更加容易,特別是在不同核心版本間切換時。這使得台灣的使用者也能輕鬆地管理和更新他們的驅動程式,確保系統的穩定運行。

$ sudo apt-get install dkms

檢查目前的 NVIDIA GPU 驅動程式版本

$ ls /usr/src
linux-headers-5.15.0-76  linux-headers-5.15.0-76-generic  nvidia-535.54.03

重新生成驅動模組

$ sudo dkms install -m nvidia -v 535.54.03
Creating symlink /var/lib/dkms/nvidia/535.54.03/source -> /usr/src/nvidia-535.54.03

Kernel preparation unnecessary for this kernel. Skipping...
applying patch disable_fstack-clash-protection_fcf-protection.patch...patching file Kbuild
Hunk #1 succeeded at 123 (offset 52 lines).


Building module:
cleaning build area...
unset ARCH; [ ! -h /usr/bin/cc ] && export CC=/usr/bin/gcc; env NV_VERBOSE=1 'make' -j16 NV_EXCLUDE_BUILD_MODULES='' KERNEL_UNAME=5.15.0-76-generic IGNORE_XEN_PRESENCE=1 IGNORE_CC_MISMATCH=1 SYSSRC=/lib/modules/5.15.0-76-generic/build LD=/usr/bin/ld.bfd modules........
cleaning build area...

nvidia.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.15.0-76-generic/updates/dkms/

nvidia-modeset.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.15.0-76-generic/updates/dkms/

nvidia-drm.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.15.0-76-generic/updates/dkms/

nvidia-peermem.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.15.0-76-generic/updates/dkms/

nvidia-uvm.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.15.0-76-generic/updates/dkms/

depmod...

檢查 nvidia-smi 是否正常

$ nvidia-smi
Fri Jul 14 10:33:19 2023
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.54.03              Driver Version: 535.54.03    CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA RTX A4000               Off | 00000000:17:00.0 Off |                  Off |
| 42%   60C    P0              43W / 140W |      2MiB / 16376MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
|   1  NVIDIA RTX A4000               Off | 00000000:65:00.0 Off |                  Off |
| 40%   59C    P0              37W / 140W |      1MiB / 16376MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|  No running processes found                                                           |
+---------------------------------------------------------------------------------------+

結論

如果問題是在 apt 發生的可以參考下一篇「在 Ubuntu 發生 dpkg: error processing archive 的問題該如何處理?」的解決方法,它就可以自動修復目前發生的錯誤了。