50

I'm interested in compiling a new kernel under Ubuntu 12.04 x86 64 bit.

I found this wiki page which is basically a mirror for this blog and there are a lot of steps (git, etc.) that appear useless to me.

With earlier releases/distros, I used to create a .config file and modify a Makefile if I needed to, then just run make and it's done.

Is there is a simple way to do this under Ubuntu?

ish
  • 139,926
user827992
  • 2,851

4 Answers4

41

1. Use apt-get source to download the Ubuntu version of the kernel

apt-get source linux-image-$(uname -r)

gives a folder that contains, for example:

linux-3.2.0                linux_3.2.0-26.41.dsc
linux_3.2.0-26.41.diff.gz  linux_3.2.0.orig.tar.gz

The bolded diff includes all the Ubuntu/Debian customizations.

2. To build a stock kernel with your own .config, use the "old-fashioned" Debian make-kpkg method

This is the alternate old-fashioned way described in the wiki:

sudo apt-get install kernel-package

If you are compiling a kernel for the first time:

sudo apt-get build-dep linux-image-$(uname -r)

Then cd into the source directory (here, linux-3.2.0), and either run make oldconfig to create .config file with your running kernel's configuration, or copy a third-part .config to this directory.

Depending on whether you want a text or graphical config, install:

(Text)

sudo apt-get install libncurses5 libncurses5-dev

(Graphical)

sudo apt-get install qt3-dev-tools libqt3-mt-dev

And then run:

(Text)

make menuconfig

(Graphical)

make xconfig

When done, just run:

fakeroot make-kpkg -j N --initrd --append-to-version=my-very-own-kernel kernel-image kernel-headers

where N is how many jobs to run in parallel (usually the number of CPUs you have), and my-very-own-kernel is a custom string to identify this build.

When done, the kernel image and header files will be ready as debs in the parent directory; you can install them with sudo dpkg -i, which will also take care of adding GRUB entries, etc.

Lesmana
  • 18,386
ish
  • 139,926
  • why they tells you nothing about this diff, to me a diff is just a diff, it can contain everything. – user827992 Jul 14 '12 at 10:22
  • I don't know, you would have to ask them (google for Canonical Kernel Team). I just told you how to build it ;) – ish Jul 14 '12 at 10:29
  • 1
    ok, last question, how to apply this compressed diff? :D I have to go in the linux-3.2.0 directory and run patch -p1 <../patch.diff ? – user827992 Jul 14 '12 at 10:48
  • 2
    There's no need to apply the diff; apt-get source ... did that for you already! – ish Jul 14 '12 at 10:54
  • Excuse me, but I am new to kernel compilation: when you say "Then cd into the source directory (here, linux-3.2.0)", where is that directory supposed to be? Should it be `/usr/src/linux-headers-3.2.0" for this example? – Sopalajo de Arrierez Jul 13 '15 at 21:41
  • Helpful answer. Only thing I miss the make nconfig. – Ray Oct 20 '15 at 12:13
  • 1
    This doesn't seem to work anymore without calling make-kpkg with fakeroot. I get errors on "installing" the deb. Editing the answer... – tudor -Reinstate Monica- Apr 27 '16 at 03:49
  • 2
    -1 You call it "old-fashioned," I call it outdated. fakeroot debian/rules editconfigs, fakeroot debian/rules clean, fakeroot debian/rules binary is the correct, problem-free way. – Aleksandr Dubinsky Sep 28 '16 at 12:49
  • This does not work anymore in Ubuntu 18.04+. Try apt-get source linux-source instead and follow the instructions on how to clone the git repo. – rustyx Jun 08 '19 at 10:52
  • @rustyx doesn't work either, I get: You must put some 'deb-src' URIs in your sources.list – Nulik Jul 03 '21 at 14:34
23

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:

screenshot from kernel.org, showing kernel source archives available for download

After cding 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 Install 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 Install build-essential.

To configure graphically, install libqt4-dev Install libqt4-dev and pkg-config Install 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?

Novice
  • 530
  • 1
    Step 3, make menuconfig requires apt get install libncurses5-dev – Hannu Aug 19 '14 at 10:03
  • @Hannu thank you, I just completely forgot to mention that. 64-bit system users may require "ia32-libs" also for the support of 32-bit programs to run. – Novice Aug 19 '14 at 10:37
  • Could be better to find a version-independent package too... I'm not sure there is any though. – Hannu Aug 19 '14 at 10:47
  • @EliahKagan Thank you. Is it OK now or should I delete it completely? I have no issue in doing that. – Novice Aug 19 '14 at 12:05
  • @Novice You've lessened material reproduced from the source; this is helpful. But there was still copied text together with your own and it was otherwise very similar. When relying mostly on one source, it's better to make something inspired or educated by it than a version of it. You should understand why what you're saying is right, avoid some of the source's shortcomings, and make something well suited to your audience (here, Ubuntu users). I think we'll benefit from a "traditional" kernel compilation guide, so I've tried to improve this more. If you like, feel free to edit further. – Eliah Kagan Aug 19 '14 at 15:15
  • @EliahKagan Thank you very much, I'll take care from next time. – Novice Aug 20 '14 at 05:17
  • 2
    -1 This doesn't produce an Ubuntu kernel. It doesn't even produce .deb files. This is a recipe for problems. – Aleksandr Dubinsky Sep 28 '16 at 12:11
  • This is not the first time I've come back to this answer. Very useful for my ubuntu odroids, thank you for this. – Xunnamius Sep 11 '19 at 16:11
  • Sorry, thought I added this link (useful for odroids): https://wiki.odroid.com/odroid-xu4/software/building_kernel – Xunnamius Sep 12 '19 at 06:24
3

The quick instructions for building an Ubuntu kernel (as opposed to vanilla) can be found here: https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel.

I won't copy the whole wiki, but I'll list the minimal steps to compile the version of the Ubuntu kernel that you currently have installed. (To get the most recent version, clone the kernel git repository of the upcoming Ubuntu release.) Tested on Ubuntu 16.04.

# Get source code
apt-get source linux-image-$(uname -r)

# Install dependencies
sudo apt-get build-dep linux-image-$(uname -r)

# Compile
cd linux-4.4.0
fakeroot debian/rules clean
fakeroot debian/rules binary

# Install (obviously the versions will be different)
cd ..
sudo dpkg -i linux-image-4.4.0-38-generic_4.4.0-38.57_amd64.deb linux-headers-4.4.0-38_4.4.0-38.57_all.deb linux-headers-4.4.0-38-generic_4.4.0-38.57_amd64.deb
0

Ubuntu git kernel repository

Following ideas from https://help.ubuntu.com/community/Kernel/Compile and with greater clarification of why I think that config look correct at Where can I get the 11.04 kernel .config file?:

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-bionic.git linux
cd linux
git checkout Ubuntu-4.15.0-36.39
fakeroot debian/rules clean
debian/rules updateconfigs
fakeroot debian/rules build-generic
cp debian/build/build-generic/.config .
make -j `nproc`

Tested in Ubuntu 18.04.