How to compile kernel in ubuntu 20.04

STEP 1: FIRST TYPE IN TERMINAL : sudo nano /etc/default/grub

where nano is your TEXT editor

STEP2: INCREASE GRUB_TIMEOUT TO 10 WHICH DETERMINES YOUR BOOTING TIME

press ctrl+o and enter to save the file and then ctrl+x to exit.

STEP 3. type: sudo update-grub to update the changes.

STEP 4: TYPE: uname -r to show the kernel version installed in your computer . OR uname -a all about it

STEP 5: GO TO www.kernel.org and download the stable or longterm release (tarball version) it is highly recommanded to download the stable or longterm versions .

STEP6.GO TO your downloaded KERNEL FOLDER( WE ASSUME THAT THE FOLDER IS IN /Downloads/)

STEP7. type: cd ~/Downloads/ in terminal and type mkdir linux_kernel

STEP8. EXTRACT THE .tar file there (in the linux_kernel folder )

STEP9. TYPE : sudo apt-get install build-essential gcc bc bison flex libssl-dev libncurses5-dev libelf-dev

to install dependencies gcc is the c compiler flebx is lexical analyzer and bison is parser.

STEP10. type: cp /boot/config-$(uname -r) ./. config to provide support a wide range of hardware by copying the .config file.

STEP 11. type make localmodconfig it will ask u questions about the new drivers and support of kernel keep pressing enter to set all the answers of the questions to default or u may search google for answering this.you have to type this command in /Downloads/linux_kernel folder otherwise the method will not work…

IF U KNOW WHAT THE KERNEL MODULE IS ABOUT THEN U MAY ANSWER THIS …

or…

The Linux kernel build-system provide many build targets, the best way to know about it is probably to do a make help:

Configuration targets:

config - Update current config utilising a line-oriented program

nconfig - Update current config utilising a ncurses menu based program

menuconfig - Update current config utilising a menu based program

xconfig - Update current config utilising a QT based front-end

gconfig - Update current config utilising a GTK based front-end

oldconfig - Update current config utilising a provided .config as base

localmodconfig - Update current config disabling modules not loaded

localyesconfig - Update current config converting local mods to core

silentoldconfig - Same as oldconfig, but quietly, additionally update deps

defconfig - New config with default from ARCH supplied defconfig

savedefconfig - Save current config as ./defconfig (minimal config)

allnoconfig - New config where all options are answered with no

allyesconfig - New config where all options are accepted with yes

allmodconfig - New config selecting modules when possible

alldefconfig - New config with all symbols set to default

randconfig - New config with random answer to all options

listnewconfig - List new options

olddefconfig - Same as silentoldconfig but sets new symbols to their default value

kvmconfig - Enable additional options for guest kernel support

tinyconfig - Configure the tiniest possible kernel

what i have written above is a list of all kernel compiling policies .

STEP 12:TYPE: make -j4 deb-pkg where 4 is the number of cores/thread of your processor u want to give the kernel compiling process … now take a cofee mug in your hand wait for half of an hour or 40-50 mins acconding to your proceesors quality. the command will make a .deb file of your compiled kernel.

STEP 13: Type: sudo dpkg -i linux-*.deb it will install the compiled .deb package in your system.
STEP 14: Type: sudo update-grub to update grub of your system .

STEP15: NOW REBOOT AND YOU WILL SEE YOUR COMPILED KERNEL IN GRUB MENU. ENJOY YOUR OWN KERNEL.

FOR YOUR HELP I THINK THIS VIDEO WILL BE HELPFUL
https://youtu.be/E4yRcmQqvWM
’
IF U DO NOT WANT TO COMPILE KERNEL JUST INSTALL THE KERNEL THEN YOU CAN SEARCH FOR linux-headers and linux-image packages by running apt-cache search
and then update your grub by sudo update-grub

There is another way to do it. You can install .deb packages of kenel and linux-headers
then run STEP 13 AND STEP 14

5 Likes

after step 12: I have got below files in my /Downloads folder and the I fallowed the step 13 with “sudo dpkg -i linux-mage-5.10.34-dbg_5.10.34-1_amd64.deb” command.
But after reboot I didn’t see my new kernel image in my /boot folder.
Can you help with this to me?
linux-headers-5.10.34_5.10.34-1_amd64.deb
linux-image-5.10.34_5.10.34-1_amd64.deb
linux-image-5.10.34-dbg_5.10.34-1_amd64.deb
linux-libc-dev_5.10.34-1_amd64.deb

After running sudo dpkg -i linux-*.deb, you’ll need to run sudo update-grub.

1 Like

I have updated the grub boot2
but still there is no 5.10.34 kernel in my /boot

go https://launchpad.net/ubuntu/+source/linux/

wget https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/linux/5.11.0-25.27/linux_5.11.0.orig.tar.gz
wget https://launchpad.net/ubuntu/+archive/primary/+sourcefiles/linux/5.11.0-25.27/linux_5.11.0-25.27.diff.gz
cd
rm -rf ubuntucustom
mkdir ubuntucustom
cd ubuntucustom
tar xzf ../linux_5.11.0.orig.tar.gz
cp ../linux_5.11.0-25.27.diff.gz ./
gunzip *.diff.gz
cd linux-5.11
patch -p1 < ../linux_*.diff
cp /boot/config-`uname -r` .config
yes "" | make oldconfig
make clean
make -j `getconf _NPROCESSORS_ONLN` deb-pkg LOCALVERSION=-25.27custom

Ok sir ! Thank You ! I forgot to write it

1 Like

Hi,

I have met below errors, do you know how to fix this?

make[2]: *** [debian/rules:7: build-arch] Error 2
dpkg-buildpackage: error: debian/rules binary subprocess returned exit status 2
make[1]: *** [scripts/Makefile.package:77: deb-pkg] Error 2
make: *** [Makefile:1576: deb-pkg] Error 2

Note that i have already change the SAFETY_TRUSTED_KEYS to “”, didn’t help.

The build tools are all have been installed,

bc is already the newest version (1.07.1-2build1).
bison is already the newest version (2:3.5.1+dfsg-1).
flex is already the newest version (2.6.4-6.2).
gcc is already the newest version (4:9.3.0-1ubuntu2).
gcc set to manually installed.
libelf-dev is already the newest version (0.176-1.1build1).
libncurses5-dev is already the newest version (6.2-0ubuntu2).
build-essential is already the newest version (12.8ubuntu1.1).
libssl-dev is already the newest version (1.1.1f-1ubuntu2.8).

Thanks a lot for help.

Exactly the same issues here. Curious if you found a solution.

there are errors in the guide

at least from step 7

STEP7. type: cd ~/Downloads/ in terminal and type mkdir linux_kernel
STEP8. EXTRACT THE .tar file there (in the linux_kernel folder )
STEP9. TYPE : sudo apt-get install build-essential gcc bc bison flex libssl-dev libncurses5-dev libelf-dev
to install dependencies gcc is the c compiler flebx is lexical analyzer and bison is parser.
STEP10. type: cp /boot/config-$(uname -r) ./. config to provide support a wide range of hardware by copying the .config file.
STEP 11. type make localmodconfig

It is not clear what should be done, besides they make mistakes, which for a novice kills the spirit of compiling a kernel. sorry my english is bad

I do it like this:
STEP7. cd ~/Downloads/
name@ubuntu:~/Downloads$
STEP8. name@ubuntu:~/Downloads/linux_kernel$ tar -xf /home/name/Downloads/linux-5.16.2.tar.xz
…
STEP10. type: cp /boot/config-$(uname -r) ./.config

the file: .config it will stay in the folder: /Downloads/linux_kernel/.config

and it should be in the folder: /Downloads/linux_kernel/linux-5.16.2/.config

that’s why the command: make localmodconfig
it will work if it is done in the folder: /Downloads/linux_kernel/linux-5.16.2

2 Likes

For me it worked with make localmodconfig in step on ubuntu 20.04. Thank you for this wonderful guide!

A post was split to a new topic: Wifi failed after compiling

root@ziomario-Z390-AORUS-PRO:/usr/src/linux-source-6.5.0/linux-source-6.5.0# make

…
CC [M] ubuntu/ubuntu-host/ubuntu-host.mod.o
LD [M] ubuntu/ubuntu-host/ubuntu-host.ko
BTF [M] ubuntu/ubuntu-host/ubuntu-host.ko

CALL scripts/checksyscalls.sh
DESCEND objtool
INSTALL libsubcmd_headers
DESCEND bpf/resolve_btfids
INSTALL libsubcmd_headers
CHK kernel/kheaders_data.tar.xz
Kernel: arch/x86/boot/bzImage is ready (#2)

Reading from here :

he says to do this :

root@ziomario-Z390-AORUS-PRO:/usr/src/linux-source-6.5.0/linux-source-6.5.0# make -j4 deb-pkg
GEN debian
error: creating source package requires git repository

What’s the next command to install the recompiled kernel or better to produce a deb package ?