Sometimes you want to build a single module for an existing kernel but you don’t want to recompile the whole thing. Here’s a way to save some time and compile only the code you care about.

Certainly the simplest way is to build a kernel from source, which will compile all of the requested modules in the process. But what if you don’t want to build the entire kernel? Instead you’d like to just build a single module in the source tree without building the whole damn thing. Why would you do this? Perhaps your vendor did not include a needed module in their stock kernel. Or perhaps you intend to send a patch to a linux kernel driver and you would like to test it before submission. Or perhaps you have an ancient laptop that can barely handle browsing the web without needing more RAM and making you scream and throw things all while trying to not draw attention to yourself in the coffee shop.

Whatever the reason, here’s how to do it:

First, configure the kernel to build everything as a module:

make allmodconfig

There are several ways to configure the linux kernel, but if you only intend to build a single module then this is probably what you want. You may already have a .config in your source tree and so you can skip this step. If not, you certainly will after you run the above command.

Then you must prepare the build environment:

make modules_prepare

Once that command completes, you’re ready to build a module! The command is simple, you need only know the subdirectory that holds the module you want to build. For instance, say I would like to build the newly added fwserial module (TTY over FireWire). The source for the module lives in drivers/staging/fwserial. So the appropriate command to build only that module would be:

make M=drivers/staging/fwserial

You can replace my hypothetical path to fwserial with whatever you want. After compilation completes, you could find this module at drivers/staging/fwserial/fwserial.ko.