Skip to content

Instantly share code, notes, and snippets.

@EduApps-CDG
Last active May 26, 2024 00:13
Show Gist options
  • Save EduApps-CDG/733e29c28dd53e91128d384c2e879397 to your computer and use it in GitHub Desktop.
Save EduApps-CDG/733e29c28dd53e91128d384c2e879397 to your computer and use it in GitHub Desktop.
A guide of How to Build Linux Kernel using android

How to Build Linux Kernel with Android

This guide shows how to build Linux on a Android Device and was made for people that doesn't have s Computer.

Minimum Requiriments:

  • 4GB of free space (2GB if delete the temp files)
  • Android 5.0

Preparing the Environment

You need to install Termux from Google Play Store. Now we will download the Kernel so we need WGET:

apt-get update &&
apt-get --assume-yes install wget &&
cd ~ &&
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.6.tar.xz

The next step is decompress that tarball with UNXZ and TAR (integrated with Termux):

unzx -d linux-5.4.6.tar.xz  &&
tar -xf linux-5.4.6.tar

(Optional) Delete the Linux tarball:

rm linux-5.4.6.tar

Now we have the Linux Source on our Android, lets compile!

Compiling

In Android, compile is a terror for Linux Kernel. First we need to create a config file.

The .config file dilemma

If you had an Linux PC you could simply do this:

cp /boot/config-$(uname -r) linux-5.4.6/.config

But you dont have, so we need to do this:

Root mode (easy)

If you have a Rooted Potato Phone you can simply:

cp /proc/config.gz ~ &&
gzip -d config.gz &&
mv config linux-5.4.6/.config &&
cd linux-5.46

No Root mode (medium)

If else, first you need to download it from our repository:

wget https://eduapps-cdg.github.io/kernel-configs/raw/master/android/$(getprop | grep -i ro.product.model | sed "s/\[ro.product.model\]: //" | sed "s/\[//" | sed "s/\]//").gz &&
mv $(getprop | grep -i ro.product.model | sed "s/\[ro.product.model\]: //" | sed "s/\[//" | sed "s/\]//").gz config.gz &&
gzip -d config.gz &&
mv config linux-5.4.6/.config &&
cd linux-5.46

No Root mode (hard)

If the file was not found, you must generate it. It has only one command, but this command is the worst! It's the best choice if you are developing a new device. Complete the first command of Building and type:

make menuconfig

Building

Once solved the dilemma, you will not need to do it again. the next step is install the compilers:

apt-get install build-essential ncurses bison flex openssl libelf clang

This command will setup to build the Kernel with full power, if you have a single core device, it doesn't help so much but works:

make -j $(nproc)
make modules_install
make install

And finally, the Last Step is: Enjoy the building time and drink a coffee!

@scoot1980
Copy link

is it safe to do this?

@EduApps-CDG
Copy link
Author

yes, i done it

@Austcool-Walker
Copy link

This is to install a newer kernel on android correct? @EduApps-CDG

@Austcool-Walker
Copy link

Screenshot_20220203-151117_Termux

@EduApps-CDG
Copy link
Author

EduApps-CDG commented Feb 3, 2022

@Austcool-Walker never tried, what I did was compile a custom kernel for Ubuntu in Termux. You can make custom roms the same way.
About your print, it appears that this is a compilation issue.

Maybe this solves your issue.

@xexuza
Copy link

xexuza commented Jul 12, 2022

Bro when i try this command cp /proc/config.gz it says missing file operand pls help i want to buildkernel without pc

@EduApps-CDG
Copy link
Author

EduApps-CDG commented Jul 12, 2022 via email

@OfficiallyMelon
Copy link

Bro when i try this command cp /proc/config.gz it says missing file operand pls help i want to buildkernel without pc

You need to copy the whole command

@hqnicolas
Copy link

hqnicolas commented Nov 1, 2022

Android 11 Kernel 4.19
Dont work, anyone can help?

~ $ cd linux-5.4.6
~/linux-5.4.6 $ make -j $(nproc)
HOSTLD scripts/kconfig/conf
ld.lld: error: undefined symbol: bcmp

referenced by confdata.c
scripts/kconfig/confdata.o:(conf_write)
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [scripts/Makefile.host:116: scripts/kconfig/conf] Error 1
make[1]: *** [Makefile:567: syncconfig] Error 2
make: *** [Makefile:678: include/config/auto.conf.cmd] Error 2
~/linux-5.4.6 $

@EduApps-CDG
Copy link
Author

EduApps-CDG commented Nov 2, 2022

Android 11 Kernel 4.19
Dont work, anyone can help?

@hotnikq

Hello, did you tried to run it inside a Ubuntu chroot and see if it works?

Also, i got an Android 12 some days ago. I will recheck it later and see if it still works.

@RadioactivePlays
Copy link

RadioactivePlays commented Sep 21, 2023

Can it be used to natively compile the same kernel that is running on the phone?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment