> For the complete documentation index, see [llms.txt](https://i.janardhanpulivarthi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://i.janardhanpulivarthi.com/season-2/build-linux-kernel.md).

# Build linux kernel

### Get a copy of source code

Take a copy of the linux kernel source repository via `git` software

```
git clone --depth=1 https://github.com/torvalds/linux
```

or download a zip file from this link - <https://github.com/torvalds/linux/archive/refs/heads/master.zip>

### Generate `.config` file

```
make menuconfig
```

#### menuconfig options

Here are the images of the some menuconfig options chosen.

![menuconfig](/files/RIUHdJ1Pw0QjoCdK2xgG)

![Options](/files/hOxKxCWdjxL4uCVQby9T) ![kernel .config support](/files/oLNHDoXzUJkc9Fa5fhYU) ![Module unloading](/files/FU3CFgDt3uv6iwpCZqN0) ![Kernel Function Tracer](/files/zoDjdkKFqRutqMBwLpNJ)

Run `make help` to get the hints and options for building.

### build a image

After that do, `make bzImage`.

```bash
/workspace/linux (master) $ make bzImage

  SYSHDR  arch/x86/include/generated/uapi/asm/unistd_32.h
...
  GZIP    arch/x86/boot/compressed/vmlinux.bin.gz
  HOSTCC  arch/x86/boot/compressed/mkpiggy
  MKPIGGY arch/x86/boot/compressed/piggy.S
  AS      arch/x86/boot/compressed/piggy.o
  CC      arch/x86/boot/compressed/cpuflags.o
  CC      arch/x86/boot/compressed/early_serial_console.o
  CC      arch/x86/boot/compressed/kaslr.o
  CC      arch/x86/boot/compressed/ident_map_64.o
  CC      arch/x86/boot/compressed/idt_64.o
  AS      arch/x86/boot/compressed/idt_handlers_64.o
  AS      arch/x86/boot/compressed/mem_encrypt.o
  CC      arch/x86/boot/compressed/pgtable_64.o
  CC      arch/x86/boot/compressed/acpi.o
  AS      arch/x86/boot/compressed/efi_thunk_64.o
  LD      arch/x86/boot/compressed/vmlinux
  ZOFFSET arch/x86/boot/zoffset.h
  AS      arch/x86/boot/header.o
  CC      arch/x86/boot/main.o
  CC      arch/x86/boot/memory.o
  CC      arch/x86/boot/pm.o
  AS      arch/x86/boot/pmjump.o
  CC      arch/x86/boot/printf.o
  CC      arch/x86/boot/regs.o
  CC      arch/x86/boot/string.o
  CC      arch/x86/boot/tty.o
  CC      arch/x86/boot/video.o
  CC      arch/x86/boot/video-mode.o
  CC      arch/x86/boot/version.o
  CC      arch/x86/boot/video-vga.o
  CC      arch/x86/boot/video-vesa.o
  CC      arch/x86/boot/video-bios.o
  LD      arch/x86/boot/setup.elf
  OBJCOPY arch/x86/boot/setup.bin
  OBJCOPY arch/x86/boot/vmlinux.bin
  HOSTCC  arch/x86/boot/tools/build
  BUILD   arch/x86/boot/bzImage
Kernel: arch/x86/boot/bzImage is ready  (#1)
/workspace/linux (master) $ 
```

Note **Kernel: arch/x86/boot/bzImage is ready (#1)**.

### Errors

fatal error: libelf.h: No such file or directory

```bash
/workspace/linux (master) $ make
  SYSHDR  arch/x86/include/generated/uapi/asm/unistd_32.h
...
  DESCEND objtool
<stdin>:1:10: fatal error: libelf.h: No such file or directory
compilation terminated.
  HOSTCC  /workspace/linux/tools/objtool/fixdep.o
...
  AR      /workspace/linux/tools/objtool/libsubcmd.a
  CC      /workspace/linux/tools/objtool/arch/x86/special.o
In file included from /workspace/linux/tools/objtool/include/objtool/objtool.h:13,
                 from /workspace/linux/tools/objtool/include/objtool/arch.h:11,
                 from /workspace/linux/tools/objtool/include/objtool/check.h:11,
                 from /workspace/linux/tools/objtool/include/objtool/special.h:10,
                 from arch/x86/special.c:4:
/workspace/linux/tools/objtool/include/objtool/elf.h:10:10: fatal error: gelf.h: No such file or directory
   10 | #include <gelf.h>
      |          ^~~~~~~~
compilation terminated.
make[4]: *** [/workspace/linux/tools/build/Makefile.build:97: /workspace/linux/tools/objtool/arch/x86/special.o] Error 1
make[3]: *** [/workspace/linux/tools/build/Makefile.build:139: arch/x86] Error 2
make[2]: *** [Makefile:56: /workspace/linux/tools/objtool/objtool-in.o] Error 2
make[1]: *** [Makefile:69: objtool] Error 2
make: *** [Makefile:1337: tools/objtool] Error 2
```

Install `libelf-dev` package

```
/workspace/linux (master) $ make bzImage
  SYNC    include/config/auto.conf.cmd
  LEX     scripts/kconfig/lexer.lex.c
  YACC    scripts/kconfig/parser.tab.[ch]
  HOSTCC  scripts/kconfig/lexer.lex.o
  HOSTCC  scripts/kconfig/menu.o
  HOSTCC  scripts/kconfig/parser.tab.o
  HOSTCC  scripts/kconfig/preprocess.o
  HOSTCC  scripts/kconfig/symbol.o
  HOSTCC  scripts/kconfig/util.o
  HOSTLD  scripts/kconfig/conf
*
* Restart config...
*
*
* Control Group support
*
Control Group support (CGROUPS) [Y/n/?] y
  Favor dynamic modification latency reduction by default (CGROUP_FAVOR_DYNMODS) [N/y/?] (NEW) y
```

aa

```
/workspace/linux (master) $ make bzImage
  SYNC    include/config/auto.conf.cmd
  LEX     scripts/kconfig/lexer.lex.c
  YACC    scripts/kconfig/parser.tab.[ch]
  HOSTCC  scripts/kconfig/lexer.lex.o
  HOSTCC  scripts/kconfig/menu.o
  HOSTCC  scripts/kconfig/parser.tab.o
  HOSTCC  scripts/kconfig/preprocess.o
  HOSTCC  scripts/kconfig/symbol.o
  HOSTCC  scripts/kconfig/util.o
  HOSTLD  scripts/kconfig/conf
*
* Restart config...
*
*
* Control Group support
*
Control Group support (CGROUPS) [Y/n/?] y
  Favor dynamic modification latency reduction by default (CGROUP_FAVOR_DYNMODS) [N/y/?] (NEW) y
```

## Video

{% embed url="<https://www.youtube.com/watch?v=SuB-rJ_baTM>" %}
