Easiest Way To Build Mainline Linux Kernel in Debian/Ubuntu

The .deb Packages

This guide is for Debian/Ubuntu only, and only for mainline kernels (not distribution kernels).

After following this guide, you will end up with three .deb packages:

  • linux-headers-(version)-(arch).deb This package contains all the kernel headers that can be used to compile out-of-tree modules as well as DKMS. On Ubuntu this package corresponds to these two: linux-headers-(version) and linux-headers-(version)-(flavor) (flavor is typically generic).
  • linux-image-(version)-(arch).deb This package contains the kernel image (vmlinuz), as well as all the in-tree modules compiled as modules. When this package is installed, it will also update postinstall scripts, such as refreshing DKMS, initramfs, and Grub bootloader config. On Ubuntu this package corresponds to all of these three: linux-image-(version) and linux-modules-(version) and possibly linux-modules-extra-(version).
  • linux-libc-dev-(version)-(arch).deb This package contains the development headers for user-space modules. Typically this package can be different from the running kernel version as long as the version difference is not that large.

This guide is not for cross compiling: the resulting kernel architecture will be the same as the architecture of the machine that you used to build the kernel. This guide allows the kernel to be compiled in parallel.

Required Dependencies

If you’re building in Ubuntu, the required dependencies can be installed by the following steps:

  1. apt build-dep linux-image-XXX Where the linux-image-XXX is any Linux kernel image available in your repository. For instance, the first entry of the apt list | fgrep linux-image | fgrep generic command will do.
  2. apt install coreutils bison flex libncurses-dev bc rsync kmod cpio libelf-dev libssl-dev lz4

Note that you might need to enable source repositories (deb-src) to be able to install these packages.

Building the Packages

Now let’s the fun begins! In recent mainline Linux kernel versions, it is very simple to make those aforementioned debian packages. Just follow these steps:

  1. Download and extract or clone the kernel sources from https://kernel.org (or you can clone using git via git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git).
  2. Clean the kernel source tree (e.g. make mrproper, more on this later).
  3. Configure the kernel. Copy your .config file to the source root, or make -j$(nproc) menuconfig.
  4. make -j$(nproc) deb-pkg.

And that’s it!

For cleaning the source tree, Linux has 3 levels for cleaning: clean, mrproper, and distclean. Information regarding these options are available right in the Makefile itself. This is a quote from the Makefile:

Cleaning targets:
  clean       - Remove most generated files but keep the config and
                enough build support to build external modules
  mrproper    - Remove all generated files + config + various backup files
  distclean   - mrproper + remove editor backup and patch files

Leave a Comment