23

I am planning to write some device drivers and I need to get the Linux kernel source. My Linux kernel version is 3.2.0-23-generic-pae and I downloaded the image from this. In many of the articles I have read, it tells me that I need to have the entire kernel tree to start inserting new modules.

Is it enough if I download this image and paste it into the usr/src/ folder or do I have to do something else?

  • 1
    I usually just install the 'linux-source' package from synaptic when I'm custom compiling for one of my machines, but given you're doing development you might need the most recent version. This link might be useful to you: help.ubuntu.com/community/Kernel/Compile – Mark Paskal Jul 5 '12 at 5:50
27

This will get the source of the stock kernel:

apt-get source linux-source

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

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
  • If I use git to clone how do i specify the version. Also If i Clone it is it enough if a I copy pate it into the usr/src folder – Sab Jul 5 '12 at 7:07
  • That git repository is kept to 3.2.0 specifically; it follows what's in Ubuntu Precise (12.04). If you want a different version change ubuntu-precise.git to ubuntu-quantal.git or whatever. It should be fine to just copy into usr/src. – Bryce Jul 5 '12 at 7:11
  • Okay.Thanks- But how did you find that git link? – Sab Jul 5 '12 at 7:15
  • Found it at wiki.ubuntu.com/KernelTeam/GitKernelBuild – Bryce Jul 5 '12 at 7:30
  • And where should I be able to find the source tree when installing with... Damn, I should read better. The answer says apt-get source not apt-get install. Than the source tree will end up in your current path. – Pro Backup Aug 12 '14 at 21:37
7
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.

  • This requires the machine you are downloading on to match the target machine. Really only works if you have a network. Most of the readers landing on this page won't have a network (hence why they suddenly need the source). I need it as a tarball on a thumb drive and I don't have a working apt. – mckenzm May 16 '16 at 20:01
  • This only downloaded the linux-headers into /usr/src - not the full kernel source - on Ubuntu 16.04 – Pierz Jan 8 '18 at 15:24
3

Download source directly from Launchpad

Strangely enough everyone recommends apt-get source which doesn't work most of the time if you're looking for a specific kernel version:

Say you need kernel source for 3.19.0-58 :
apt-get source linux-image-3.19.0-58-generic will get the source for the latest kernel in the series: 3.19.0-80 in this case, which is not what you asked for.

So you have two options:

1) Give up, install kernel 3.19.0-80 and use apt-get source

2) Get source directly from launchpad:

links

  • Put them in a new directory, unpack with dpkg-source -x *.dsc

Notes:

  • There must be a better way.
  • You can get the version you want from git, but you're in for a big download. See "Obtaining the kernel sources for an Ubuntu release using git" in Ubuntu Wiki Kernel Source Code.
  • Thank you for explaining this so clearly. However, I am still unable to find the source for linux-image-4.15.0-39-generic. This process is needlessly complicated. I've tried googling for it but I keep finding packages that are a few kilobytes only. Any tips? – forgetso Nov 14 '18 at 19:00
3

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).
  • Doesn't work for me (but neither does the apt-get version). I'm running Ubuntu 16.04.4, kernel 4.4.0-98-generic. It says, "You must put some 'source' URIs in your sources.list". I have no idea how to do that. – Andrew Bainbridge Jan 9 '18 at 12:08
  • @AndrewBainbridge I added info about that. Is that OK for you now? – Tim Jan 10 '18 at 23:03
  • Yes thanks. It was a stupid comment from me. I only needed to google the error message. Anyway, have an upvote for your effort. – Andrew Bainbridge Jan 11 '18 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.