Along with installing Gentoo , compiling your own Linux kernel is one of those things that’s considered very geeky and technical. We’ve already covered a few reasons why you might want to do this. Before we walk you through the steps, here’s a brief overview on some important concepts.
What Is a Kernel?
In short, it’s a vital part of your operating system, if not the most important. An operating system is responsible for letting your programs function, by allowing them access to your hardware. However, it’s the kernel which actually carries all these jobs out.
The Linux kernel is special in that it can be tailor made to fit the hardware you own. Contrast this with the Windows NT kernel, which will be generally the same across every computer. To do this, the kernel must go through a process called compiling.
For more info, check out our introduction to kernels in Linux .
What Is Compiling?
Generally, programs are written in a human readable format. This makes it easier for people to create software. For example, the Linux kernel is written mainly in a programming language called C. However, computers can’t understand this. They only recognize a complicated language called machine code. To do so, the lines of text need to be translated using a special piece of software called a compiler.
Different compilers are used to convert different programming languages. For example, GCC is a compiler which translates C code into machine code (among others). But that’s not all they do — compilers can also optimize the translated code for specific machines. Along with this, they can also remove unwanted features from programs whilst doing so.
Usually, this process is already done for lots of software. Instead, people download the converted binary instructions, which work out of the box. For example, the web browser you’re running right now is already compiled. This is because compiling something can be a little bit complicated and time consuming. Think of it like translating a large book into a foreign language.
In short, compiling the kernel means converting its code into something that will actually run on your computer.
Obtaining Kernel Sources
You can’t make a fire without fuel. Likewise, you can’t compile programs if you don’t have the code for it in the first place. The method is generally the same for most Linux operating systems with a few exceptions. Many of them supply their own custom kernel sources. You should try and use that if you can. For example, if you’re using a Debian-based operating system such as Ubuntu, you should install the sources using these commands:
sudo apt-get update sudo apt-get install linux-source
You can also install different versions of this package (e.g. linux-source-4.8.0) if you like. You’ll find the installed files in the /usr/src/ directory, so navigate there, and view the contents:
cd /usr/src/ ls
You’ll see a file that ends with .tar.bz2. This is the source you’ll be using. Extract it with this command:
sudo tar xjvf linux-source-4.4.0.tar.bz2
Make sure you replace the file with the correct name, of course. The file will be quite big, so the process will take a little while!
If you like, you may obtain your sources from the official Linux Kernel website. I recommend this only if your Linux operating system doesn’t provide them. Select the version you want and download away. If you go through this route, you should download it to an empty folder. This way, if you want to compile more kernels in the future, you’ll have a nice folder to keep them organized in.
Having done this, extract the file with this command:
tar xJvf linux-4.9.11.tar.xz
Make sure you’re using the filename that matches the version you downloaded!
Customize the Kernel
This is the step where you can choose what parts of the kernel you want to get rid of. For example, the kernel ships with a lot of different drivers for a variety of devices. If you don’t need that support, getting rid of them can yield some speed benefits, along with a smaller kernel.
Before you proceed, you must install some programs that will let you customize and compile it.
On Debian-based systems, you need the build-essential package, among others:
sudo apt-get install build-essential gcc libncurses5-dev libssl-dev
The libncurses5-dev package will make it easier to configure the kernel, so make sure to install it. Other Linux operating systems will have a similar program, but this may be under a different name. For example, if you’re using Fedora , it’s called ncurses.
If it’s not installed already, you’ll also need the GNU Compiler Collection (GCC). This is the tool responsible for translating the raw source code into something understandable to computers. That is to say, compiling.
Having installed these programs, open up your terminal so that you’re inside the extracted Linux source folder. For example, if you used the linux-source method, you’d do this:
Kernel Configuration File
Before compiling the kernel, you need to tell GCC how you want it done. You do this using a command line tool called make. Type in this command:
sudo make localmodconfig
This creates a file called .config, which tells GCC what to compile from the kernel sources. The make localmodconfig command detects currently running kernel components, and marks them for compilation. If the kernel is inside a folder you own, you won’t need sudo for it to work.
You may come across some messages like the one below. Just press the Enter key to skip them — they’re usually just new kernel features.
However, localmodconfig is not perfect! If you’re not currently using some parts of your computer, it may not detect all the things it supports. As such, you need to enable them manually. Alternatively, you can skip this step entirely and use the next command instead. This will compile the new kernel with the same options as the one you’re currently using.
If you’d prefer that, but used the localmodconifg command before, do this:
sudo make clean
This will give you a clean build. You should also use this command if you’ve compiled a kernel with these sources before.
Inside the kernel source folder, there’s a file called .config. This is what GCC will use to choose what to compile. Instead of editing it manually (not recommended), you’ll instead use a few terminal tools. This will make selecting compile options much easier.
Having done this, fine tune it further:
sudo make nconfig
You’ll see a colorful menu pop up. This is the kernel configuration menu. To navigate around, use the arrow keys. You can press the right arrow key to expand entries with a —> sign next to them. Navigate out of these sub-menus by pressing the left arrow key.
You can toggle menu entries that have the <> or  sign next to them by pressing the space bar. This will cycle through the different menu options. If you see a * or M inside, that specific kernel component will be compiled. The only difference between them is that the M option will be loaded when it’s needed. This can be useful if you’re compiling a driver for example, that won’t be used often.
If you want to know more about what a specific switch does, press F2 over it. You’ll see a helpful description of what you’re compiling.
Once you’re all done, press the F9 button to save and exit.
Compiling & Installing the Kernel
Now that you’ve created a custom make file, you’ll need to compile the kernel. Type in this command:
sudo make -j$(nproc --all)
You will not need sudo if you downloaded the kernel sources off the web. The second part of the command helps speed up the kernel compile time, by taking advantage of all your CPU cores . You may get rid of this, or change the number to something else (e.g. -j2), if you want to use your computer for other tasks without too many hiccups. However, this will also make the compiling slower!
This process can and will take a very long time. The less you chose to compile, the shorter it will be. Even so, you’ll probably need to be patient.
Afterwards, type this command in to finish compiling the rest of the kernel:
sudo make modules_install
To actually use this kernel, you’ll need to enter this command:
sudo make install
This will automatically copy the kernel to your /boot folder and generate the appropriate files to make it work.
Switching Kernels Using GRUB
If you reboot after installing your new kernel, your system might not work! This is usually due to an improperly compiled kernel (that is, not supporting your computer well enough). Case in point, my own laptop. Just in case this happens, I recommend editing GRUB so that it can switch back to older kernels. To do this, use this command:
sudo nano /etc/default/grub
Place a # sign in front of the GRUB_HIDDEN_TIMEOUT and GRUB_HIDDEN_TIMEOUT_QUIET lines. Afterwards, save these changes by running this command:
sudo grub-mkconfig -o /boot/grub/grub.cfg
Now if you reboot your computer, you’ll be able to switch to an older kernel if your new one doesn’t work. Simply go to the Advanced options menu item and select the kernel you want to boot. If everything works out fine, congratulations! You’ll be using the kernel you compiled by yourself.
Are you interested in compiling more kernels? Other programs? Do you find it worthwhile?
Image Credits: chombosan/Shutterstock