contador web Saltar al contenido

How to build a Linux Kernel step by step [TUTORIAL COMPLETO]

Kernel compilation is a bit full of magic in the tech world, but it's actually not a complicated "super" process, it needs a little attention, yes, but nothing that a few attempts and some Kernel Panic won't let you do. "manger ". Today we will learn how to compile a kernel for your distribution.

How to compile Linux Kernel Let's talk a little bit about kernel compilation and if you should or need to do this kind of thing.

As I mentioned earlier, the kernel build is wrapped up in a very large song where "only the knowledgeable" can do it, in fact, it takes a bit of advanced knowledge on your hardware to optimize the kernel, but not necessarily to compile it. .

Face this tutorial in a didactic way, I believe that the biggest beneficiaries will be those who want to learn a little more about Linux and informatics, most users (common and advanced) do not really need a compiled kernel, let's say this is a " "luxury" that you can give yourself or not.

Linux distributions make a great effort to provide you with a generic kernel that can handle virtually any hardware and still extract good performance from your computer. If you know your processor, motherboard, memory, etc., and finally, if you know your hardware well, you can adjust some modules to make the kernel leaner and more optimized. In my experience the difference is not blatant but it can help. I usually use the kernel offered by the distribution I am using, I will only consider changing if something is not working properly and this can be done in several ways, compiling only one of them.

You may also want to use an older kernel on an older machine also for better hardware support, if your distro doesn't offer this kernel, compiling it may be the only solution to bring your "pet dinosaur" to life.

Compiling your Linux Kernel

Linus Torvalds released on Kernel 4.12 recently which brings several improvements and as I see it, better video drivers for AMD cards the main highlight. If you use Arch, Fedora, Manjaro, openSUSE Tumbleweed, unstable versions of Debian, among other distros that usually keep recent packages, you will probably get this kernel very soon (this has not been received), however, other distros tend to use Kernel LTS for better stability, Debian Stable, Ubuntu, Mint, among others. These keep a kernel in an "older" version and often a modified version as well, including extra drivers and other optimizations and profiles that concern specific distro.

Also read: Meet the Graphic Kernel Manager for Ubuntu

If you feel insecure doing this kind of thing, or if this is the first time you are going to do it, I recommend that you focus on four points.

1 – Read all the material before starting to do anything.

Believe me, if you don't have the patience to read this full article, you probably won't have the patience to compile the kernel. Don't be afraid to learn, this article will not run away from you, so read it more than once and use it as a guide in your process. 2 – Do the process in VirtualBox once at least so you understand how everything works.

Learn how to test before putting things into production, learn to make mistakes, and learn about your mistakes. Compiling in VirtualBox may take longer because your computer's power gets split, but it allows you to test a number of different things. Another good option is to test on a backup computer you have, so you can try optimizing the kernel for it.

3 – Don't be afraid, but be responsible. Some people think that to compile the kernel you need to be "Chuck Norris" on Linux, but the truth is that "anyone with toddy in a dark room" does that, just pay attention.

If you are doing it on a virtual or testing machine, there is nothing to worry about. In fact, even doing your work machine, knowing how to get back to "normal", doesn't have much to worry about either.

Once the kernel has been compiled, understand that everything that works and does not work on your system with regard to hardware management "your fault", security patches, updates and the like will only apply if you do. Chances are your distro will continue to provide kernel updates that it brings by default through the repository, but as far as the kernel you compile, well, it's up to you.

4 – Get your coffee. Always have a coffee!

Starting to build the Linux kernel

The first step, no doubt, is to download the kernel you want to install. Always note the version, as this information will be important in the future. In this example we will use the latest (currently) Kernel 4.12. Access the and download the latest version available: Linux Kernel Download

Click on the yellow button and download it to the directory you want, for example your folder Downloads. For organization purposes (after all you can have many folders and files there), I suggest you create a folder with any name and put the file you downloaded inside, I will use the name "kernel" in this example, as the image below:

Linux kernel compilation
See example above for "kernel" folder under "Downloads"

After that, open your terminal and now let's start the "prank". Have you ever had a cup of coffee (or tea if you are in the process of decaf)?

This process may take a while, be patient, read carefully and everything should go well.

First of all, let's install some packages that will be useful for our compilation. In the example I am using Linux Mint, but the commands will work for all Debian-based distros, including Ubuntu, remembering that you have to do the process as root or using "sudo" wherever possible. In this case, we can use sudo without problems:

Compiling Linux Kernel


sudo apt install build-essential libncurses5-dev

Packages have the same name in just about every district, so just use your package manager. Some commands that we're going to use are basic Bash stuff, like "ls" to list files and folders in directories and "clear" to clear the screen (or CTRL + L), so feel free to use them, like Now you should have your terminal full of information, just clear the screen by typing: clear.

Our next step is to extract the contents of the Kernel archive file that you downloaded to a specific folder.

Switch to download folder

Remember I had commented to put the file inside a folder called "kernel", which in turn was inside the "Downloads" folder? We'll need to go to her with the command:

cd ~ / Downloads / kernel /

Once inside it, rotate the "ls" to see if the file is there. In our example the file is called linux-4.12.tar.xz, This type of file is a compressed file like any other .rar or .zip, but it uses tar.xz compression, we will need to extract its files and we will make it through the terminal, so we can now direct it directly to the folder / usr / src / that where he must stay to continue. From now on, let's go into root mode and do it this way, so run the command:

su root

Enter your password and you should already be root if you are using Bash, which is why you have a "#" in place of the "$" on the terminal:

Compiling Kernel

Now let's extract the contents of the file and direct it to the desired directory:

Tip: use the tab to complete commands, for example, instead of typing linux-4.12.tar.xz, just type "li"and press tab. It works at various other times as well.

Compiling Kernel


tar -xvf linux-4.12.tar.xz -C / usr / src /

If you have questions about the command tar, you can check your help page by typing: tar –help. The extraction should take a few seconds, so wait for the process to finish, when this happens you should see a screen similar to this:

Compiling Kernel

Now let's see if the files actually ended up in the right place. So far you could have done everything in graphical mode, but half the grace of compiling the kernel is using commands to unzip files, right?

Kernel compilation files


cd / usr / src /


You should see a directory named "linux-4.12".

Here's a tip that might make it a little easier, what if instead of typing "linux-4.12" every time you just call it "linux" or "kernel" or "meganfox"? Let's create a symbolic link to it, so you don't have to type more than one word or waste time typing more characters:

Creating symbolic link Command:

ln -s linux-4.12 linux

Look what beauty? Now when you access the "linux" directory it will enter linux-4.12. If you give a new "ls" you will notice the existence of a directory called "linux". Let's get into it too:

Tip: O Linux "Case sentivive" means that your operating system is case sensitive, a directory called 'Linux' different from another called 'linux', which in turn is different from another called 'LInux' and so on. Pay attention to typing the commands and directories exactly as their names are.

compiling Linux kernel



And note the presence of the "linux" link.

cd linux

To move into the "linux" directory


Again to list the files contained within the folder.

Now that the actual compilation begins. The next step is one of the most important and important determinants of compiling a Linux kernel:

Kernel Configuration


make menuconfig

This command will run and load a kind of interface where you can make various adjustments, if you want, of course:

Tip: In this option, you can choose to copy the configuration file of your current kernel as well, just by confirming when the utility prompts you, however, in this article we will explore it further.

Compiling Kernel

Here, for me, the same concept of overlock is worth. You can go through testing options until you find one that is really stable and optimized for you. It's hard for me to say anything here for you to configure because I don't know exactly what hardware you have, what devices you plug into your computer, I don't know your motherboard model, etc.

It is very worthwhile to do this process several times and know your hardware to make modifications and test how it will work. Here you can enable and disable drivers for example. An example I can give that you can, in the driver session, disable support for proprietary kernel driver blobs by unchecking the option, this might please those who just like open source.

Compiling Kernel

It's up to you to explore all these options (which are many), but beware, my recommendation:

Research everything you want to change to understand what the option does and understand the consequence of your action, avoid doing that kind of stuff on unstoppable work machines, and always keep an extra kernel that you know works, like What came along with your distro, don't remove it, so you can get back to it if you have a problem. Study your hardware and see what features you can enable and disable.

Optimizing a kernel for you is stable for some time and some compilation attempts.

Tip: Read carefully the caption that appears on this screen, it tells you how you navigate the menus and how to check and uncheck the options.

Since I don't know what your hardware is and we want to continue our compilation experience, let's just use all the default options. For that, without making any changes, let's go to the option "Save"and hit the" Enter "key.

Compiling Linux Kernel

When saving a new screen appears where you can choose the name of your configuration file (the one you were editing, or not in the previous step), you can leave the default once again ".config" and press "enter" to Confirm:

Compiling Kernel

Once the setup is done, a new screen will open with a single exit option, just confirm by pressing "enter" once again.

compiling kernel

This will take you to the first screen of the setup menu, now you just exit by selecting the "Exit" option:

compiling kernel

Back to our terminal one more time, you can clear it to clean it again. What are we going to do now purely for information and practicality.

Let's change the name of the kernel a bit, no big deal, "light in the snow", just so we know this is our compiled version.

Important information you can enter here what version of the compilation are you doing, so you can try several different ways and start with the kernel you want later on GRUB.

You can use here any text edit (in text or graphic mode), I was intending to use VIM, but it doesn't come installed on Linux Mint (maybe in your distro it comes), in compensation I have pre installed nano it's the vim.tiny (leaner version of said whose), which will do, too, after all, I just want to change a line in the configuration file. If you want you can install VIM or any other.

Choose what you prefer and we will edit the file:

compiling kernel


vim.tiny / usr / src / linux / Makefile

What I want to change in this file is the description in EXTRAVERSION:

Configuring Makefile

Navigate your keyboard to there and enter the information you want, I'll put diocomp1, which for me means "Diolinux Build 1", after all, as I said, I can do several builds of the same kernel and go testing, so the next time I can put diocomp2 and so on, getting simpler to identify each one.

To save and exit in VIM you have to press the "ESC" key until the word "INSERT" goes down from there, and type:

: wq

In forgetting the colon, W serves to save your edit and Q to quit. If you want to learn more about VIM and the terminal, check out our Bash course at Diolinux EAD, it has a bonus module only over VIM.

Next step, generating the image bzImage:

generating image


make bzImage

Connects to in the uppercase "I". In this part I had this problem, as you can see, only occurred on Linux Mint / Ubuntu, Debian was good. That was the lack of a system package that you solve with:

apt install libssl-dev

This part takes my young man, so go for a walk or have your coffee, just leave the terminal working, try to close the other applications because the build consumes machine, memory and processor resources especially, and the freer it is, the faster tends to be. I suggest, enjoy and watch an episode of Bates Motel on Netflix, the series is very cool.

When you are done, you should see a screen similar to this:

Linux kernel compilation

If all went well, you should have an image inside the boot directory, which is a subfolder within your "linux" folder, to verify this run the following command:

ls / usr / src / linux / arch / x86_64 / boot /

The architecture there in the middle of the command (x86_64) depends on the type of kernel you are compiling, either 32 or 64 bits. You should see something like this:

Compiling Kernel

The next step will compile your kernel modules, which includes the drivers you have selected there in the configuration menu:

make modules


make modules

Depending on the number of modules enabled this process also it may take a lot of fucking, wait patiently. (Or go crazy, but I think it won't help anything). In my case it took just over an hour and a half, but it depends on the power of your hardware. At the end you should see a screen like this:

Compiling Kernel

good you leave a good free space also in your partition / or in / usr, depending on how you partitioned, this generic compilation generated almost 14 GB of data.

Now with the modules compiled, we will need to install them:

Make modules_install

After the process is complete, you will see a screen similar to this:

Kernel compilation

Now let's install the kernel you just compiled:

Installing the compiled kernel


make install

When you are done performing this task, you should see an image similar to this:

installing the new kernel
Some errors that appear on this screen occur due to VirtualBox.

Now we need to change directories to run one more command that will create our kernel boot image:

This command will serve to make sure the name of our kernel, as you can see in the second line of the image above, the kernel we compiled is named "4.12.0diocomp1", we will need this name in the following command, which:

This command should not give you anything at the terminal, it will just "run", so to speak.

We're nearing the end, we need to get GRUB to recognize our new kernel so that we can reboot the machine using it, so this will need to update the list of systems / kernels that are listed in the GRUB menu. .

This step may vary slightly depending on the system you are using, Debian, Ubuntu, Mint, etc. have a native system shell script that makes this update via the command:

Eventually your distro may have another method of doing this same step, so it is worth checking the system documentation if the command doesn't work.

Notice the image above, we already have the kernel and initrd image found with our compilation, this command doesn't take long and by the time we have everything ready to start using and testing our compiled kernel.

You can now restart your computer to test the new kernel you compiled yourselfIf it works I know you'll be feeling like an Elliot of life, but let's go to the ultimate test.

If your computer has only one operating system installed, it should automatically load your kernel, if it is the latest installed, if you have more than one system, you will see GRUB, where it is easy for you to identify if your new one. kernel is listed to start.

If GRUB does not appear to you, simply hold down the "Shift" key at computer startup, then select advanced mode and you will see an image similar to this, with your kernel as the option to boot.

Just hit "enter" to boot to the desired kernel.

If all went well, your computer will function normally, once on the desktop you can check if you are using the correct kernel by running the following command:

Ready my friend or my friend, you just compiled the Linux Kernel! It wasn't so hard, was it?

I just need a little attention and patience.

Restart your computer one more time and in the GRUB screen select another version of the kernel other than your compilation, giving preference to the latest one, other than yours.

Your system should start normally and is ready for removal. As we did the "on-nail" compilation, as they say, the removal will have to be too, there are some files and directories you need to delete, which are the files from your compiled kernel, just them:

Let's go to / boot to clean up the files there first, then use "ls" to list the files and directories inside this folder, so you can see the name of the kernel you compiled, it's easy to recognize because of the modification we made. in the name "l ago", as we edit the configuration file. All files have the version of our compiled kernel (4.12 in our example) and the information we put in EXTRAVERSION, in the example I put diocomp1.

To remove we will use the command '' rm ", as it is in the image above, so the command would be:

Remember to put the YOUR VERSE from the kernel, with the name you gave it.

Running the command and giving a new "ls" you will see that the files have been deleted:

removing compiled kernel

We need to remove a few more things before we can update GRUB again.

We need to change directories first:

removing old kernel


cd / lib / modules /

First we switch to the module directory once inside it by running the "ls" to see the content again. Surely you find another directory with the name of your kernel build, in the example we have "4.12.0diocomp1", we have to remove it too:


In my example it was:

rm -rf 4.12.0diocomp1 /

Note that unlike how much we delete files in the / boot directory, here we use a parameter for "rm", "-rf", it serves to recursively delete folders and files, if you only use "rm", command cannot delete the folder because it is not empty.

If you give a new "ls" after deleting your folder, you will see that it no longer exists.

Now there is only one more file to delete.

removing compiled kernel


cd / var / lib / initramfs /


cd / var / lib / initramfs-tools /

Here I had a "surprise", the last time I had compiled a kernel the directory was just called "initramfs", but I found out that it got an extra suffix "initramfs-tools", I can't tell when that changed, but Searching I found references of 2014/2015, I also can not say if this is a particularity of Linux Mint, which I am using in this tutorial, anyway found the directory and you also find it.

Inside you can give another "ls" to see there, once again you should find your compiled kernel, just remove the file as you did with the files in / boot.


In my example:

rm 4.12.0diocomp1

Then we need to update GRUB again so that it removes the entry from the compiled kernel, otherwise trying to boot the computer because you get a nice error screen.


How to update grub When upgrading GRUB, note that the compiled kernel is missing from the entries. On reboot you can even look into GRUB to check that the compiled kernel no longer exists. grub without compiled kernel

If you still have questions, when you arrive at your desktop, check again via terminal:

uname -r

As you can see, we return to the generic kernel.


This is probably one of the biggest tutorials I could ever write here on the blog and also one of the most "complex" topics covered. Most of the time I saw tutorials on the subject they were not so "step by step" and rarely taught how to remove the compiled kernel, I hope I have accomplished my goal of demystifying this issue a bit and showing that it is not so complicated as most people think.

It doesn't have to be any genius to compile a kernel, as you will see, however, the differential part may be the optimization for your hardware and in this case you will have to do a particular study of your situation and understand what you can change that. can give you some benefit.

I have written this article with a lot of dedication and care, but it is not error free, even though I have checked the whole process a few times (in more than one distro), if you find Portuguese errors, compilation errors or have suggestions, use the comments to collaborate and enhance the material, certainly will be of great help.

Share to your friends as a payment for the content and until next time! 🙂

We killed one more oil, huh?

_____________________________________________________________________________ See any errors or would you like to add any suggestions to this article? Collaborate, click here.