xref: /linux/Documentation/admin-guide/quickly-build-trimmed-linux.rst (revision 5181afcdf99527dd92a88f80fc4d0d8013e1b510)
1.. SPDX-License-Identifier: (GPL-2.0+ OR CC-BY-4.0)
2.. [see the bottom of this file for redistribution information]
3
4===========================================
5How to quickly build a trimmed Linux kernel
6===========================================
7
8This guide explains how to swiftly build Linux kernels that are ideal for
9testing purposes, but perfectly fine for day-to-day use, too.
10
11The essence of the process (aka 'TL;DR')
12========================================
13
14*[If you are new to compiling Linux, ignore this TLDR and head over to the next
15section below: it contains a step-by-step guide, which is more detailed, but
16still brief and easy to follow; that guide and its accompanying reference
17section also mention alternatives, pitfalls, and additional aspects, all of
18which might be relevant for you.]*
19
20If your system uses techniques like Secure Boot, prepare it to permit starting
21self-compiled Linux kernels; install compilers and everything else needed for
22building Linux; make sure to have 12 Gigabyte free space in your home directory.
23Now run the following commands to download fresh Linux mainline sources, which
24you then use to configure, build and install your own kernel::
25
26    git clone --depth 1 -b master \
27      https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git ~/linux/
28    cd ~/linux/
29    # Hint: if you want to apply patches, do it at this point. See below for details.
30    # Hint: it's recommended to tag your build at this point. See below for details.
31    yes "" | make localmodconfig
32    # Hint: at this point you might want to adjust the build configuration; you'll
33    #   have to, if you are running Debian. See below for details.
34    make -j $(nproc --all)
35    # Note: on many commodity distributions the next command suffices, but on Arch
36    #   Linux, its derivatives, and some others it does not. See below for details.
37    command -v installkernel && sudo make modules_install install
38    reboot
39
40If you later want to build a newer mainline snapshot, use these commands::
41
42    cd ~/linux/
43    git fetch --depth 1 origin
44    # Note: the next command will discard any changes you did to the code:
45    git checkout --force --detach origin/master
46    # Reminder: if you want to (re)apply patches, do it at this point.
47    # Reminder: you might want to add or modify a build tag at this point.
48    make olddefconfig
49    make -j $(nproc --all)
50    # Reminder: the next command on some distributions does not suffice.
51    command -v installkernel && sudo make modules_install install
52    reboot
53
54Step-by-step guide
55==================
56
57Compiling your own Linux kernel is easy in principle. There are various ways to
58do it. Which of them actually work and is the best depends on the circumstances.
59
60This guide describes a way perfectly suited for those who want to quickly
61install Linux from sources without being bothered by complicated details; the
62goal is to cover everything typically needed on mainstream Linux distributions
63running on commodity PC or server hardware.
64
65The described approach is great for testing purposes, for example to try a
66proposed fix or to check if a problem was already fixed in the latest codebase.
67Nonetheless, kernels built this way are also totally fine for day-to-day use
68while at the same time being easy to keep up to date.
69
70The following steps describe the important aspects of the process; a
71comprehensive reference section later explains each of them in more detail. It
72sometimes also describes alternative approaches, pitfalls, as well as errors
73that might occur at a particular point -- and how to then get things rolling
74again.
75
76..
77   Note: if you see this note, you are reading the text's source file. You
78   might want to switch to a rendered version, as it makes it a lot easier to
79   quickly look something up in the reference section and afterwards jump back
80   to where you left off. Find a the latest rendered version here:
81   https://docs.kernel.org/admin-guide/quickly-build-trimmed-linux.html
82
83.. _backup_sbs:
84
85 * Create a fresh backup and put system repair and restore tools at hand, just
86   to be prepared for the unlikely case of something going sideways.
87
88   [:ref:`details<backup>`]
89
90.. _secureboot_sbs:
91
92 * On platforms with 'Secure Boot' or similar techniques, prepare everything to
93   ensure the system will permit your self-compiled kernel to boot later. The
94   quickest and easiest way to achieve this on commodity x86 systems is to
95   disable such techniques in the BIOS setup utility; alternatively, remove
96   their restrictions through a process initiated by
97   ``mokutil --disable-validation``.
98
99   [:ref:`details<secureboot>`]
100
101.. _buildrequires_sbs:
102
103 * Install all software required to build a Linux kernel. Often you will need:
104   'bc', 'binutils' ('ld' et al.), 'bison', 'flex', 'gcc', 'git', 'openssl',
105   'pahole', 'perl', and the development headers for 'libelf' and 'openssl'. The
106   reference section shows how to quickly install those on various popular Linux
107   distributions.
108
109   [:ref:`details<buildrequires>`]
110
111.. _diskspace_sbs:
112
113 * Ensure to have enough free space for building and installing Linux. For the
114   latter 150 Megabyte in /lib/ and 100 in /boot/ are a safe bet. For storing
115   sources and build artifacts 12 Gigabyte in your home directory should
116   typically suffice. If you have less available, be sure to check the reference
117   section for the step that explains adjusting your kernels build
118   configuration: it mentions a trick that reduce the amount of required space
119   in /home/ to around 4 Gigabyte.
120
121   [:ref:`details<diskspace>`]
122
123.. _sources_sbs:
124
125 * Retrieve the sources of the Linux version you intend to build; then change
126   into the directory holding them, as all further commands in this guide are
127   meant to be executed from there.
128
129   *[Note: the following paragraphs describe how to retrieve the sources by
130   partially cloning the Linux stable git repository. This is called a shallow
131   clone. The reference section explains two alternatives:* :ref:`packaged
132   archives<sources_archive>` *and* :ref:`a full git clone<sources_full>` *;
133   prefer the latter, if downloading a lot of data does not bother you, as that
134   will avoid some* :ref:`peculiar characteristics of shallow clones the
135   reference section explains<sources_shallow>` *.]*
136
137   First, execute the following command to retrieve a fresh mainline codebase::
138
139     git clone --no-checkout --depth 1 -b master \
140       https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git ~/linux/
141     cd ~/linux/
142
143   If you want to access recent mainline releases and pre-releases, deepen you
144   clone's history to the oldest mainline version you are interested in::
145
146     git fetch --shallow-exclude=v6.0 origin
147
148   In case you want to access a stable/longterm release (say v6.1.5), simply add
149   the branch holding that series; afterwards fetch the history at least up to
150   the mainline version that started the series (v6.1)::
151
152     git remote set-branches --add origin linux-6.1.y
153     git fetch --shallow-exclude=v6.0 origin
154
155   Now checkout the code you are interested in. If you just performed the
156   initial clone, you will be able to check out a fresh mainline codebase, which
157   is ideal for checking whether developers already fixed an issue::
158
159      git checkout --detach origin/master
160
161   If you deepened your clone, you instead of ``origin/master`` can specify the
162   version you deepened to (``v6.0`` above); later releases like ``v6.1`` and
163   pre-release like ``v6.2-rc1`` will work, too. Stable or longterm versions
164   like ``v6.1.5`` work just the same, if you added the appropriate
165   stable/longterm branch as described.
166
167   [:ref:`details<sources>`]
168
169.. _patching_sbs:
170
171 * In case you want to apply a kernel patch, do so now. Often a command like
172   this will do the trick::
173
174     patch -p1 < ../proposed-fix.patch
175
176   If the ``-p1`` is actually needed, depends on how the patch was created; in
177   case it does not apply thus try without it.
178
179   If you cloned the sources with git and anything goes sideways, run ``git
180   reset --hard`` to undo any changes to the sources.
181
182   [:ref:`details<patching>`]
183
184.. _tagging_sbs:
185
186 * If you patched your kernel or have one of the same version installed already,
187   better add a unique tag to the one you are about to build::
188
189     echo "-proposed_fix" > localversion
190
191   Running ``uname -r`` under your kernel later will then print something like
192   '6.1-rc4-proposed_fix'.
193
194   [:ref:`details<tagging>`]
195
196 .. _configuration_sbs:
197
198 * Create the build configuration for your kernel based on an existing
199   configuration.
200
201   If you already prepared such a '.config' file yourself, copy it to
202   ~/linux/ and run ``make olddefconfig``.
203
204   Use the same command, if your distribution or somebody else already tailored
205   your running kernel to your or your hardware's needs: the make target
206   'olddefconfig' will then try to use that kernel's .config as base.
207
208   Using this make target is fine for everybody else, too -- but you often can
209   save a lot of time by using this command instead::
210
211     yes "" | make localmodconfig
212
213   This will try to pick your distribution's kernel as base, but then disable
214   modules for any features apparently superfluous for your setup. This will
215   reduce the compile time enormously, especially if you are running an
216   universal kernel from a commodity Linux distribution.
217
218   There is a catch: 'localmodconfig' is likely to disable kernel features you
219   did not use since you booted your Linux -- like drivers for currently
220   disconnected peripherals or a virtualization software not haven't used yet.
221   You can reduce or nearly eliminate that risk with tricks the reference
222   section outlines; but when building a kernel just for quick testing purposes
223   it is often negligible if such features are missing. But you should keep that
224   aspect in mind when using a kernel built with this make target, as it might
225   be the reason why something you only use occasionally stopped working.
226
227   [:ref:`details<configuration>`]
228
229.. _configmods_sbs:
230
231 * Check if you might want to or have to adjust some kernel configuration
232   options:
233
234  * Evaluate how you want to handle debug symbols. Enable them, if you later
235    might need to decode a stack trace found for example in a 'panic', 'Oops',
236    'warning', or 'BUG'; on the other hand disable them, if you are short on
237    storage space or prefer a smaller kernel binary. See the reference section
238    for details on how to do either. If neither applies, it will likely be fine
239    to simply not bother with this. [:ref:`details<configmods_debugsymbols>`]
240
241  * Are you running Debian? Then to avoid known problems by performing
242    additional adjustments explained in the reference section.
243    [:ref:`details<configmods_distros>`].
244
245  * If you want to influence the other aspects of the configuration, do so now
246    by using make targets like 'menuconfig' or 'xconfig'.
247    [:ref:`details<configmods_individual>`].
248
249.. _build_sbs:
250
251 * Build the image and the modules of your kernel::
252
253     make -j $(nproc --all)
254
255   If you want your kernel packaged up as deb, rpm, or tar file, see the
256   reference section for alternatives.
257
258   [:ref:`details<build>`]
259
260.. _install_sbs:
261
262 * Now install your kernel::
263
264     command -v installkernel && sudo make modules_install install
265
266   Often all left for you to do afterwards is a ``reboot``, as many commodity
267   Linux distributions will then create an initramfs (also known as initrd) and
268   an entry for your kernel in your bootloader's configuration; but on some
269   distributions you have to take care of these two steps manually for reasons
270   the reference section explains.
271
272   On a few distributions like Arch Linux and its derivatives the above command
273   does nothing at all; in that case you have to manually install your kernel,
274   as outlined in the reference section.
275
276   If you are running an immutable Linux distribution, check its documentation
277   and the web to find out how to install your own kernel there.
278
279   [:ref:`details<install>`]
280
281.. _another_sbs:
282
283 * To later build another kernel you need similar steps, but sometimes slightly
284   different commands.
285
286   First, switch back into the sources tree::
287
288      cd ~/linux/
289
290   In case you want to build a version from a stable/longterm series you have
291   not used yet (say 6.2.y), tell git to track it::
292
293      git remote set-branches --add origin linux-6.2.y
294
295   Now fetch the latest upstream changes; you again need to specify the earliest
296   version you care about, as git otherwise might retrieve the entire commit
297   history::
298
299     git fetch --shallow-exclude=v6.0 origin
300
301   Now switch to the version you are interested in -- but be aware the command
302   used here will discard any modifications you performed, as they would
303   conflict with the sources you want to checkout::
304
305     git checkout --force --detach origin/master
306
307   At this point you might want to patch the sources again or set/modify a build
308   tag, as explained earlier. Afterwards adjust the build configuration to the
309   new codebase using olddefconfig, which will now adjust the configuration file
310   you prepared earlier using localmodconfig  (~/linux/.config) for your next
311   kernel::
312
313     # reminder: if you want to apply patches, do it at this point
314     # reminder: you might want to update your build tag at this point
315     make olddefconfig
316
317   Now build your kernel::
318
319     make -j $(nproc --all)
320
321   Afterwards install the kernel as outlined above::
322
323     command -v installkernel && sudo make modules_install install
324
325   [:ref:`details<another>`]
326
327.. _uninstall_sbs:
328
329 * Your kernel is easy to remove later, as its parts are only stored in two
330   places and clearly identifiable by the kernel's release name. Just ensure to
331   not delete the kernel you are running, as that might render your system
332   unbootable.
333
334   Start by deleting the directory holding your kernel's modules, which is named
335   after its release name -- '6.0.1-foobar' in the following example::
336
337     sudo rm -rf /lib/modules/6.0.1-foobar
338
339   Now try the following command, which on some distributions will delete all
340   other kernel files installed while also removing the kernel's entry from the
341   bootloader configuration::
342
343     command -v kernel-install && sudo kernel-install -v remove 6.0.1-foobar
344
345   If that command does not output anything or fails, see the reference section;
346   do the same if any files named '*6.0.1-foobar*' remain in /boot/.
347
348   [:ref:`details<uninstall>`]
349
350.. _submit_improvements_qbtl:
351
352Did you run into trouble following the step-by-step guide not cleared up by the
353reference section below? Did you spot errors? Or do you have ideas on how to
354improve the guide?
355
356If any of that applies, please let the developers know by sending a short note
357or a patch to Thorsten Leemhuis <linux@leemhuis.info> while ideally CCing the
358public Linux docs mailing list <linux-doc@vger.kernel.org>. Such feedback is
359vital to improve this text further, which is in everybody's interest, as it will
360enable more people to master the task described here.
361
362Reference section for the step-by-step guide
363============================================
364
365This section holds additional information for each of the steps in the above
366guide.
367
368.. _backup:
369
370Prepare for emergencies
371-----------------------
372
373   *Create a fresh backup and put system repair and restore tools at hand*
374   [:ref:`... <backup_sbs>`]
375
376Remember, you are dealing with computers, which sometimes do unexpected things
377-- especially if you fiddle with crucial parts like the kernel of an operating
378system. That's what you are about to do in this process. Hence, better prepare
379for something going sideways, even if that should not happen.
380
381[:ref:`back to step-by-step guide <backup_sbs>`]
382
383.. _secureboot:
384
385Dealing with techniques like Secure Boot
386----------------------------------------
387
388   *On platforms with 'Secure Boot' or similar techniques, prepare everything to
389   ensure the system will permit your self-compiled kernel to boot later.*
390   [:ref:`... <secureboot_sbs>`]
391
392Many modern systems allow only certain operating systems to start; they thus by
393default will reject booting self-compiled kernels.
394
395You ideally deal with this by making your platform trust your self-built kernels
396with the help of a certificate and signing. How to do that is not described
397here, as it requires various steps that would take the text too far away from
398its purpose; 'Documentation/admin-guide/module-signing.rst' and various web
399sides already explain this in more detail.
400
401Temporarily disabling solutions like Secure Boot is another way to make your own
402Linux boot. On commodity x86 systems it is possible to do this in the BIOS Setup
403utility; the steps to do so are not described here, as they greatly vary between
404machines.
405
406On mainstream x86 Linux distributions there is a third and universal option:
407disable all Secure Boot restrictions for your Linux environment. You can
408initiate this process by running ``mokutil --disable-validation``; this will
409tell you to create a one-time password, which is safe to write down. Now
410restart; right after your BIOS performed all self-tests the bootloader Shim will
411show a blue box with a message 'Press any key to perform MOK management'. Hit
412some key before the countdown exposes. This will open a menu and choose 'Change
413Secure Boot state' there. Shim's 'MokManager' will now ask you to enter three
414randomly chosen characters from the one-time password specified earlier. Once
415you provided them, confirm that you really want to disable the validation.
416Afterwards, permit MokManager to reboot the machine.
417
418[:ref:`back to step-by-step guide <secureboot_sbs>`]
419
420.. _buildrequires:
421
422Install build requirements
423--------------------------
424
425   *Install all software required to build a Linux kernel.*
426   [:ref:`...<buildrequires_sbs>`]
427
428The kernel is pretty stand-alone, but besides tools like the compiler you will
429sometimes need a few libraries to build one. How to install everything needed
430depends on your Linux distribution and the configuration of the kernel you are
431about to build.
432
433Here are a few examples what you typically need on some mainstream
434distributions:
435
436 * Debian, Ubuntu, and derivatives::
437
438     sudo apt install bc binutils bison dwarves flex gcc git make openssl \
439       pahole perl-base libssl-dev libelf-dev
440
441 * Fedora and derivatives::
442
443     sudo dnf install binutils /usr/include/{libelf.h,openssl/pkcs7.h} \
444       /usr/bin/{bc,bison,flex,gcc,git,openssl,make,perl,pahole}
445
446 * openSUSE and derivatives::
447
448     sudo zypper install bc binutils bison dwarves flex gcc git make perl-base \
449       openssl openssl-devel libelf-dev
450
451In case you wonder why these lists include openssl and its development headers:
452they are needed for the Secure Boot support, which many distributions enable in
453their kernel configuration for x86 machines.
454
455Sometimes you will need tools for compression formats like bzip2, gzip, lz4,
456lzma, lzo, xz, or zstd as well.
457
458You might need additional libraries and their development headers in case you
459perform tasks not covered in this guide. For example, zlib will be needed when
460building kernel tools from the tools/ directory; adjusting the build
461configuration with make targets like 'menuconfig' or 'xconfig' will require
462development headers for ncurses or Qt5.
463
464[:ref:`back to step-by-step guide <buildrequires_sbs>`]
465
466.. _diskspace:
467
468Space requirements
469------------------
470
471   *Ensure to have enough free space for building and installing Linux.*
472   [:ref:`... <diskspace_sbs>`]
473
474The numbers mentioned are rough estimates with a big extra charge to be on the
475safe side, so often you will need less.
476
477If you have space constraints, remember to read the reference section when you
478reach the :ref:`section about configuration adjustments' <configmods>`, as
479ensuring debug symbols are disabled will reduce the consumed disk space by quite
480a few gigabytes.
481
482[:ref:`back to step-by-step guide <diskspace_sbs>`]
483
484
485.. _sources:
486
487Download the sources
488--------------------
489
490  *Retrieve the sources of the Linux version you intend to build.*
491  [:ref:`...<sources_sbs>`]
492
493The step-by-step guide outlines how to retrieve Linux' sources using a shallow
494git clone. There is :ref:`more to tell about this method<sources_shallow>` and
495two alternate ways worth describing: :ref:`packaged archives<sources_archive>`
496and :ref:`a full git clone<sources_full>`. And the aspects ':ref:`wouldn't it
497be wiser to use a proper pre-release than the latest mainline code
498<sources_snapshot>`' and ':ref:`how to get an even fresher mainline codebase
499<sources_fresher>`' need elaboration, too.
500
501Note, to keep things simple the commands used in this guide store the build
502artifacts in the source tree. If you prefer to separate them, simply add
503something like ``O=~/linux-builddir/`` to all make calls; also adjust the path
504in all commands that add files or modify any generated (like your '.config').
505
506[:ref:`back to step-by-step guide <sources_sbs>`]
507
508.. _sources_shallow:
509
510Noteworthy characteristics of shallow clones
511~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
512
513The step-by-step guide uses a shallow clone, as it is the best solution for most
514of this document's target audience. There are a few aspects of this approach
515worth mentioning:
516
517 * This document in most places uses ``git fetch`` with ``--shallow-exclude=``
518   to specify the earliest version you care about (or to be precise: its git
519   tag). You alternatively can use the parameter ``--shallow-since=`` to specify
520   an absolute (say ``'2023-07-15'``) or relative (``'12 months'``) date to
521   define the depth of the history you want to download. As a second
522   alternative, you can also specify a certain depth explicitly with a parameter
523   like ``--depth=1``, unless you add branches for stable/longterm kernels.
524
525 * When running ``git fetch``, remember to always specify the oldest version,
526   the time you care about, or an explicit depth as shown in the step-by-step
527   guide. Otherwise you will risk downloading nearly the entire git history,
528   which will consume quite a bit of time and bandwidth while also stressing the
529   servers.
530
531   Note, you do not have to use the same version or date all the time. But when
532   you change it over time, git will deepen or flatten the history to the
533   specified point. That allows you to retrieve versions you initially thought
534   you did not need -- or it will discard the sources of older versions, for
535   example in case you want to free up some disk space. The latter will happen
536   automatically when using ``--shallow-since=`` or
537   ``--depth=``.
538
539 * Be warned, when deepening your clone you might encounter an error like
540   'fatal: error in object: unshallow cafecaca0c0dacafecaca0c0dacafecaca0c0da'.
541   In that case run ``git repack -d`` and try again``
542
543 * In case you want to revert changes from a certain version (say Linux 6.3) or
544   perform a bisection (v6.2..v6.3), better tell ``git fetch`` to retrieve
545   objects up to three versions earlier (e.g. 6.0): ``git describe`` will then
546   be able to describe most commits just like it would in a full git clone.
547
548[:ref:`back to step-by-step guide <sources_sbs>`] [:ref:`back to section intro <sources>`]
549
550.. _sources_archive:
551
552Downloading the sources using a packages archive
553~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
554
555People new to compiling Linux often assume downloading an archive via the
556front-page of https://kernel.org is the best approach to retrieve Linux'
557sources. It actually can be, if you are certain to build just one particular
558kernel version without changing any code. Thing is: you might be sure this will
559be the case, but in practice it often will turn out to be a wrong assumption.
560
561That's because when reporting or debugging an issue developers will often ask to
562give another version a try. They also might suggest temporarily undoing a commit
563with ``git revert`` or might provide various patches to try. Sometimes reporters
564will also be asked to use ``git bisect`` to find the change causing a problem.
565These things rely on git or are a lot easier and quicker to handle with it.
566
567A shallow clone also does not add any significant overhead. For example, when
568you use ``git clone --depth=1`` to create a shallow clone of the latest mainline
569codebase git will only retrieve a little more data than downloading the latest
570mainline pre-release (aka 'rc') via the front-page of kernel.org would.
571
572A shallow clone therefore is often the better choice. If you nevertheless want
573to use a packaged source archive, download one via kernel.org; afterwards
574extract its content to some directory and change to the subdirectory created
575during extraction. The rest of the step-by-step guide will work just fine, apart
576from things that rely on git -- but this mainly concerns the section on
577successive builds of other versions.
578
579[:ref:`back to step-by-step guide <sources_sbs>`] [:ref:`back to section intro <sources>`]
580
581.. _sources_full:
582
583Downloading the sources using a full git clone
584~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
585
586If downloading and storing a lot of data (~4,4 Gigabyte as of early 2023) is
587nothing that bothers you, instead of a shallow clone perform a full git clone
588instead. You then will avoid the specialties mentioned above and will have all
589versions and individual commits at hand at any time::
590
591    curl -L \
592      https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/clone.bundle \
593      -o linux-stable.git.bundle
594    git clone linux-stable.git.bundle ~/linux/
595    rm linux-stable.git.bundle
596    cd ~/linux/
597    git remote set-url origin \
598      https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
599    git fetch origin
600    git checkout --detach origin/master
601
602[:ref:`back to step-by-step guide <sources_sbs>`] [:ref:`back to section intro <sources>`]
603
604.. _sources_snapshot:
605
606Proper pre-releases (RCs) vs. latest mainline
607~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
608
609When cloning the sources using git and checking out origin/master, you often
610will retrieve a codebase that is somewhere between the latest and the next
611release or pre-release. This almost always is the code you want when giving
612mainline a shot: pre-releases like v6.1-rc5 are in no way special, as they do
613not get any significant extra testing before being published.
614
615There is one exception: you might want to stick to the latest mainline release
616(say v6.1) before its successor's first pre-release (v6.2-rc1) is out. That is
617because compiler errors and other problems are more likely to occur during this
618time, as mainline then is in its 'merge window': a usually two week long phase,
619in which the bulk of the changes for the next release is merged.
620
621[:ref:`back to step-by-step guide <sources_sbs>`] [:ref:`back to section intro <sources>`]
622
623.. _sources_fresher:
624
625Avoiding the mainline lag
626~~~~~~~~~~~~~~~~~~~~~~~~~
627
628The explanations for both the shallow clone and the full clone both retrieve the
629code from the Linux stable git repository. That makes things simpler for this
630document's audience, as it allows easy access to both mainline and
631stable/longterm releases. This approach has just one downside:
632
633Changes merged into the mainline repository are only synced to the master branch
634of the Linux stable repository  every few hours. This lag most of the time is
635not something to worry about; but in case you really need the latest code, just
636add the mainline repo as additional remote and checkout the code from there::
637
638    git remote add mainline \
639      https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
640    git fetch mainline
641    git checkout --detach mainline/master
642
643When doing this with a shallow clone, remember to call ``git fetch`` with one
644of the parameters described earlier to limit the depth.
645
646[:ref:`back to step-by-step guide <sources_sbs>`] [:ref:`back to section intro <sources>`]
647
648.. _patching:
649
650Patch the sources (optional)
651----------------------------
652
653  *In case you want to apply a kernel patch, do so now.*
654  [:ref:`...<patching_sbs>`]
655
656This is the point where you might want to patch your kernel -- for example when
657a developer proposed a fix and asked you to check if it helps. The step-by-step
658guide already explains everything crucial here.
659
660[:ref:`back to step-by-step guide <patching_sbs>`]
661
662.. _tagging:
663
664Tagging this kernel build (optional, often wise)
665------------------------------------------------
666
667  *If you patched your kernel or already have that kernel version installed,
668  better tag your kernel by extending its release name:*
669  [:ref:`...<tagging_sbs>`]
670
671Tagging your kernel will help avoid confusion later, especially when you patched
672your kernel. Adding an individual tag will also ensure the kernel's image and
673its modules are installed in parallel to any existing kernels.
674
675There are various ways to add such a tag. The step-by-step guide realizes one by
676creating a 'localversion' file in your build directory from which the kernel
677build scripts will automatically pick up the tag. You can later change that file
678to use a different tag in subsequent builds or simply remove that file to dump
679the tag.
680
681[:ref:`back to step-by-step guide <tagging_sbs>`]
682
683.. _configuration:
684
685Define the build configuration for your kernel
686----------------------------------------------
687
688  *Create the build configuration for your kernel based on an existing
689  configuration.* [:ref:`... <configuration_sbs>`]
690
691There are various aspects for this steps that require a more careful
692explanation:
693
694Pitfalls when using another configuration file as base
695~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
696
697Make targets like localmodconfig and olddefconfig share a few common snares you
698want to be aware of:
699
700 * These targets will reuse a kernel build configuration in your build directory
701   (e.g. '~/linux/.config'), if one exists. In case you want to start from
702   scratch you thus need to delete it.
703
704 * The make targets try to find the configuration for your running kernel
705   automatically, but might choose poorly. A line like '# using defaults found
706   in /boot/config-6.0.7-250.fc36.x86_64' or 'using config:
707   '/boot/config-6.0.7-250.fc36.x86_64' tells you which file they picked. If
708   that is not the intended one, simply store it as '~/linux/.config'
709   before using these make targets.
710
711 * Unexpected things might happen if you try to use a config file prepared for
712   one kernel (say v6.0) on an older generation (say v5.15). In that case you
713   might want to use a configuration as base which your distribution utilized
714   when they used that or an slightly older kernel version.
715
716Influencing the configuration
717~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
718
719The make target olddefconfig and the ``yes "" |`` used when utilizing
720localmodconfig will set any undefined build options to their default value. This
721among others will disable many kernel features that were introduced after your
722base kernel was released.
723
724If you want to set these configurations options manually, use ``oldconfig``
725instead of ``olddefconfig`` or omit the ``yes "" |`` when utilizing
726localmodconfig. Then for each undefined configuration option you will be asked
727how to proceed. In case you are unsure what to answer, simply hit 'enter' to
728apply the default value.
729
730Big pitfall when using localmodconfig
731~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
732
733As explained briefly in the step-by-step guide already: with localmodconfig it
734can easily happen that your self-built kernel will lack modules for tasks you
735did not perform before utilizing this make target. That's because those tasks
736require kernel modules that are normally autoloaded when you perform that task
737for the first time; if you didn't perform that task at least once before using
738localmodconfig, the latter will thus assume these modules are superfluous and
739disable them.
740
741You can try to avoid this by performing typical tasks that often will autoload
742additional kernel modules: start a VM, establish VPN connections, loop-mount a
743CD/DVD ISO, mount network shares (CIFS, NFS, ...), and connect all external
744devices (2FA keys, headsets, webcams, ...) as well as storage devices with file
745systems you otherwise do not utilize (btrfs, ext4, FAT, NTFS, XFS, ...). But it
746is hard to think of everything that might be needed -- even kernel developers
747often forget one thing or another at this point.
748
749Do not let that risk bother you, especially when compiling a kernel only for
750testing purposes: everything typically crucial will be there. And if you forget
751something important you can turn on a missing feature later and quickly run the
752commands to compile and install a better kernel.
753
754But if you plan to build and use self-built kernels regularly, you might want to
755reduce the risk by recording which modules your system loads over the course of
756a few weeks. You can automate this with `modprobed-db
757<https://github.com/graysky2/modprobed-db>`_. Afterwards use ``LSMOD=<path>`` to
758point localmodconfig to the list of modules modprobed-db noticed being used::
759
760    yes "" | make LSMOD="${HOME}"/.config/modprobed.db localmodconfig
761
762Remote building with localmodconfig
763~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
764
765If you want to use localmodconfig to build a kernel for another machine, run
766``lsmod > lsmod_foo-machine`` on it and transfer that file to your build host.
767Now point the build scripts to the file like this: ``yes "" | make
768LSMOD=~/lsmod_foo-machine localmodconfig``. Note, in this case
769you likely want to copy a base kernel configuration from the other machine over
770as well and place it as .config in your build directory.
771
772[:ref:`back to step-by-step guide <configuration_sbs>`]
773
774.. _configmods:
775
776Adjust build configuration
777--------------------------
778
779   *Check if you might want to or have to adjust some kernel configuration
780   options:*
781
782Depending on your needs you at this point might want or have to adjust some
783kernel configuration options.
784
785.. _configmods_debugsymbols:
786
787Debug symbols
788~~~~~~~~~~~~~
789
790   *Evaluate how you want to handle debug symbols.*
791   [:ref:`...<configmods_sbs>`]
792
793Most users do not need to care about this, it's often fine to leave everything
794as it is; but you should take a closer look at this, if you might need to decode
795a stack trace or want to reduce space consumption.
796
797Having debug symbols available can be important when your kernel throws a
798'panic', 'Oops', 'warning', or 'BUG' later when running, as then you will be
799able to find the exact place where the problem occurred in the code. But
800collecting and embedding the needed debug information takes time and consumes
801quite a bit of space: in late 2022 the build artifacts for a typical x86 kernel
802configured with localmodconfig consumed around 5 Gigabyte of space with debug
803symbols, but less than 1 when they were disabled. The resulting kernel image and
804the modules are bigger as well, which increases load times.
805
806Hence, if you want a small kernel and are unlikely to decode a stack trace
807later, you might want to disable debug symbols to avoid above downsides::
808
809    ./scripts/config --file .config -d DEBUG_INFO \
810      -d DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT -d DEBUG_INFO_DWARF4 \
811      -d DEBUG_INFO_DWARF5 -e CONFIG_DEBUG_INFO_NONE
812    make olddefconfig
813
814You on the other hand definitely want to enable them, if there is a decent
815chance that you need to decode a stack trace later (as explained by 'Decode
816failure messages' in Documentation/admin-guide/tainted-kernels.rst in more
817detail)::
818
819    ./scripts/config --file .config -d DEBUG_INFO_NONE -e DEBUG_KERNEL
820      -e DEBUG_INFO -e DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT -e KALLSYMS -e KALLSYMS_ALL
821    make olddefconfig
822
823Note, many mainstream distributions enable debug symbols in their kernel
824configurations -- make targets like localmodconfig and olddefconfig thus will
825often pick that setting up.
826
827[:ref:`back to step-by-step guide <configmods_sbs>`]
828
829.. _configmods_distros:
830
831Distro specific adjustments
832~~~~~~~~~~~~~~~~~~~~~~~~~~~
833
834   *Are you running* [:ref:`... <configmods_sbs>`]
835
836The following sections help you to avoid build problems that are known to occur
837when following this guide on a few commodity distributions.
838
839**Debian:**
840
841 * Remove a stale reference to a certificate file that would cause your build to
842   fail::
843
844    ./scripts/config --file .config --set-str SYSTEM_TRUSTED_KEYS ''
845
846   Alternatively, download the needed certificate and make that configuration
847   option point to it, as `the Debian handbook explains in more detail
848   <https://debian-handbook.info/browse/stable/sect.kernel-compilation.html>`_
849   -- or generate your own, as explained in
850   Documentation/admin-guide/module-signing.rst.
851
852[:ref:`back to step-by-step guide <configmods_sbs>`]
853
854.. _configmods_individual:
855
856Individual adjustments
857~~~~~~~~~~~~~~~~~~~~~~
858
859   *If you want to influence the other aspects of the configuration, do so
860   now* [:ref:`... <configmods_sbs>`]
861
862You at this point can use a command like ``make menuconfig`` to enable or
863disable certain features using a text-based user interface; to use a graphical
864configuration utilize, use the make target ``xconfig`` or ``gconfig`` instead.
865All of them require development libraries from toolkits they are based on
866(ncurses, Qt5, Gtk2); an error message will tell you if something required is
867missing.
868
869[:ref:`back to step-by-step guide <configmods_sbs>`]
870
871.. _build:
872
873Build your kernel
874-----------------
875
876  *Build the image and the modules of your kernel* [:ref:`... <build_sbs>`]
877
878A lot can go wrong at this stage, but the instructions below will help you help
879yourself. Another subsection explains how to directly package your kernel up as
880deb, rpm or tar file.
881
882Dealing with build errors
883~~~~~~~~~~~~~~~~~~~~~~~~~
884
885When a build error occurs, it might be caused by some aspect of your machine's
886setup that often can be fixed quickly; other times though the problem lies in
887the code and can only be fixed by a developer. A close examination of the
888failure messages coupled with some research on the internet will often tell you
889which of the two it is. To perform such an investigation, restart the build
890process like this::
891
892    make V=1
893
894The ``V=1`` activates verbose output, which might be needed to see the actual
895error. To make it easier to spot, this command also omits the ``-j $(nproc
896--all)`` used earlier to utilize every CPU core in the system for the job -- but
897this parallelism also results in some clutter when failures occur.
898
899After a few seconds the build process should run into the error again. Now try
900to find the most crucial line describing the problem. Then search the internet
901for the most important and non-generic section of that line (say 4 to 8 words);
902avoid or remove anything that looks remotely system-specific, like your username
903or local path names like ``/home/username/linux/``. First try your regular
904internet search engine with that string, afterwards search Linux kernel mailing
905lists via `lore.kernel.org/all/ <https://lore.kernel.org/all/>`_.
906
907This most of the time will find something that will explain what is wrong; quite
908often one of the hits will provide a solution for your problem, too. If you
909do not find anything that matches your problem, try again from a different angle
910by modifying your search terms or using another line from the error messages.
911
912In the end, most trouble you are to run into has likely been encountered and
913reported by others already. That includes issues where the cause is not your
914system, but lies the code. If you run into one of those, you might thus find a
915solution (e.g. a patch) or workaround for your problem, too.
916
917Package your kernel up
918~~~~~~~~~~~~~~~~~~~~~~
919
920The step-by-step guide uses the default make targets (e.g. 'bzImage' and
921'modules' on x86) to build the image and the modules of your kernel, which later
922steps of the guide then install. You instead can also directly build everything
923and directly package it up by using one of the following targets:
924
925 * ``make -j $(nproc --all) bindeb-pkg`` to generate a deb package
926
927 * ``make -j $(nproc --all) binrpm-pkg`` to generate a rpm package
928
929 * ``make -j $(nproc --all) tarbz2-pkg`` to generate a bz2 compressed tarball
930
931This is just a selection of available make targets for this purpose, see
932``make help`` for others. You can also use these targets after running
933``make -j $(nproc --all)``, as they will pick up everything already built.
934
935If you employ the targets to generate deb or rpm packages, ignore the
936step-by-step guide's instructions on installing and removing your kernel;
937instead install and remove the packages using the package utility for the format
938(e.g. dpkg and rpm) or a package management utility build on top of them (apt,
939aptitude, dnf/yum, zypper, ...). Be aware that the packages generated using
940these two make targets are designed to work on various distributions utilizing
941those formats, they thus will sometimes behave differently than your
942distribution's kernel packages.
943
944[:ref:`back to step-by-step guide <build_sbs>`]
945
946.. _install:
947
948Install your kernel
949-------------------
950
951  *Now install your kernel* [:ref:`... <install_sbs>`]
952
953What you need to do after executing the command in the step-by-step guide
954depends on the existence and the implementation of an ``installkernel``
955executable. Many commodity Linux distributions ship such a kernel installer in
956``/sbin/`` that does everything needed, hence there is nothing left for you
957except rebooting. But some distributions contain an installkernel that does
958only part of the job -- and a few lack it completely and leave all the work to
959you.
960
961If ``installkernel`` is found, the kernel's build system will delegate the
962actual installation of your kernel's image and related files to this executable.
963On almost all Linux distributions it will store the image as '/boot/vmlinuz-
964<your kernel's release name>' and put a 'System.map-<your kernel's release
965name>' alongside it. Your kernel will thus be installed in parallel to any
966existing ones, unless you already have one with exactly the same release name.
967
968Installkernel on many distributions will afterwards generate an 'initramfs'
969(often also called 'initrd'), which commodity distributions rely on for booting;
970hence be sure to keep the order of the two make targets used in the step-by-step
971guide, as things will go sideways if you install your kernel's image before its
972modules. Often installkernel will then add your kernel to the bootloader
973configuration, too. You have to take care of one or both of these tasks
974yourself, if your distributions installkernel doesn't handle them.
975
976A few distributions like Arch Linux and its derivatives totally lack an
977installkernel executable. On those just install the modules using the kernel's
978build system and then install the image and the System.map file manually::
979
980     sudo make modules_install
981     sudo install -m 0600 $(make -s image_name) /boot/vmlinuz-$(make -s kernelrelease)
982     sudo install -m 0600 System.map /boot/System.map-$(make -s kernelrelease)
983
984If your distribution boots with the help of an initramfs, now generate one for
985your kernel using the tools your distribution provides for this process.
986Afterwards add your kernel to your bootloader configuration and reboot.
987
988[:ref:`back to step-by-step guide <install_sbs>`]
989
990.. _another:
991
992Another round later
993-------------------
994
995  *To later build another kernel you need similar, but sometimes slightly
996  different commands* [:ref:`... <another_sbs>`]
997
998The process to build later kernels is similar, but at some points slightly
999different. You for example do not want to use 'localmodconfig' for succeeding
1000kernel builds, as you already created a trimmed down configuration you want to
1001use from now on. Hence instead just use ``oldconfig`` or ``olddefconfig`` to
1002adjust your build configurations to the needs of the kernel version you are
1003about to build.
1004
1005If you created a shallow-clone with git, remember what the :ref:`section that
1006explained the setup described in more detail <sources>`: you need to use a
1007slightly different ``git fetch`` command and when switching to another series
1008need to add an additional remote branch.
1009
1010[:ref:`back to step-by-step guide <another_sbs>`]
1011
1012.. _uninstall:
1013
1014Uninstall the kernel later
1015--------------------------
1016
1017  *All parts of your installed kernel are identifiable by its release name and
1018  thus easy to remove later.* [:ref:`... <uninstall_sbs>`]
1019
1020Do not worry installing your kernel manually and thus bypassing your
1021distribution's packaging system will totally mess up your machine: all parts of
1022your kernel are easy to remove later, as files are stored in two places only and
1023normally identifiable by the kernel's release name.
1024
1025One of the two places is a directory in /lib/modules/, which holds the modules
1026for each installed kernel. This directory is named after the kernel's release
1027name; hence, to remove all modules for one of your kernels, simply remove its
1028modules directory in /lib/modules/.
1029
1030The other place is /boot/, where typically one to five files will be placed
1031during installation of a kernel. All of them usually contain the release name in
1032their file name, but how many files and their name depends somewhat on your
1033distribution's installkernel executable (:ref:`see above <install>`) and its
1034initramfs generator. On some distributions the ``kernel-install`` command
1035mentioned in the step-by-step guide will remove all of these files for you --
1036and the entry for your kernel in the bootloader configuration at the same time,
1037too. On others you have to take care of these steps yourself. The following
1038command should interactively remove the two main files of a kernel with the
1039release name '6.0.1-foobar'::
1040
1041    rm -i /boot/{System.map,vmlinuz}-6.0.1-foobar
1042
1043Now remove the belonging initramfs, which often will be called something like
1044``/boot/initramfs-6.0.1-foobar.img`` or ``/boot/initrd.img-6.0.1-foobar``.
1045Afterwards check for other files in /boot/ that have '6.0.1-foobar' in their
1046name and delete them as well. Now remove the kernel from your bootloader's
1047configuration.
1048
1049Note, be very careful with wildcards like '*' when deleting files or directories
1050for kernels manually: you might accidentally remove files of a 6.0.11 kernel
1051when all you want is to remove 6.0 or 6.0.1.
1052
1053[:ref:`back to step-by-step guide <uninstall_sbs>`]
1054
1055.. _faq:
1056
1057FAQ
1058===
1059
1060Why does this 'how-to' not work on my system?
1061---------------------------------------------
1062
1063As initially stated, this guide is 'designed to cover everything typically
1064needed [to build a kernel] on mainstream Linux distributions running on
1065commodity PC or server hardware'. The outlined approach despite this should work
1066on many other setups as well. But trying to cover every possible use-case in one
1067guide would defeat its purpose, as without such a focus you would need dozens or
1068hundreds of constructs along the lines of 'in case you are having <insert
1069machine or distro>, you at this point have to do <this and that>
1070<instead|additionally>'. Each of which would make the text longer, more
1071complicated, and harder to follow.
1072
1073That being said: this of course is a balancing act. Hence, if you think an
1074additional use-case is worth describing, suggest it to the maintainers of this
1075document, as :ref:`described above <submit_improvements_qbtl>`.
1076
1077
1078..
1079   end-of-content
1080..
1081   This document is maintained by Thorsten Leemhuis <linux@leemhuis.info>. If
1082   you spot a typo or small mistake, feel free to let him know directly and
1083   he'll fix it. You are free to do the same in a mostly informal way if you
1084   want to contribute changes to the text -- but for copyright reasons please CC
1085   linux-doc@vger.kernel.org and 'sign-off' your contribution as
1086   Documentation/process/submitting-patches.rst explains in the section 'Sign
1087   your work - the Developer's Certificate of Origin'.
1088..
1089   This text is available under GPL-2.0+ or CC-BY-4.0, as stated at the top
1090   of the file. If you want to distribute this text under CC-BY-4.0 only,
1091   please use 'The Linux kernel development community' for author attribution
1092   and link this as source:
1093   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/plain/Documentation/admin-guide/quickly-build-trimmed-linux.rst
1094..
1095   Note: Only the content of this RST file as found in the Linux kernel sources
1096   is available under CC-BY-4.0, as versions of this text that were processed
1097   (for example by the kernel's build system) might contain content taken from
1098   files which use a more restrictive license.
1099
1100