Here are the steps. This procedure is based on nixCraft's How to: Compile Linux kernel 2.6--but modernized considerably.
Download and extract the source code of the kernel you wish to build.
You can get upstream kernel source code at kernel.org. Version 3.16.1 (the latest stable kernel as of this writing) will be used here. So you may need to modify these commands if you're using a different version.
Kernel source code is currently provided in .tar.xz archives, so click the "tar.xz" link by whatever version you want:
After cd
ing to the directory where you downloaded the archive, you can extract it with tar
:
tar xf linux-3.16.1.tar.xz
Install the necessary build tools and perform kernel configuration.
To get Ubuntu's toolchain (gcc
, make
, and so forth) install the build-essential metapackage:
sudo apt-get update
sudo apt-get install build-essential
Two reasonably user-friendly ways to configure what goes into your kernel are provided by the make
targets xconfig
and menuconfig
.
xconfig
runs a graphical configuration utility, while menuconfig
is text-based (i.e., its interface appears fully within your terminal). Each requires some additional software not provided by build-essential .
To configure graphically, install libqt4-dev and pkg-config and run make xconfig
:
sudo apt-get install libqt4-dev pkg-config
make xconfig
To configure in the terminal, install libncurses5-dev
(thanks to Hannu for this info) and run make menuconfig
:
sudo apt-get install libncurses5-dev
make menuconfig
Build the configured kernel.
First run this to compile the kernel and create vmlinuz
:
make
vmlinuz
is "the kernel." Specifically, it is the kernel image that will be uncompressed and loaded into memory by GRUB or whatever other boot loader you use.
Then build the loadable kernel modules:
make modules
Install your newly built kernel.
Assuming those make
commands completed successfully, it's time to install the new kernel. First install the modules:
sudo make modules_install
Then install the kernel itself:
sudo make install
That puts vmlinuz-3.16.1
(a copy of vmlinuz
), config-3.16.1
(a text file storing kernel configuration parameters), and System.map-3.16.1
(the kernel symbol lookup table) in /boot
. For more details, see this comp.os.linux.misc post by Hadron and man installkernel
.
Final setup, so the kernel can be started and boot the system:
This section is partly based on information in Kernel/Compile.
With the kernel now where it needs to be, it needs:
Generate your initramfs with mkinitramfs
:
cd /boot
sudo mkinitramfs -ko initrd.img-3.16.1 3.16.1
When you update the configuration of the GRUB2 boot loader--which has been the default in Ubuntu since 9.10--should automatically detect the new kernel and add an option to boot from it.
sudo update-grub
Try your kernel.
Now you can reboot to test out your new kernel. You may need to hold down Shift or press Esc during boot to see the GRUB boot menu where you can select between the different kernels that are installed.
To make your kernel (or another one) the default, see How do I change the GRUB boot order?
fakeroot debian/rules ...
commands. See the answer by @Pilot6 at a linked question. Or am I missing something?