Compiling The Linux Kernel On Ubuntu & Debian


Hey buddy, today we’ll be compiling a kernel
from source. A kernel is a piece of software that directly
controls the hardware and does some basic functions like file management, memory management,
processor multitasking and much more. Every operating system has one. The Linux kernel is open-source. Meaning anyone can view, modify and publish
it’s source code. In this video, we will download the Linux
kernel’s source code and compile it on your own computer.Not everyone needs to compile
the Linux kernel from source code. If you are a computer enthusiast like me then
you can do this for learning or just for fun. If you a developer you might be searching
for a bug to help the Linux community. You may also be compiling the Linux kernel
because you have got some new hardware that isn’t supported by your distribution yet. In case our custom built kernel fails to start,
we need some time to select the stock kernel that came with the distribution in the grub
boot menu. In the description below, you can find links
for increasing timeout for Lilo, BURG and Syslinux, if you not using GRUB as your bootloader. Because Debian and Ubuntu use GRUB as it’s
default bootloader, let’s increase the timeout for it. To get started, open a terminal and run the
command: “sudo nano /etc/default/grub”.I am using nano because it is the easiest, you
use a text editor of your choice. Here as you can see, the GRUB timeout is set
to only 5 seconds.We want to increase that timeout to 10 seconds so we have plenty of
time to select a different kernel. With that, also make sure to comment out GRUB
hidden timeout and GRUB hidden timeout quiet. If they don’t already exist in the file then
you need not make any modifications other than increasing the GRUB timeout to 10. One other thing, is to make sure GRUB timeout
is not commented out. Once the necessary changes have been made,
press control plus O to save the file and then press control plus x to exit out of nano.Now
that we have updated our GRUB bootloader’s configuration, we have to re-build our initial
RAM filesystem to contain the bootloader with the newest configuration. To do that, run the command: “sudo update-grub”.This
will re-build the initial RAM filesystem so when we reboot after installing the compiled
kernel the changes are applied to GRUB. To check what version of Linux kernel is currently
running on any Linux distribution, run the command: “uname -r” to just show the kernel
version or “uname -a” to show everything about the kernel.As you can see we are running the
Linux kernel 5.3.0. The next thing would be to download the kernel
source code, so let’s close the terminal and open an internet browser.Now, head over to
kernel.org as that is a good place where we can download the kernel source from. If you intent to compile the kernel which
is under development you can also directly clone the kernel source from it’s Git repository. But obsolete and under development kernels
can be extremely unstable.For this video let’s download Linux kernel 5.4.1 which is the latest
stable at the time of making this video. To download click on the tarball link.Your
internet browser may or may not ask where to save the file. If asked save it in a directory which is empty,
I’ll save it in the downloads directory itself.So, the source code is around 104 megabytes when
compressed. As we have now downloaded the kernel source
code, let’s close the internet browser and re-open the terminal.Here navigate to the
directory where you have downloaded the kernel.As you can see, we downloaded a tar.xz file which
is highly compressed. First we need to extract the source code from
this archive. We will make a directory using the command:
“mkdir linux_kernel”.To start the extraction let’s run the command: “tar xvf linux-* -C
linux_kernel/ –strip-components=1”. This will extract the downloaded kernel archive
into the linux_kernel directory that we just made.To increase the extraction speed you
can remove the “v” in the “tar xvf” part of the command. As the extraction process has been finished
we will enter the linux_kernel directory we created.So, this is how the source code of
a kernel looks like. Running the command “du -sh” tells us that
the extracted source code is around one gigabyte.Now that we have the source code for the kernel
that we are going to compile, the next thing to do is installing dependency software packages
that are required to compile the Linux kernel on Ubuntu or Debian. Note that they might defer a little bit depending
on whether you want to compile on Ubuntu or Debian. To install those dependency software packages
on Debian, run the command: “sudo apt install build-essential rsync gcc bc bison flex libssl-dev
libncurses5-dev libelf-dev”.When asked whether to continue, press the y key on your keyboard. Build-essential is a group of software packages
that will install required tools for compilation. Rsync is a data backup and transfer utility. GCC is the GNU C compiler which actually compiles
C code. Bc is an extension to GCC. Bison is a parser. Flex is an lexical analyzer. Libssl-dev is the development package for
OpenSSL. Libncurses5-dev is the development package
for ncurses and finally libelf-dev is a library for reading elf files. If you don’t know about all these packages
and what they do, just know that they need to be installed before you can successfully
compile the Linux kernel.The last thing to do before we start compiling the Linux kernel
on Ubuntu and Debian is to create a config file that tells which modules, components
and drivers should be included in the compiled kernel. To do that there are two ways and each way
has it’s drawbacks and plus points. The first method to configure the kernel compilation
is to copy the config file that came with your distribution of Linux. To do that on an Ubuntu or Debian based system
run the command: “cp /boot/config-$(uname -r) ./.config”.This will copy the config file
of the currently running kernel that came from your distribution of Linux. The kernel from your Linux distribution comes
a lot of drivers and modules. This is to support a wide range of hardware. The advantage of coping the config file of
the kernel from your distribution is that you retain the support for that wide range
of hardware. Although it will take a long time to compile
all those drivers and modules, where you won’t require majority of them to run your computer. The second method is to generate a config
file based on the currently connected hardware of your computer. To do that run the command: “make localmodconfig”.This
will ask you questions on whether to enable a kernel module or not. Keep pressing the enter key until all prompts
have been answered. If you know what is each module is about,
you can take your time to answer these prompts.Once the process is done, we are now ready to start
the compilation process. The compilation can be done by a single core
of your processor or use multiple cores. It is recommended to stick with a single core
even though it takes longer. Sometimes the compilation process may break
when using multiple cores. To start the compilation process, run the
command: “make deb-pkg”.If you still want to compile the Linux kernel using multiple
cores, then run the command: “make -j” followed by the number of processor cores you want
to compile with “deb-pkg”. For example if you want to compile the kernel
using 4 cores, you can run the command: “make -j4 deb-pkg”. Using the second method, that is generating
the config file from your connected hardware takes much less time to compile the kernel
as only the modules and drivers that are required for your system are compiled into the kernel. Resulting in a smaller kernel which is quicker
to load, which reduces your kernel overhead. One major drawback is that the kernel will
have no idea of any new hardware that you will connect in the future. If you don’t frequently connect new hardware
like printers, cameras, and other devices. Using only the required drivers will slightly
improve the performance. The kernel should be freshly compiled in case
you get some new hardware to include the drives for that hardware. So, I don’t want to waste your time, let me
fast forward until the kernel has finished compiling.So, on AMD Ryzen 2200G with 4 gigabytes
of RAM on a VirtualBox virtual machine. The compilation took just over 30 minutes. Once the compilation process finished, you
should get Debian DEB packages in the parent directory. To install them let’s go a directory back,
and run the command: “sudo dpkg -i linux-*.deb” to install all Debian packages.And the installation
for the newly compiled kernel will start. Once the installation is done, dpkg will automatically
re-build the initial RAM filesystem. Note that this might take a minute or so. Now that we have installed a newly compiled
kernel, let’s try booting into it. To reboot the computer, run the command: “sudo
shutdown -r now”. As, I compiled and installed the new kernel
on the same computer I am recording from. Let me comeback after I reboot.[command:sudo
shutdown -r now]Now, let’s open the terminal once again and check if we are running the
newly compiled kernel.Let’s re-run the uname -r command.So there we go, we have successfully
compiled the Linux kernel from source code on Debian or Ubuntu. I was planning to make this video for a year
and had written an article on my previous website too. I will link that article in the description,
but this video contains some improvements than the article.So, that was it for this
video. Please subscribe and hit the bell icon for
more videos like this. This is Vasanth Developer signing off.

Leave a Reply

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