How do I get the kernel source code?
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?
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
5 Answers 5
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:
Which will print something like:
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:
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
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.
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.
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.
- 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.
Yes thanks. It was a stupid comment from me. I only needed to google the error message. Anyway, have an upvote for your effort.
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:
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.
This downloading the src into the CURRENT DIRECTORY on Ubuntu 20.04 LTS, but it downloaded the wrong version of the kernel?? linux-5.4.0 when uname -r = 5.8.0-55-generic
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.
1) Give up, install kernel 3.19.0-80 and use apt-get source
2) Get source directly from launchpad:
- Google site:launchpad.net «linux-image-3.19.0-58-generic»
- That should give you the Launchpad Package Page for that version.
- Scroll down, click the «Source:» link, you’re on the Package Source Page now.
- Scroll down and download .tar.gz , .diff.gz , .dsc files:
- 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?
This is the only option on this page that worked for Ubuntu 20.04 LTS to get something that claims to be a similar kernel version linux-5.8.0 to the one returned but uname -r
There are cases where it is simply more practical to download the linux kernel source and headers for the version you’re interested in directly from http://kernel.org (a pretty reliable URL/site that isn’t going away [e.g. standard stackoverflow concern about stale links isn’t not an issue])
Reason? For example, you might be interested in debugging an older/defunct no-longer-supported version of linux where package repository points to dead areas, where in some cases there are either no viable current alternatives, or the repos has are expired keys that won’t let you get the stuff, requiring unmemorable difficult to search out arcane techniques to get those repositories to work anyway.
Or maybe Internet networking might be broken on Linux machine (or VM) you’re using, but file sharing or NFS works, where the source can be downloaded onto another machine and accessed via the filesystem.
If you’re working with a relatively obsolete version of Linux, chances are you’ll have to build a number of things from source and get them the hard way.
How to Build Linux Kernel From Scratch
The Linux Kernel is the foundation of all the Linux distributions. The kernel is responsible for communication between hardware and software and the allocation of available resources.
All Linux distributions are based on a predefined kernel. But, if you want to disable certain options and drivers or try experimental patches, you need to compile your own Linux kernel.
In this step-by-step guide, you will learn how to build and compile a Linux kernel from scratch.
- A system running Linux
- Access to the terminal/command line
- A user account with sudo/root privileges
- 12GB of available space on the hard drive
Building Linux Kernel
The process of building a Linux kernel can be performed in seven easy steps. However, the procedure may require a significant amount of time to complete, depending on the system speed.
Follow the steps below to build the latest Linux kernel.
Note: If the latest kernel version on the official website does not match the one mentioned in the steps below, replace the version number in the commands with the current latest version.
Step 1: Download the Source Code
1. Visit the official kernel website and download the latest kernel version. The downloaded file contains a compressed source code.
2. Open the terminal and use the wget command to download the Linux kernel source code:
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.0.7.tar.xz
The output shows the “saved” message when the download completes.
Note: Learn what to do when you get wget: command not found error.
Step 2: Extract the Source Code
When the file is ready, run the tar command to extract the source code:
The output displays the extracted kernel source code:
Step 3: Install Required Packages
Install additional packages before building a kernel. To do so, run this command:
sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison
The command we used above installs the following packages:
Package | Package description |
git | Tracks and makes a record of all changes during development in the source code. It also allows reverting the changes. |
fakeroot | Creates the fake root environment. |
build-essential | Installs development tools such as C, C++, gcc, and g++. |
ncurses-dev | Provides API for the text-based terminals. |
xz-utils | Provides fast file compression and decompression. |
libssl-dev | Supports SSL and TSL that encrypt data and make the internet connection secure. |
bc (Basic Calculator) | Supports the interactive execution of statements. |
flex (Fast Lexical Analyzer Generator) | Generates lexical analyzers that convert characters into tokens. |
libelf-dev | Issues a shared library for managing ELF files (executable files, core dumps and object code) |
bison | Converts grammar description to a C program. |
Step 4: Configure Kernel
The Linux kernel source code comes with the default configuration. However, you can adjust it to your needs. To do so, follow the steps below:
1. Navigate to the linux-6.0.7 directory using the cd command:
2. Copy the existing configuration file using the cp command:
cp -v /boot/config-$(uname -r) .config
3. To make changes to the configuration file, run the make command:
The command launches several scripts that open the configuration menu:
4. The configuration menu includes options such as firmware, file system, network, and memory settings. Use the arrows to make a selection or choose Help to learn more about the options. When you finish making the changes, select Save, and then exit the menu.
Note: Changing settings for some options can lead to a non-functional kernel. If you are unsure what to change, leave the default settings.
Step 5: Build the Kernel
1. Start building the kernel by running the following command:
The process of building and compiling the Linux kernel takes some time to complete.
The terminal lists all Linux kernel components: memory management, hardware device drivers, filesystem drivers, network drivers, and process management.
If you are compiling the kernel on Ubuntu, you may receive the following error that interrupts the building process:
No rule to make target 'debian/canonical-certs.pem
Disable the conflicting security certificates by executing the two commands below:
scripts/config --disable SYSTEM_TRUSTED_KEYS
scripts/config --disable SYSTEM_REVOCATION_KEYS
The commands return no output. Start the building process again with make , and press Enter repeatedly to confirm the default options for the generation of new certificates.
2. Install the required modules with this command:
sudo make modules_install
3. Finally, install the kernel by typing:
The output shows done when finished:
Step 6: Update the Bootloader (Optional)
The GRUB bootloader is the first program that runs when the system powers on.
The make install command performs this process automatically, but you can also do it manually.
1. Update the initramfs to the installed kernel version:
sudo update-initramfs -c -k 6.0.7
2. Update the GRUB bootloader with this command:
The terminal prints out the process and confirmation message:
Step 7: Reboot and Verify Kernel Version
When you complete the steps above, reboot the machine.
When the system boots up, verify the kernel version using the uname command:
The terminal prints out the current Linux kernel version.
In this step-by-step guide, you learned how to build a Linux kernel from scratch and install the required packages.
If you follow the instructions carefully, the process will complete successfully on your Linux machine.
The Linux kernel has a modular design. Functionality is extendible with modules or drivers. Learn how to use the modprobe command to add or remove modules on Linux.
Goran combines his leadership skills and passion for research, writing, and technology as a Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on various projects, he has developed an extraordinary understanding of cloud and virtualization technology trends and best practices.
There are several methods to remove old or unused kernels. It’s also considered good system hygiene practice.
The Linux kernel is the underlying software architecture of each Linux distribution. It provides and defines.