Best way to self-build the Ubuntu kernel with a patch?

I’m trying to build the Ubuntu kernel from Launchpad with my custom patch but it takes 10 times longer than a mainline build, and features such as the snap prompting client don’t work. What are the best steps/flags to use to build the proper Ubuntu kernel?

I build a custom mainline kernel on Ubuntu with the following steps:

  1. Apply my custom patch
  2. Copy local config from /boot
  3. Use the following flags:
    ‘’’
    scripts/config --set-str CONFIG_SYSTEM_TRUSTED_KEYS “”
    scripts/config --set-str CONFIG_SYSTEM_REVOCATION_KEYS “”
    scripts/config --enable CONFIG_DEBUG_INFO_NONE
    scripts/config --disable CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT
    scripts/config --disable CONFIG_DEBUG_INFO_DWARF4
    scripts/config --disable CONFIG_DEBUG_INFO_DWARF5
    scripts/config --disable CONFIG_DEBUG_INFO_BTF
    scripts/config --disable CONFIG_DEBUG_INFO_REDUCED
    scripts/config --disable CONFIG_DEBUG_INFO_COMPRESSED_NONE
    scripts/config --disable CONFIG_DEBUG_INFO_COMPRESSED_ZLIB
    scripts/config --disable CONFIG_DEBUG_INFO_COMPRESSED_ZSTD
    scripts/config --disable CONFIG_DEBUG_INFO_SPLIT
    scripts/config --disable CONFIG_GDB_SCRIPTS
    scripts/config --disable CONFIG_MODULE_COMPRESS_NONE
    scripts/config --enable CONFIG_MODULE_COMPRESS_ZSTD
    ‘’’
  4. make olddefconfig / make -j$(nproc)

Build from the Ubuntu source with its own packaging, since the snap prompting client needs the AppArmor support that comes from Ubuntu’s kernel. Ubuntu’s guide has the full steps (How to build an Ubuntu Linux kernel - Ubuntu Kernel documentation).

Get the source with apt source linux-image-unsigned-$(uname -r), apply your patch in that tree, then change the number after the dash in the first line of debian.master/changelog to 999.

For a quicker build, leave out the extra module packages and build only the generic flavour without debug symbols:

export do_linux_main_modules_depends=false
fakeroot debian/rules clean
fakeroot debian/rules binary-headers binary-generic binary-perarch

That export drops the zfs and v4l2loopback modules, so install their dkms packages if you use them.

The .deb files land one directory above the source tree. They’re unsigned, so boot them with Secure Boot off or sign the image with an enrolled MOK first.

Drafted with AI.

Alternatively we have this project to build any Linux kernel tree (almost) as Ubuntu kernel packages:

https://github.com/ubuntu/ukpack

1 Like