Showing posts with label kernel. Show all posts
Showing posts with label kernel. Show all posts

How to Install a Specific Linux Kernel Version

Linux kernel is ubiquitous. Android, Ubuntu, Red Hat and more, use the Linux kernel at the heart of their product's operating systems. The birth-child of Linus Torvalds 25 years ago is now the boss of operating systems.

Many would already know how to install Linux on their system or on a VM. But this is not the same as installing a different (upgraded or degraded) version of the kernel. The latest Linux kernel is release v4.19-rc2. In this post, I'll show you how to install a different version of the Linux kernel on your host/VM.


Preparation


I have an Ubuntu 18.04 running in a VM on a Windows host. The current Linux kernel version is 4.15.0-33.

nikhilh@ubuntu:~$ uname -a
Linux ubuntu 4.15.0-33-generic #36-Ubuntu SMP Wed Aug 15 16:00:05 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

You'll need certain libraries prior to installation:

nikhilh@ubuntu:~$ sudo apt-get update
nikhilh@ubuntu:~$ sudo apt-get install -y fakeroot ncurses-dev git libelf-dev libssl-dev build-essential xz-utils bc

For a project, I needed kernel version 4.15-rc3, so I downloaded it from https://github.com/torvalds/linux/releases.

Extracting the Linux Source Code


Now that we have the tar of the v4.15-rc3 kernel, the next step is to extract it.

nikhilh@ubuntu:~/Downloads$ tar -xf linux-4.15-rc3.tar.gz
nikhilh@ubuntu:~/Downloads$ cd linux-4.15-rc3

Selecting Kernel Features


Building the menuconfig code is very important before compiling the kernel code. It allows you to choose which features of the kernel you want to include. The first step would be to copy your current config file into the kernel source code's root directory

nikhilh@ubuntu:~/Downloads/linux-4.15-rc3$ cp /boot/config-$(uname -r) .config

nikhilh@ubuntu:~/Downloads/linux-4.15-rc3$ sudo make menuconfig

Once the code compiles, you should see a GUI interface. This interface allows you to select Linux features according to your preference. Since I didn't need anything changed, I saved it as a .config file and exit from that interface.


Installing Kernel Source Code


We're finally here! But don't get too excited already; this process takes almost one and half hour to complete! If you're using a VM, it would help speed up the process if you can increase the number of logical cores that the VM can access. I used 8 cores and it still took more than an hour to complete.

nikhilh@ubuntu:~/Downloads/linux-4.15-rc3$ sudo make && sudo make modules_install && sudo make install

Updating the Bootloader


Now that you've (hopefully!) installed the kernel version that you wanted, it is time to update the root filesystem and the bootloader to make sure that your kernel version is used to boot up and not the previous version.

nikhilh@ubuntu:~/linux-4.15-rc3$ sudo update-initramfs -c -k 4.15.0-rc3
nikhilh@ubuntu:~/linux-4.15-rc3$ sudo update-grub

At this point, you can go ahead and restart the system/VM. The next time you login, you will (hopefully) have the required kernel version running. You can verify this by running uname -r.

Hey! It's still the previous version!


Looks like you fell into the same pothole that I did. But don't worry, I know how to get out. Open /boot/grub/grub.cfg. Go through the file and find a string which is something along the lines of: 

gnulinux-4.15.0-33-generic-advanced-7c910840-e34a-4b21-bada-4433bf5e9f5c

(The above is just an example. You should find something like gnulinux-4.15-rc3.)

Copy that string and open /etc/default/grub. In this file, see if you can find an option - GRUB_DEFAULT=0. Change this to GRUB_DEFAULT=<string_that_you_copied>. This ensures that the default kernel that your system is loading into is the kernel version that you installed.

Now, restart the VM. If you still cannot boot up the required kernel version, please leave a comment below and we'll figure it out.

I hope this information was useful to you! It is not difficult to install a different kernel version, albeit time consuming. If there are any queries, please let me know in the comments and I'll get back to you as soon as I can!

Popular posts