The Linux Kernel Compile (Debian/Ubuntu)

Below is an image you can visualize the kernel if you want to, or read what’s below that image to begin.

These steps are to compile the linux kernel on Debian or Ubuntu, but you can use similar methods on other linux based systems.
Firstly we must download a whole bunch of packages, so we must run the command below.
sudo apt build-dep linux
Bashthere’s another aspect to this command: linux-image-$(uname -r) so the whole command would be listed below:
sudo apt build-dep linux linux-image-$(uname -r)
BashThis downloads the “build-dep” (short for build dependencies) for linux and linux-image-$(uname -r) except I cannot get that second thing with uname in it to work. So i’ve noted that here. There isn’t anything else on this on the page that I found it on, in the Ubuntu documentation: https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel

So far I have been building this kernel on my computer without this second part of the build-dep command working. Without it working. Anyway we need to download a few more dependencies, not included in this command.
sudo apt install libncurses-dev gawk flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf llvm
Bash
one more, last one:
sudo apt install git
BashNow navigate to where you want to leave the source code for the linux kernel. Like, ~/Kernel/linux-6/ because this is the 6th version of the linux kernel we will be downloading. Once you’ve cd’d your way there, run this command, which takes a really long time, it downloads 4.5 gb of files and structure that make up the linux kernel:
sudo git clone https://github.com/torvalds/linux.git
BashThis command, it downloads 4.5 gb but it’s required to build the linux kernel currently. The kernel won’t build on just the 100mb file from www.kernel.org, at least not on my machine. With this downloaded, you’ll have the kernel source and have the whole thing, you can investigate this further by googling like “linux kernel git clone” or something. I don’t know.
Anyway the command below is alot more complicated than just some “default” configuration, but it’s called defconfig anyway.. There’s really nothing simple about the linux kernel, and this is no exception. So below takes you to the directory in which that download was stored, the root of the kernel source tree, and then configures the build.
cd linux
sudo make defconfig
BashThat doesn’t take very long. It looks through the kernel source and adds whatever is in the defconfig package to the .config file in the kernel root directory. After that, if there are no errors enter the next command:
What this command means is “debian-package” and use-j16, or, 16-threads of logic to compile the kernel with. If your computer doesn’t have 16 threads, you need to edit the command to include however many threads your computer has. So for 8-threads, you would enter: sudo make -j8 deb-pkg or sudo make -j 8 deb-pkg
A space doesn’t screw us up there. The word deb-pkg means compile the debian packages, the command outputs 3 .deb files, which we talk about below.
sudo make -j16 deb-pkg
BashSee your system monitor, under the “Resources” tab you can see how many threads your machine has. If it’s not like 8 threads or 16 or greater, then compiling the kernel could take a long time.
Previous versions of the linux kernel had an error about debian-certs.pem when compiling on an ubuntu based system well they got rid of that in this latest version, so you don’t need to worry about that.
Finally after that command finishes, it takes about 1 hour or something, on my machine with 16-threads it only took about 30 minutes. After that, outside of the root directory of the linux kernel there will be some files. Look through them to find the 3 .deb files, and run the command
sudo apt install file.deb
BashSo replace file.deb with the file you want to install. You may already be familiar with installing .deb files, I don’t know, but that’s how you install them. After a restart, that’s about it for the linux kernel, which has become kind of easy to do now, now that it doesn’t bomb your system, or fail to compile…
