Building, packaging and installing a kernel from source on Debian

These days, I'm mostly running Debian stable on my personal computers. I like how... stable it is.

Flatpak allows you to run up to date software packages on a rock-solid foundation provided by Debian.

However, one package I often find my self needing a newer version for, is the Linux kernel itself - which obviously can't be run inside a flatpak. Especially on newer hardware, a newer kernel can often give you better hardware compatibility, performance, or power management.

Debian provides some excellent documentation on how to build kernel packages.

I've replicated the steps outlined there on this page, but as easy to copy-and-paste snippets to speed things up.

# Install dependencies
sudo apt install git build-essential yacc bison flex pahole perl-base libssl-dev libelf-dev debhelper

# Clone specific Linux version (depth 1 because we don't need all of the kernel history)
# Change the kernel version (6.6 in this case) as needed
git clone -b v6.6.1 --single-branch --depth 1

cd linux
# Copy config from currently running kernel over for the new kernel build
cp /boot/config-$(uname -r) .config
# Check for new options added since the old kernel was released, and prompt the user for how to configure them
make oldconfig

# Build all the .deb packages for this kernel
make -j$(cat /proc/cpuinfo | grep -e '^processor' | wc -l) deb-pkg

# Install the generated packages (kernel and headers only)
sudo apt install ../linux-image-*[0-9]_[0-9]*_amd64.deb ../linux-headers-*_amd64.deb

And you're good to go! Reboot and it GRUB should show your new kernel as the default.