How to Compile your own Kernel in Ubuntu / Debian / Mint

By | 2011/07/15

While most persons typically use the Ubuntu kernel-ppa for obtaining alternate or new kernels, you can also roll your own for a specialized VM, an embedded system, or just to geek out.

This method is a Debian sort of method in that at the very end you are left with .deb packages you can easily install or uninstall. Installing the created deb packages automatically updates your boot loader the same as installing a new kernel from your distribution.

Note: I originally posted this to various Ubuntu wiki pages, if steps look familiar!


1. Install the development tools needed if you have not already:

$ sudo apt-get install build-essential ncurses-base ncurses-dev fakeroot kernel-package

2. Fetch a kernel from http://www.kernel.org, full source.

3. Unpack the kernel sources:

$ tar xvjf linux-[version].tar.bz2

4. Change to that new directory:

$ cd linux-[version]/

5. Next, copy an existing kernel configuration:

$ cp /boot/config-YOURCURRENTKERNEL .config

6. Now type:

$ make menuconfig

…and now navigate around to enable other options that you may need. (OPTIONAL)

Press space bar once to enable support as a module . This is generally desired over built-in modules <*> in _most_ cases. If you forget something, you can always go back to this step later and recompile your kernel again.

When you are finished, keep hitting ESC to exit and say ‘Yes’ you want to save.

ncurses_menu

7. Now type

$ fakeroot make-kpkg clean

…then start compiling with:

$ fakeroot make-kpkg --jobs=4 --initrd kernel_image kernel_headers modules_image

8. When your kernel has finished compiling, just install the debs:

$ cd ..
$ sudo dpkg -i [ name of the kernel deb files such as linux-headers-3.*.deb linux-image-3.*.deb ]

And now you are done! Reboot into your new kernel.

8 thoughts on “How to Compile your own Kernel in Ubuntu / Debian / Mint

  1. Chris White

    Try building like this instead:

    fakeroot make-kpkg –jobs=4 –initrd –revision=amd64 kernel_image kernel_headers modules_image

    This example will use 4 jobs for compiling. So if your CPU is a Core i5/i7 quad-core, you’ll get 4 things compiled at once, rather than just one. It is much faster.

    Alternatively, if the above –jobs is not supported by your make-kpkg, then just do this before running the build:
    export CONCURRENCY_LEVEL=4

    Reply
  2. Chris White

    Oh and I also recommend that you actually use your distro’s current config as basis. Do so by replacing steps 4 and 5 with:

    cp /boot/config-WHATEVERYOURKERNELVERSIONIS .config
    make menuconfig (this will update the old config, adding any options it is missing)

    I also recommend that you use the menuconfig tool to go into Processor types and features > Processor family, and choose your processor. A Core i5/i7 user would choose “Core 2/newer Xeon”. Then save the configuration.

    Reply
  3. Chris White

    Also, you’re using –revision incorrectly. It’s not for the architecture. The architecture gets appended automatically.

    Here’s the naming scheme:
    kernel-image-(kernel-version)(–append-to-version)_(–revision)_(architecture).deb

    revision is for the revision, and not the architecture. An example of a revision value is the current date. Here’s a good build command to use:

    fakeroot make-kpkg –jobs=4 –initrd –append-to-version=-custom –revision=20111027 kernel_image kernel_headers modules_image

    Replace 20111027 with whatever the current date is

    Reply
    1. scott Post author

      I know revision is not ‘for’ the arch and has nothing to do with architecture, but I was implying you can use it for something like a label if needed.

      Thanks,

      Reply
      1. Chris White

        Actually you can’t use it for that, at least not anymore. It must be digits, periods, underscores or dashes. Regular letters are not legal and the build will fail.

        As for specifying a label, that is what –append-to-version=-custom is for, where you’d put your identifying name to remember what it is. Prefixing it with a dash is best practice.

        Reply
  4. Pablo

    The incorrect usage of -revision leads to an abortion of the process. Perhaps it would be reasonable to correct this in this guide.

    Thank you

    Gruesse, Pablo

    Reply
  5. doc

    now for your next trick, uninstalling a bad compile using dpkg? lol

    (being serious though)

    Reply

Leave a Reply

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.