When you are starting to learn Linux programming or Device driver development, then the first thing you need is the Linux Kernel which is available in a zipped archive format from kernel.org. Sometimes I just download the kernel to browse source code of some modules.
How to download the Linux kernel source?
You just go to kernel.org and download the desired kernel required to you. You can download the latest stable Linux kernel which will be highlighted in big on the website or else you can download any other earlier long term Linux kernel version.
Have a look on the below image where I have highlighted the latest stable and older long term kernel download links.
Basically this will download the kernel archive file into the local storage. Sometimes you may be working on a remote machine using a command line where you don’t have the browser access immediately. So, in that case you need to copy the linux kernel download link by right clicking on the download button on the web page. See the below image:
So, if you want to download the kernel into a remote system then use the following command in the remote machine:
$ wget <paste the download link>
The command should look something like this:
$ wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.4.tar.xz
Use the [shift + insert] key combination to paste the copied text into the console.
The download may take several minutes depending on the system’s internet connection where you are downloading.
How to Unpack the linux kernel archive:
Now that you have downloaded the linux kernel archive which is in a zip format and needs to be extracted or unpacked. Lets say if the downloaded file is at ~/Downloads/linux-4.4.tar.xz (I am taking an example of the latest 4.4 kernel). We will make a new directory (say kernel-src-4.4) inside /usr/src and move the new kernel archive file to the new location and start unpacking.
$ cd /usr/src $ mkdir kernel-src-<version> (use another name if already have a kernel version installed) # cp ~/Downloads/linux.<version>.tar.xz /usr/src/kernel-src-<version> # cd /usr/src/kernel-src-<version> # tar xfv linux.<version>.tar.xz
Mostly the above mentioned tar command will unpack the linux kernel archive file. If it did not work then try with the following command:
# tar xjvf linux.<version>.tar.bz2
After executing all the commands successfully a new directory (linux-4.4) would have already created in the /usr/src/kerenl-src-4.4 location.
If you are only planning to browse the latest source code then
How to point to the new kernel:
We have done unpacking the kernel and now we need to point the default linux kernel source to our newly copied kernel source which the other packages should be pointing or using from. I am assuming that the current kernel source code is pointed by /usr/src/linux symbolic link (or the current kernel source directory). Follow the below commands to do that:
$ cd /usr/src/kernel-src-<version> # rm /usr/src/linux (assuming that's a link to your old kernel) # ln -s /usr/src/kernel-src-<version>/linux-<version> linux
Now the /usr/src/linux symbolic link will point to /usr/src/kernel-src-<version>/linux-<version>