DKMS and Building Kernel Modules on Ubuntu
This article explains DKMS usage and manual kernel module build steps with concrete commands. DKMS automates rebuilding modules when the kernel is updated.
1 Install DKMS and kernel headers
Section titled “1 Install DKMS and kernel headers”sudo apt updatesudo apt install -y dkms build-essential linux-headers-$(uname -r)Confirm headers match uname -r.
2 Simple manual build (for testing)
Section titled “2 Simple manual build (for testing)”Create a minimal module in ~/projects/hellomodule/:
hellomodule.c:
#include <linux/module.h>#include <linux/kernel.h>static int __init hello_init(void){ printk(KERN_INFO "hello loaded\n"); return 0; }static void __exit hello_exit(void){ printk(KERN_INFO "hello unloaded\n"); }module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("GPL");Makefile:
obj-m += hellomodule.oall: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) cleanBuild and load:
makesudo insmod hellomodule.kodmesg | tailsudo rmmod hellomodule3 DKMS workflow (packaging a module)
Section titled “3 DKMS workflow (packaging a module)”Create /usr/src/yourmod-1.0/ with source and a dkms.conf describing the module. Minimal dkms.conf:
PACKAGE_NAME="yourmod"PACKAGE_VERSION="1.0"BUILT_MODULE_NAME[0]="yourmod"DEST_MODULE_LOCATION[0]="/kernel/drivers/misc"AUTOINSTALL="yes"Register and build with DKMS:
sudo dkms add -m yourmod -v 1.0sudo dkms build -m yourmod -v 1.0sudo dkms install -m yourmod -v 1.0DKMS stores builds under /var/lib/dkms/yourmod/1.0/ and will rebuild automatically on kernel updates.
To remove:
sudo dkms remove -m yourmod -v 1.0 --all4 Troubleshooting DKMS builds
Section titled “4 Troubleshooting DKMS builds”- View build logs:
/var/lib/dkms/<module>/<version>/build/make.log. - Ensure matching
linux-headers-$(uname -r)are installed. - If a DKMS build fails after a kernel update, manually run
dkms buildto see errors and fix missing symbols or API changes. - Use
dkms statusto list installed modules and their build state.