Pinterest Stumbleupon Whatsapp
Advertisement

Along with installing Gentoo Gentoo: A Linux Distribution Where You Compile Your Own Optimized Software Gentoo: A Linux Distribution Where You Compile Your Own Optimized Software The sheer number of different ways in which Linux can be run is astounding, as there are plenty of choices to go around. While there are plenty of distributions which rely on either the .deb... Read More , compiling your own Linux kernel is one of those things that’s considered very geeky and technical. We’ve already covered a few reasons 3 Reasons Why You Might Want to Compile Your Own Kernel [Linux] 3 Reasons Why You Might Want to Compile Your Own Kernel [Linux] Linux systems are so ridiculously flexible and configurable, it's downright crazy. A huge part of why Linux is so flexible is because of its modular structure. Every single system component and program is split up... Read More 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 The Linux Kernel: An Explanation In Layman's Terms The Linux Kernel: An Explanation In Layman's Terms There is only one de facto thing that Linux distributions have in common: the Linux kernel. But while it's often talked about, a lot of people don't really know exactly what it does. Read More 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.

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 How to Compile & Install TAR GZ & TAR BZ2 Files in Ubuntu Linux How to Compile & Install TAR GZ & TAR BZ2 Files in Ubuntu Linux Read More 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 Are You Using the Best Web Browser for Linux in 2016? Are You Using the Best Web Browser for Linux in 2016? Using the "wrong" browser can lead to a lot of unnecessary headaches, wasted productivity, and even lost data. So which browser is the best for your Linux computer? Let's find out. Read More 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!

Alternative Method

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.

Prerequisites

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 5 Reasons to Use Pure Open Source Distro, Fedora 5 Reasons to Use Pure Open Source Distro, Fedora Fedora isn't as well known as Ubuntu, and has a reputation for being hard to use. But if this is true, why do so many people continue using Fedora? Read More , 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:

cd /usr/src/linux-source-4.4.0

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.

Further Configuration

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 How to Compile & Install TAR GZ & TAR BZ2 Files in Ubuntu Linux How to Compile & Install TAR GZ & TAR BZ2 Files in Ubuntu Linux Read More 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 What Is A Processor Core? [MakeUseOf Explains] What Is A Processor Core? [MakeUseOf Explains] Every computer has a processor, whether it's a small efficiency pro or a large performance powerhouse, or else it wouldn't be able to function. Of course, the processor, also called the CPU or Central Processing... Read More . 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 How To Customize The GRUB Boot Loader Using BURG [Ubuntu] How To Customize The GRUB Boot Loader Using BURG [Ubuntu] Read More 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

Leave a Reply

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

  1. Lex Barringer
    March 13, 2017 at 7:25 pm

    To "make" or compile and link applications, libraries, binaries images (for booting), modules and other support files should "NEVER" use, "su" or "sudo". The only time you use a superuser ability is when you go to "install" the images, libraries, modules and anything that is used system wide. However, there is a push to have modules that were previously classed as "system" are now being classed as "user" modules, that load before a user session starts and won't require the use of "su" or "sudo" to protect the core of the operating system, or in this case, the Linux kernel.

    On another note; I've noticed you're not using, "make mrproper" before you make your associated menuconfig or what ever you decide to use in terms of GUI or non GUI option list. Of course, you can edit the conf file for the kernel compilation options too in vi, vim, pico or nano if you choose, even in gedit or some other larger text editing software application. To prevent screw problems from occurring when compiling your menu, it's best to use mrproper first, as per the instructions that Linus and other at the GITHUB kerel.org master head have suggested.