Wiki Table of Contents

Compiling and installing a new kernel

NOTE: the standard method of upgrading a kernel is spelled out in the Users Manual. This page discusses advanced procedures.

Preconfiguration
Since it’s not advisable to compile the kernel as root, you should extract the kernel source to a folder in your /home as a normal user, and then all the commands (except the installation commands) should be run as normal user.

Install programs needed
kernel-package – this program is needed for creating kernel deb packages.
build-essential – this program is needed for compiling source code into machine binaries (programs).
fakeroot – this program removes the need to become root for building a package
libncurses5-dev – this program is needed for using make menuconfig or make xconfig.
libqt4-dev – these files are needed for running make xconfig with QT 4.
linux-source- – this is the kernel source package you need to download in order to build the kernel.
module-assistant – this program is needed to build the missing modules after kernel compilation

Install the required programs
You can apt-get install all the programs above the way they are written, or get them through Synaptic. In a terminal:
apt-get update
apt-get install build-essential fakeroot kernel-package libncurses5-dev libqt4-dev module-assistant

(module-assistant seems to be of less and less use, being replaced by DKMS packages)

Download kernel source
You can download the kernel source package with apt-get or Synaptic. For example, if you want the 3.16 kernel you would write “apt-get install linux-source-3.16” without the quotes. Once installed you will find the source file linux-source-.tar.bz2 in /usr/src folder.
If you want a version of a kernel that is not in the Debian or antiX/antiX MX repositories you can get your kernel from here: ftp://ftp.kernel.org/pub/linux/kernel/ and choose the version by clicking on one of the folders whose names start with a “v”. Note that the filenames in these directory listings are sorted according to the ASCII collating sequence; 2.6.10 comes between 2.6.1 and 2.6.2, not after 2.6.9.
The very latest kernels may require updated versions of kernel-package from upstream Debian to build correctly; usually they can be installed directly from the Sid deb package from packages.debian.org.
Another easy way to get the kernel sources is to use Git. Install git-core:
apt-get install git-core

And then run:

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-3.16.git
This command will create a linux-3.16 folder with the latest kernel source files in the current directory. If you use this method you’ll need to skip the next (extraction) step. If you got the sources using git, you need to run this command in linux-3.16 folder:

sed -rie ‘s/echo “+”/#echo “+”/’ scripts/setlocalversion


Extract kernel file

  • If you download and install the kernel source package with synaptic, kpackage or apt-get, the linux-source-(ver).tar.gz or tar.bz2 file will be put into your /usr/src folder. It is recommended that you create a new kernel-build folder in your /home, copy the source tarball into that folder, then extract, patch, and build the kernel there as a normal user. You will need to extract that file by right-clicking on it and choosing “extract here”. Or you can do it manually in a terminal window using the command tar.


Make sure the file name you write matches what the real file name is!

  • If you download the kernel from the kernel.org ftp site, just extract it in your home per above, and proceed.

Apply kernel patches

  1. You would download the patches you need into a folder of your choice.
  2. Enter the extracted kernel source directory, and open up a terminal there. It’s recommended to put the source somewhere in your /home folder, instead of in /usr/src.
  3. Type patch -p1 < /path/to/kernel/patch (path to kernel patch is the full path to where you downloaded the patch to, /home/username/Downloads/foo.patch for an example, (foo.patch is what you would replace with the correct name of the patch). You would run the patch command for all patches you want to add to the kernel.

Configure the Kernel

  1. Enter the newly extracted source folder, open a terminal, and enter make oldconfig. Questions may come up during this and all you really need to do is press your “enter” key after each question to choose the default answer. You don’t need to type in an answer. If you do enter an answer and you chose a wrong one it could make your kernel unusable.
  2. After the make oldconfig is finished you will get the command prompt back and you can then choose to change some of your kernel’s configuration to remove unnecessary options or to add options that you do want. If you are upgrading the kernel, realize that the layout of kernel options often changes, and you may need to reselect something that was previously selected under a different menu before.
  3. To configure a stripped-down kernel with only support for your current hardware, use make localmodconfig This will configure the kernel based on what modules are currently loaded, plus what CPU features are detected. You should double-check the results in the GUI configurations below, and add any external USB device drivers may be necessary. It may also add virtualization features, based on your CPU, that you may want to disable.
  4. If you want to configure your kernel some more before building it, you can type make menuconfig and then go through all the kernel options to add or remove things.
  5. You can use make xconfig instead of make menuconfig. xconfig is a better-looking and easier-to-use kernel configuration tool, and you can load and save various config files.
  6. Configure the kernel to your liking but make sure that you don’t remove something you need. Change the processor to your specific model. Other performance suggestions can be found by searching the web. The more fluff you remove, the faster the kernel will be. If you know you have specific hardware, compile it into the kernel instead of a module. If you are unsure leave it as a module. Modules are inserted when needed. For information on what hardware you have, the output of lspci is helpful and installing the app lshw and running as root is also very informative. Practice makes perfect, and the entire process takes much less time with experience.


Compile and Build the Kernel
If necessary, clean the source folder:

make-kpkg clean
Build your kernel by typing:

CONCURRENCY_LEVEL=getconf _NPROCESSORS_ONLN fakeroot make-kpkg –initrd –append-to-version=-custom kernel_image kernel_headers
Add the -uc -us flags to also build the kernel source deb.

“custom” can be anything, though it must be in lower case and not have any odd characters (the alphabet and the numerals 0-9 are allowed only). The kernel will take a while to build so take a break while you wait for it. It can take an hour, more or less, depending on how big the kernel you compiled is and how fast your computer is, how many drivers you are building, and how many threads of execution and cores your CPU supports. Note that you can customize the number of threads, i.e:

CONCURRENCY_LEVEL=2
will only use two threads, even on a four core machine. The default command uses all cores available. This will generate a lot of heat, so make sure your machine is well ventilated.

Install the Kernel and the Extra Kernel Deb Files
When you get the command prompt back you can use a file manager to look the next level up from the extracted source directory and see if you have up to four packages with the .deb extension. The kernel-image and kernel-headers packages should be installed, the source and doc packages are optional and usually not needed. The easiest way to install these debs is to type in the terminal as root in the folder with the debs:

dpkg -i *.deb
This will install all the debs at one time. Gebi will also do the job in Mepis 8.0 and 8.5.

antiX and antiX MX currently use the initrd boot method. If you get a failure to build that file, you can create it manually with (as root)

update-initramfs -c -t -k
should be of the “2.6.32-custom-my-kernel” format, as an example.

After installing the kernel and rebooting to it, check in /lib/modules/(kernel_version) that you have the symlinks build and source linked to /usr/src/linux-headers-(kernel_version). If not, the following commands in a root terminal will fix it:

cd /lib/modules/$(uname -r)
rm build
rm source
ln -s /usr/src/linux-headers-$(uname -r) build
ln -s /usr/src/linux-headers-$(uname -r) source

If not running the new kernel, replace the string $(uname-r) with the actual kernel version, such as

ln -s /usr/src/linux-headers-2.6.33-custom build


Troubleshooting


Boot Menu
When you install the kernel package the entry should be created automatically in GRUB. If for some reason it’s not there, consult MX Boot Repair

Installing Missing Modules
Often after you have compiled the kernel there will be a few missing modules. Consult the Users Manual as needed.

After rebooting using the new kernel module-assistant needs to be initialized using

m-a update

Recompiling A Kernel
Same as above really. All you would need to do if you want to compile the same kernel again is to uninstall the kernel using Synaptic or apt-get. Be sure to purge (=remove completely). Then follow the guide above to build a newer kernel.

You uninstall a kernel before you compile, build and install again because if you don’t you will run into problems because of the previous and the new kernels having the same name. Uninstalling fixes that for you.

Explanation of Terms
cd = change directory
mv = move
ln -s = make a symbolic link
su = become root (will ask for root password)
tar xvzf = extract term used for extracting tar.gz files
tar xvjf = extract term used for extracting tar.bz2 files
/path/to = write out your full path to a file and include the filename
dpkg -i = is the command used to install a deb package
terminal = Konsole or Terminal program that you can find in KMenu > System menu
KMenu = The icon on your desktop that looks like a big K
make kpkg = command to make the kernel into a deb package
make oldconfig = command to set up your kernel to use the options from the kernel you are currently booted into.
make menuconfig = command to open a configuration menu for your kernel so that you can change options in the kernel.
make xconfig = same as the make menuconfig command but the menu is better looking and easier to read.
Kpackage = Package Manager found in Kmenu > System menu.
apt-get update = command used to update your apt sources so that you will then be able to use apt-get install command or apt-get upgrade command.
apt-get install = command used to install a package from the terminal. You would follow the command with the package name. Ex. apt-get install kernel-package
xhost +local:0 = command you need to type as user in the terminal in order to give root permission to use run GUI applications as root.

Optional Terms – If needed
rm = command to remove a file or a symbolic link
rmdir = command to remove a directory or folder
cp = command used to copy a file or folder from one place to another
gzip -d = command to unzip a file with the gz extension
bz2 -d = command to unzip a file with the bz2 extension
tar xvf = command to unzip a file with the tar extension, used after a gzip -d or bz2 -d command. Note that tar does not allow anything but a filename to follow the f switch; all other switches must come before the f.

More Information
You can always check the latest notes on compiling the kernel using the Debian tools at /usr/share/doc/linux-source-version_number/debian.README.gz test edit

v. 20150716

3 thoughts on “Compiling and installing a new kernel”

  1. Agree – a point in this example is to go Backwards kernel version 3.16 – the want would be now to go to advanced territory and opt for the Latest kernel 5.x.y levels. Documentation could have examples or posted in the blog per “Kernel Updates” Be nice if there where available various kernels to try similar to other distros have the ability to install as few versions higher then, stable kernel allowing a user to use the more advanced and or More fixed from bugs kernel be a thought. Examples of steps can really be helpful

    Reply
  2. –Documentation is in the Manual, Section 7.6
    –Installation of higher/lower kernels is carried out in the Package Installer > Kernel section

    Reply

Leave a Comment

Do NOT follow this link or you will be banned from the site!