Fast compile kernel with all modules needed for your box

tux_compWith Ubuntu, you can always get the latest packaged kernel from the Ubuntu mainline kernel-ppa and install it on your system. However, the kernel with be a debug enabled kernel with many more modules than your device actually needs and will be of a considerable size. For example the installed size of my non-debug custom compiled kernel is ~21MB but the one from mainline is >200MB. There is a way to build only the modules loaded currently in your system but it will compile ONLY the loaded kernel modules. Arch Linux has a way to compile all the kernel modules ever loaded in the system using modprobe-db but it is not available for Ubuntu. Here’s a easy way to do it on Ubuntu. I am not going into the details of what packages you need to compile the kernel on Ubuntu and stuff as you can easily look it up in Google and apt-get the necessary packages. In addition, it is advisable to run all commands as root. Steps:

  1. Reboot your system and go into BIOS/UEFI settings. Make sure all devices are enabled.
  2. Attach external devices you use frequently, like USB2, USB3, SD Card, DVD, USB dongle etc. This is to ensure the relevant modules are loaded. For example I use an encrypted volume so I need dm_crypt module loaded. If you know the module already you can directly insert it. For example:
    # insmod dm_crypt
  3. Get the latest kernel source. The easiest way is to get it from Linus’s Git repository on GitHub:
    # git clone
  4. Go into the linux directory and copy your existing running kernel configuration. It’s better if you install a mainline kernel closer to the latest version. The differences in configuration will be less and it wil help in Step 5.
    # cp -vi /boot/config-`uname -r` .config
  5. Check and mitigate the differences between your running kernel configuration and the newer one:
    # make oldconfig
  6. Update the configuration to compile only the loaded modules in the running kernel:
    # make localmodconfig
  7. Make any further configuration changes:
    # make menuconfig
    # make xconfig
  8. Compile and package. As you are compiling only a few modules compilation time will be less. Make it parallel as well.
    # apt-get install dpkg-dev
    # make -jn deb-pkg
    //where n = 1 + number of CPU cores you have
  9. The packages will be generated above the linux directory. You will find both the debug and non-debug kernels. Install the non-debug one along with headers and libc packages:
    # dpkg -i linux-headers-xxx.deb linux-image-xxx.deb linux-libc-dev_xxx.deb
  10. Reboot and select your new kernel from GRUB to boot into it!

Leave a Reply

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