How to Compile Your Own Linux Kernel

Tips on How to Compile Your Own Linux Kernel

In short, compiling the kernel means converting its code into something that will actually run on your computer. Along with installing Gentoo, compiling your own Linux kernel is one of those things that’s considered very geeky and technical. And here are some tips on how to compile your own Linux Kernel. Check out, below.

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

Obtaining Kernel Sources

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/

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.

Alternative Method

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:

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.