How to Compile and Install Linux Kernel v4.9.11 Source On a Debian / Ubuntu Linux

How do I download, compile and install the latest version of the Linux kernel on a Debian Linux v8.x or Ubuntu Linux LTS home server or my laptop? How do I build and install a custom Linux kernel on a Debian or Ubuntu Linux based system?

In order to create a custom kernel configuration file and build a custom kernel, the full Linux kernel source tree must first be downloaded and installed. The latest Linux kernel stable version is 4.9.11. In this tutorial, you will learn how to compile the Linux kernel version 4.9.11 on a Debian and Ubuntu Linux operating system and build .deb file.

Why build a custom kernel?

Compiling a custom Linux kernel has its advantages and disadvantages. To change the kernel’s behavior, one had to compile and then reboot into a new Linux. Most of the functionality in the Linux kernel contained in modules that can be dynamically loaded and unloaded from the kernel as necessary. Some benefits of a custom Linux kernel:

  1. Support a wide range of hardware including the latest hardware.
  2. Remove unwanted drivers from the kernel.
  3. Faster boot time due to small kernel size.
  4. Increased security due to additional or removed modules/drivers/features.
  5. You will learn about the kernel and advanced usage.
  6. Always run the cutting edge latest kernel.
  7. Lower memory usage.

Note: The following instructions were tested on both Debian Linux v8.x and Ubuntu Linux v14.04.4/16.04.2 LTS.


You need to install the following packages on a Debian or Ubuntu Linux to compiler the Linux kernel:

  • git : Fast, scalable, distributed revision control system. You can grab the latest source code using the git command.
  • fakeroot : Tool for simulating superuser privileges. Useful to build .deb files.
  • build-essential : Tools for building the Linux kernel such as GCC compiler and related tools on a Debian or Ubuntu Linux based system.
  • ncurses-dev : Developer’s libraries for ncurses. This is used by menuconfig while configuring the kernel options.
  • kernel-package : Utility for building Linux kernel related Debian packages.
  • xz-utils : XZ-format compression utilities to decompress the Linux kernel tar ball.
  • Disk space : 10 GB or more free disk space.
  • Time : Kernel compilation may take quite a while, depending on the power of your machine.

Install required packages

Open the terminal application. Type the following apt-get command to install the required packages for building the Linux kernel:
$ sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc
Sample outputs:

Fig.01: Install gcc and friends

Finally, install the kernel-package package too:
$ sudo apt-get install kernel-package
$ sudo apt-get --no-install-recommends install kernel-package
Sample outputs:

Fig.02: Install utility for building Linux kernel

Download the Linux kernel source code

Type the following wget command to grab both source code and pgp keys:
$ wget
$ wget wget

Sample outputs:

Fig.03: Use the wget to grab the latest source code from

Use GnuPG to verify kernel signatures:
$ unxz linux-4.9.11.tar.xz
$ gpg --verify linux-4.9.11.tar.sign

Sample outputs:

gpg: assuming signed data in `linux-4.9.11.tar'
gpg: Signature made Monday 20 February 2017 04:28:37 AM IST using RSA key ID 00411886
gpg: Can't check signature: public key not found

Get the public key from the PGP keyserver in order to verify the signature i.e. RSA key ID 00411886 (from the above outputs):
$ gpg --keyserver hkp:// --recv-keys 00411886
Sample outputs:

gpg: requesting key 00411886 from hkp server
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 00411886: public key "Linus Torvalds <[email protected]>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

Now verify again:
$ gpg --verify linux-4.9.11.tar.sign
Sample outputs:

gpg: assuming signed data in `linux-4.9.11.tar'
gpg: Signature made Monday 20 February 2017 04:28:37 AM IST using RSA key ID 00411886
gpg: Good signature from "Linus Torvalds <[email protected]>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: ABAF 11C6 5A29 70B1 30AB  E3C4 79BE 3E43 0041 1886

If you do not “BAD signature” output from “gpg --verify” command, untar the Linux kernel tar ball using the tar command enter:
$ tar xvf linux-4.9.11.tar
$ ls
$ cd linux-4.9.11/
$ ls

Sample outputs


arch COPYING Documentation fs ipc kernel Makefile README scripts tools block CREDITS drivers include Kbuild lib mm REPORTING-BUGS security usr certs crypto firmware init Kconfig MAINTAINERS net samples sound virt

Configure the Linux kernel

First, copy your existing Linux kernel config file
$ cd linux-4.9.11
$ cp -v /boot/config-$(uname -r) .config

Sample outputs:

'/boot/config-4.4.0-62-generic' -> '.config'

To configure the kernel, run:
$ make menuconfig
Sample outputs:

WARNING: It is easy to remove support for a device driver or option and end up with a broken kernel. For example, if the ext4 driver is removed from the kernel configuration file, a system may not boot. When in doubt, just leave support in the kernel.

Make sure you save the changes before exit from menuconfig.

Compile the Linux kernel

You need to clean the source tree and reset the kernel-package parameters, type:
$ make-kpkg clean
Sample outputs:

Fig.06: Run make-kpkg command

Now, you can compile the kernel, run:
$ fakeroot make-kpkg --initrd --revision=1.0.NAS kernel_image kernel_headers
To speed up the compile process pass the -j option (-j 16 means you are using all 16 cores to compile the Linux kernel):
$ fakeroot make-kpkg --initrd --revision=1.0.NAS kernel_image kernel_headers -j 16
Sample outputs:

Fig.07: Start compiling the kernel

The fakeroot runs a command called make-kpkg in an environment wherein it appears to have root privileges for file manipulation. This is useful for allowing users to create archives (tar, ar, .deb etc.) with files in them with root permissions/ownership. The make-kpkg command build Debian/Ubuntu kernel packages from Linux kernel sources and options are:

  • --initrd : Create an initrd image.
  • --revision=1.0.NAS : Set custom revision for your kernel such as 1.0.NAS or -1.0-custom-kernel etc.
  • kernel_image : This target produces a Debian package of the Linux kernel source image, and any modules configured in the kernel configuration file .config.
  • kernel_headers : This target produces a Debian package of the Linux kernel header image.

Please note that kernel compilation may take quite a while, depending on the power of your machine. On my shared 4 CORE CPU and 4GB ram it took 60 mins to build the Linux kernel. In the end you should see something as follows on screen:

test ! -e debian/control~ || rm -f debian/control~
dpkg-gencontrol -isp -DArchitecture=amd64 -plinux-headers-4.9.11 
dpkg-gencontrol: warning: -isp is deprecated; it is without effect
create_md5sums_fn () { cd $1 ; find . -type f ! -regex './DEBIAN/.*' ! -regex './var/.*'      -printf '%P' | xargs -r0 md5sum > DEBIAN/md5sums ; if [ -z "DEBIAN/md5sums" ] ; then rm -f "DEBIAN/md5sums" ; fi ; } ; create_md5sums_fn                   /tmp/linux-4.9.11/debian/linux-headers-4.9.11
chown -R root:root                  /tmp/linux-4.9.11/debian/linux-headers-4.9.11
chmod -R og=rX                      /tmp/linux-4.9.11/debian/linux-headers-4.9.11
dpkg --build                        /tmp/linux-4.9.11/debian/linux-headers-4.9.11 ..
dpkg-deb: building package `linux-headers-4.9.11' in `../linux-headers-4.9.11_1.0.NAS_amd64.deb'.
cp -pf debian/control.dist          debian/control
make[2]: Leaving directory '/tmp/linux-4.9.11'
make[1]: Leaving directory '/tmp/linux-4.9.11'

Verify kernel deb files:

$ ls  ../*.deb
../linux-headers-4.9.11_1.0.NAS_amd64.deb  ../linux-image-4.9.11_1.0.NAS_amd64.deb 

Installing a custom kernel

Type the following dpkg command to install a custom kernel on your system:
$ cd ..
$ sudo dpkg -i linux-headers-4.9.11_1.0.NAS_amd64.deb
$ sudo dpkg -i linux-image-4.9.11_1.0.NAS_amd64.deb

Sample outputs:

Selecting previously unselected package linux-headers-4.9.11.
(Reading database ... 96175 files and directories currently installed.)
Preparing to unpack linux-headers-4.9.11_1.0.NAS_amd64.deb ...
Unpacking linux-headers-4.9.11 (1.0.NAS) ...
Setting up linux-headers-4.9.11 (1.0.NAS) ...
Examining /etc/kernel/header_postinst.d.
Selecting previously unselected package linux-image-4.9.11.
(Reading database ... 110487 files and directories currently installed.)
Preparing to unpack linux-image-4.9.11_1.0.NAS_amd64.deb ...
Unpacking linux-image-4.9.11 (1.0.NAS) ...
Setting up linux-image-4.9.11 (1.0.NAS) ...
 Hmm. There is a symbolic link /lib/modules/4.9.11/build
 However, I can not read it: No such file or directory
 Therefore, I am deleting /lib/modules/4.9.11/build
 Hmm. The package shipped with a symbolic link /lib/modules/4.9.11/source
 However, I can not read the target: No such file or directory
 Therefore, I am deleting /lib/modules/4.9.11/source
Running depmod.
Examining /etc/kernel/postinst.d.
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.9.11 /boot/vmlinuz-4.9.11
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.9.11 /boot/vmlinuz-4.9.11
update-initramfs: Generating /boot/initrd.img-4.9.11
W: mdadm: /etc/mdadm/mdadm.conf defines no arrays.
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 4.9.11 /boot/vmlinuz-4.9.11
run-parts: executing /etc/kernel/postinst.d/update-notifier 4.9.11 /boot/vmlinuz-4.9.11
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 4.9.11 /boot/vmlinuz-4.9.11
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.9.11
Found initrd image: /boot/initrd.img-4.9.11
Found linux image: /boot/vmlinuz-4.4.0-62-generic
Found initrd image: /boot/initrd.img-4.4.0-62-generic
Found linux image: /boot/vmlinuz-4.4.0-21-generic
Found initrd image: /boot/initrd.img-4.4.0-21-generic

Reboot the box/server/laptop

Type the following command:
$ sudo reboot
$ sudo shutdown -r now

Verify that everything is working

Type the following command to verify your new kernel and everything is working fine:
$ uname -a
$ uname -r
$ uname -mrs
$ dmesg | more
$ dmesg | egrep -i --color 'error|critical|failed'

Sample outputs:

Linux ubuntu-box-1 4.9.11 #1 SMP Mon Feb 20 21:10:55 IST 2017 x86_64 x86_64 x86_64 GNU/Linux

And, there you have it, the Linux kernel version 4.9.11 installed and working correctly.

See also
This entry is 1 of 2 in the Installing Mainline Linux Kernel on a Ubuntu/Debian series. Keep reading the rest of the series:

  1. How to Compile and Install Linux Kernel From Source Code
  2. How to install mainline Linux kernel on Ubuntu Linux

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.