Last updated on July 5, 2020 by Dan Nanni
Before installing full kernel source on your Linux system, ask yourself whether you really need the full kernel source. If you are trying to compile a kernel module or a custom driver for your kernel, you do not need the full kernel source. You only need to install matching kernel header files, and that's it.
You need the full kernel source tree only if you want to build a custom kernel after modifying the kernel code in any way and/or tweaking default kernel options.
Here is how to download and install full kernel source tree from Debian or Ubuntu repositories. While you can download the official kernel source code from https://www.kernel.org/pub/linux/kernel/, using distro's repositories allows you to download a kernel source with the maintainer's patches applied to it.
Before downloading kernel source, install
dpkg-dev, which contains a suite of development tools needed to build Debian source packages. Among other things,
dpgk-source tool which can extract a Debian source package and automatically apply patches.
$ sudo apt-get install dpkg-dev
Next, run the following command to download full kernel source.
$ apt-get source linux-image-$(uname -r)
Along with the full kernel source (
linux_X.X.XX.orig.tar.xz), any available kernel patches (
linux_X.X.X+XXX.debian.tar.xz) and source control file (
linux_XXXX.dsc) will also be downloaded and stored in the current directory. The
.dsc file instructs how the patches are applied to the kernel sources.
Upon the completion of download, the above command will automatically invoke
dpkg-source tool, which will unpack the downloaded kernel source in the current directory, and apply downloaded patches according to
The final full kernel source tree will be available in the current directory as
If you want to install full kernel source, the Debian way described above should work on Ubuntu as well.
There is another way to download full kernel source on Ubuntu. You can actually check out the kernel source tree maintained by Canonical for different Ubuntu releases.
$ sudo apt-get install git $ git clone git://kernel.ubuntu.com/ubuntu/ubuntu-$(lsb_release --codename | cut -f2).git
For example, if you are using Ubuntu 14.04, the above command will check out code from "
ubuntu-trusty" Git repository.
Once you check out the Git repository, use the following command to install necessary development packages to meet the build dependencies for the kernel source tree.
$ sudo apt-get build-dep linux-image-$(uname -r)