get the kernel source..

     There are plenty of methods to obtain the Linux kernel source code, the most popular using git. Although considered the most appropriate method because kernel developers as well as many software developers use it, there is a learning curve to using it correctly and understandanding exactly what’s going on. Because many sites already document how to use git, the following are 2 alternatives of getting the Linux source on an Ubuntu machine.

The apt-get method:

     If this isn’t the simplest method, it’s pretty close! To fetch the source code for the current running kernel, open a terminal and type the following:
cd /usr/src/
sudo apt-get source linux-image-`uname -r`

     That’s it! The directory with the source code should look something simillar to:

/usr/src/linux-image-"some-version-number"/

     Above, “some-version-number” is the version of your current running kernel.

note: Different folks advocate for different locations of where the kernel source should be downloaded to. One could just as easily download it to the home directory as well, so, if not already in the home directory, the first command would have been:
cd ~/

The wget method:

     While the apt-get method is appropriate for getting the source for the current running kernel, the wget method is not much more complex, and allows us to get the very latest stable Linux kernel available. https://www.kernel.org is the most appropriate place to get it, either by simply clicking the large STABLE release download button and saving it to some location, or copying the link location and proceeding from inside the terminal.

     For the latter, simply right-click on the Latest Stable Kernel download button, select Copy Link Location, and get back into a terminal window. When it comes to inserting the link location, simply hit Ctrl+Shift+v to paste-in the link.

     At this time, the latest version is v3.17.4, but the following examples were tested on v3.7.1, so that version number will be used instead. Also, and for the sake of variety, we’ll be downloading and unpacking the source code in the user’s home directory. The main difference here is that we no longer need to prefix certain commands with sudo, since we are not writing to a directory who’s owner is root. The following commands will help us obtain the kernel source, and place it in our user’s home directoy:

cd ~/
mkdir kernels && cd kernels/
wget -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.17.1.tar.xz
tar -xJf linux-3.17.1.tar.xz

The above steps are as follows:

  • change to user’s home directory (/home/“username”/).
  • create kernels/ directory and enter it.
  • fetch the source code bundle
  • extract(x) contents of xz(J) file(f) linux-3.17.1.tar.xz

     The resulting directory is then located at:

~/kernels/linux-3.17.1/

     With wget -c, if the download was previously interrupted for some reason, invoking -c will allow us to get the remainder of the file, whereas not invoking it would simply get a new file entirely, leaving the partial download untouched.

note: we could have done the same steps above, but instead of using the ~/ directory, we could have used the /usr/src/ directory, the main difference being that we need to prefix the mkdir, wget, and tar commands with sudo and enter the administrative password when prompted. E.g.:

cd /usr/src/
sudo mkdir kernels && cd kernels/
sudo wget -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.17.1.tar.xz
sudo tar -xJf linux-3.17.1.tar.xz