How to Fix ‘Module Build for Kernel Failed’ DKMS in Ubuntu

Running package upgrades or installing third-party hardware drivers (such as NVIDIA graphics, VirtualBox host modules, or Realtek Wi-Fi chipsets) in Ubuntu only to be stopped by the error “Error! Bad return status for module build on kernel: X.X.X-generic (x86_64). Consult /var/lib/dkms/…/build/make.log for more information” interrupts software deployment. Dynamic Kernel Module Support (DKMS) is the framework responsible for compiling out-of-tree kernel modules whenever a new Linux kernel is installed. When DKMS builds fail, hardware peripherals cease functioning and APT package upgrades freeze. This failure typically stems from missing kernel header packages, mismatched GCC compiler versions, uninstalled build-essential packages, or upstream driver code incompatible with newer Linux kernels.

Install Matching Linux Kernel Headers and Build Tools

DKMS cannot compile C source code into kernel objects without the exact header files, symbol maps, and development toolchain matching your active running kernel.

  1. Open a terminal window (press Ctrl + Alt + T).
  2. Query your active kernel version:
    uname -r
  3. Install the complete compilation suite and matching kernel headers:
    sudo apt update && sudo apt install -y build-essential linux-headers-generic linux-headers-$(uname -r) dkms
  4. If your system runs an OEM or HWE kernel, install the corresponding meta-package:
    sudo apt install -y linux-headers-generic-hwe-$(lsb_release -rs)
  5. Re-trigger the DKMS build:
    sudo dpkg --configure -a

Inspect the Detailed ‘make.log’ Build Diagnostics

The terminal error specifies an exact file path to a compilation build log. Querying this log pinpoints the specific C syntax or missing header error.

  1. View the end of the build log generated by DKMS:
    sudo tail -n 50 /var/lib/dkms/*/*/build/make.log
  2. Look for error lines flagged with error::
    • error: implicit declaration of function: Indicates the driver source code is outdated and incompatible with newer Linux kernel API changes.
    • compiler version mismatch: Indicates the system GCC version differs from the compiler used to build the running kernel.

Verify GCC Compiler Version Synchronisation

The Linux kernel strictly enforces that loadable kernel modules must be compiled with the same GCC compiler toolchain version used to compile the active kernel itself.

  1. Check which GCC version compiled your running kernel:
    cat /proc/version
  2. Check your default installed GCC compiler version:
    gcc --version
  3. If the kernel was compiled with gcc-13 while your default compiler is gcc-12, install the matching version:
    sudo apt install -y gcc-13 g++-13
  4. Set the matching compiler as default via update-alternatives or environment variable:
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 100
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 100

Remove or Upgrade Incompatible DKMS Modules

If you upgraded Ubuntu to a new major release (e.g. Ubuntu 24.04), legacy third-party drivers (such as older VirtualBox or Realtek Wi-Fi drivers) will fail to compile until upgraded to modern driver revisions.

  1. Inspect the current status of all registered DKMS modules:
    dkms status
  2. Identify any module displaying a broken status.
  3. Completely remove the failing module from the DKMS registry (replace with your specific module name and version):
    sudo dkms remove -m problematic-driver -v 1.0 --all
  4. Purge the unmaintained third-party PPA or package:
    sudo apt purge problematic-driver-dkms
  5. Install the updated driver package maintained for your current kernel series.

Rebuild All DKMS Modules Cleanly

Once headers and compiler versions are reconciled, execute an automated rebuild across all installed drivers.

  1. Reinstall all modules for the current kernel:
    sudo dkms autoinstall -k $(uname -r)
  2. Update initial RAM disk boot images:
    sudo update-initramfs -u -k all
  3. Verify that dkms status now reports installed across all modules with zero build errors.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.