How to compile a single module in Ubuntu linux

Occasionally you may experience problems in a specific kernel module that requires a patch.  Once patched, you can follow these directions to compile only the module affected, saving a bunch of time.

Assume the following, for this example:

  • You are building a module for Ubuntu 3.2.0-21-generic kernel.
  • Your source tree with the patched module is located in /usr/src/linux-source-3.2.0
  • Your linux header source is located in /usr/src/linux-headers-3.2.0-21-generic
  • Your working folder ( where you are building the new module )   is /usr/src/mytree

In this case, your EXTRAVERSION is -21-generic ( this is the specific flavor of the 3.2.0 kernel we are building ) this is important to note, and will be used later in this example.

Now to create your working folder and change into it:

mkdir /usr/src/mytree
cd /usr/src/mytree

Now that you are in your working folder, you need to copy over your kernel configuration file and Module.symvers file.

cp /boot/config-`uname -r`  .config
cp /usr/src/linux-headers-3.2.0-21-generic/Module.symvers .

Now with those files in place, you need to change to your source folder and prep your work area by running a few make commands.  These commands make use of the EXTRAVERSION we noted above:

cd /usr/src/linux-source-3.2.0
make EXTRAVERSION=-21-generic O=/usr/src/mytree oldconfig
make EXTRAVERSION=-21-generic O=/usr/src/mytree prepare
make EXTRAVERSION=-21-generic O=/usr/src/mytree outputmakefile
make EXTRAVERSION=-21-generic O=/usr/src/mytree archprepare
make EXTRAVERSION=-21-generic O=/usr/src/mytree modules SUBDIRS=scripts

At this point you are ready to compile the module you have patched.  Suppose it is the drivers/net/wireless/rt2x00 module:

make EXTRAVERSION=-21-generic O=/usr/src/mytree modules SUBDIRS=drivers/net/wireless/rt2x00

At this point you should have a drivers/net/wireless/rt2x00 folder in your working folder ( /usr/src/mytree ) which contains the .ko module files.

Simply backup the old modules in /lib/modules/3.2.0-21-generic/kernel/drivers/net/wireless/rt2x00 , and replace them with your new ones, then reload or reboot to test the new modules!

Good Luck!

Bookmark the permalink.
There are to How to compile a single module in Ubuntu linux

  1. Thank you for this guide! I wanted to debug the i915 module on my Thinkpad X30 running Xubuntu 12.04 but couldn’t find any up to date instructions on compiling a signle kernel module. Following your guide everything worked like a charm!

    Just in case someone might also be interested in messing up with i915, here’s what I did

    sudo apt-get build-dep --no-install-recommends linux-image-$(uname -r)
    apt-get source linux-image-$(uname -r)
    mkdir ~/my_i915
    cd ~/my_i915
    cp /usr/src/linux-headers-3.2.0-24-generic/Module.symvers .
    cp /boot/config-`uname -r` .config
    cd ~/linux-3.2.0/
    make EXTRAVERSION=-24-generic O=~/my_i915 oldconfig
    make EXTRAVERSION=-24-generic O=~/my_i915 prepare
    make EXTRAVERSION=-24-generic O=~/my_i915 outputmakefile
    make EXTRAVERSION=-24-generic O=~/my_i915 archprepare
    make EXTRAVERSION=-24-generic O=~/my_i915 modules SUBDIRS=scripts
    vim drivers/gpu/drm/i915/intel_display.c
    make EXTRAVERSION=-24-generic O=~/my_i915 modules SUBDIRS=drivers/gpu/drm/i915
    sudo cp ~/my_i915/drivers/gpu/drm/i915/i915.ko /lib/modules/3.2.0-24-generic/kernel/drivers/gpu/drm/i915/
    sudo depmod -a
    sudo update-initramfs -u

    • Vladyslav,

      Thanks so much for this. I was looking for something almost exactly like this, but for the radeon driver under ubuntu 13.04. I adapted your instructions for the radeon and it worked great.

      I really appreciate that you posted this.

      Take care.

    • Thanks Vladyslav! I’m using this as a recipe for experimenting with the sata_sil driver.

  2. Pingback October 14, 2012: Compiling a single kernel module - Dogs | Dogs

  3. Vladyslav, thanks for sharing – I am also having a problem with i915 and I need to compile it. Will give a try – hope your instructions will help me also 🙂

  4. Pingback February 24, 2014: Linux: Apple Wireless A1314 Fn Key Not Registered, Looks Like Software Bug | Click & Find Answer !

  5. This actually made my day. Now I have a “working” driver for my cx231xx based usb capture-device.

    According to

    I ended up editing “drivers/media/usb/cx231xx/cx231xx-cards.c


    So in drivers/media/video/cx231xx/cx231xx-cards.c go to struct usb_device_id cx231xx_id_table[] and add these two lines e.g. at the beginning of the definition:

    {USB_DEVICE(0x1D19, 0x6109),
    .driver_info = CX231XX_BOARD_PV_XCAPTURE_USB},

  6. Pingback October 29, 2015: Unix:Linux: Apple Wireless A1314 Fn key not registered, looks like software bug – Unix Questions

  7. Pingback November 4, 2015: Ubuntu:how do I compile just wireless drivers in kernel? – Ubuntu Linux Questions

  8. Pingback April 7, 2016: Linux: Apple Wireless A1314 Fn key not registered, looks like software bug – Stars Blog

Leave a Reply

Your email address will not be published. Required fields are marked *