How do I get the kernel source code?

This will get the source of the stock kernel:

apt-get source linux-source
  • https://help.ubuntu.com/community/Kernel/Compile/

You can check what version of the kernel is running like this:

uname -r

Which will print something like:

3.13.0-46-generic

You can find a list of current source package versions available on your system via:

apt-cache search linux-source

To get the upstream version of the kernel:

git clone git://kernel.ubuntu.com/ubuntu/ubuntu-trusty.git
  • https://wiki.ubuntu.com/KernelTeam/GitKernelBuild

In the above link, 'trusty' is the codename for the version of Ubuntu. You can find out the codename for the version of Ubuntu you have installed via:

cat /etc/lsb-release

apt-get source linux

is the easiest way. It will download the source from your repository - and it'll be the same as the version you're running (assuming you haven't already customised it).

But if you want to find where the source is maintained you can run:

apt-cache showsrc linux

Look for the 'Vcs-' attribute (Version control system). It'll usually be a git (Vcs-Git) or mercurial repository.

Note - these commands work with any package. Just substitute 'linux' with the package you're interested in. And also note that 'apt-get source' doesn't need sudo access and will dump the source in your current directory.


Just pick one from:

sudo apt install linux-source  # downloads into system directory
sudo apt source linux-source  # downloads into working directory

If you're told to give some 'sources' URLs in your sources.list, go edit your /etc/apt/sources.list file and uncomment the desired deb-src line, for example (if you're running Xenial):

deb http://fr.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://fr.archive.ubuntu.com/ubuntu/ xenial main restricted

Removing the # on this last line enables sources repositories for download, including kernel sources.

Remarks:

  • Use modern tools; avoid apt-get.
  • These commands download the source code for your current kernel (in my case 4.4.0-75-generic for example).
  • When downloaded into system directory, files are stored in /usr/src/linux-source-4.4.0/ (for example).

Tags:

Kernel