formats

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

Published on July 15, 2011, by + in linux, sysadmin.




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. Now time to make a default kernel configuration.

Here you can either copy an existing .config from an existing distribution kernel or use various provided defconfigs.


Issuing the command:

make xxx_defconfig

…creates a .config file as a base for your desired architecture. There are literally hundreds of defconfigs all located in linux-[version]/arch/ARCH/configs/xxx_defconfig

Some examples are below.

For a standard 32bit x86 PC, type: make i386_defconfig

For a standard 64bit x86_64 PC, type: make x86_64_defconfig

Other examples:

For Power G5 machines, type: make g5_defconfig

For Power G3 and G4 Macs, type: make pmac32_defconfig

For the PS3, type: make ps3_defconfig

There is even a wii_defconfig :)





6. Now type:

$ make menuconfig



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

(Ex: file system support, network and wireless modules, media and tv modules, any and everything you need!)

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 --initrd kernel_image kernel_headers modules_image



This will take a long time to compile, probably at least an hour even on a recent i5/i7 box.

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 Responses

  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

  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.

  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

    • scott
      Twitter:

      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,

      • 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.

  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

  5. mikecrosoft

    Thanks for this extremely helpful guide, I’ve posted your information with full credits to you at http://forums.linuxmint.com/viewtopic.php?f=90&t=97774&p=557908#p557908. Regards!.

  6. doc

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

    (being serious though)

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

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