Build perf From Kernel Source to Match Your Running Kernel
Build the perf tool from the Linux source tree with make -C tools/perf, check the feature report, and install it so its version matches the kernel you run.

Most of the time apt install linux-tools-$(uname -r) is the right answer. It fails when your kernel is a custom build, a vendor kernel with no matching tools package, or a release so new the distribution has not packaged its perf yet. The tool lives inside the kernel repository under tools/perf, so you can build the exact version that goes with your kernel. This post does that on Debian 12 with kernel 6.12 sources and GCC 12, and extends installing perf and profiling your first program.
Why the versions should match
perf and the kernel agree on a binary interface, perf_event_open(2), and on the layout of the events the kernel writes to the ring buffer. The interface is stable for old tools, but new kernel features appear only in a new tool: new event names, new sample fields, new PMU descriptions. Running a much older perf on a new kernel works for basics and silently lacks the newer events. Running a much newer perf on an old kernel asks for attributes the kernel rejects with EINVAL. Building from the same tree sidesteps both.
Get the source
Either fetch the release tarball for your exact kernel or clone the stable tree. The tarball is smaller and needs no git history:
uname -r
curl -LO https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.12.tar.xz
tar -xf linux-6.12.tar.xz
cd linux-6.12/tools/perf6.12.0-customThe first line is your running kernel as uname -r reports it. The numbers 6.12 in the filename are the upstream release your kernel was built from; a distribution kernel may add a suffix and patches, in which case the closest upstream tarball is still a good match for perf purposes. Kernel.org publishes the tarballs under cdn.kernel.org/pub/linux/kernel/.
Install build dependencies
perf builds with whatever optional libraries it can find, and leaves features out when they are missing. The useful ones for profiling are libelf, libdw (for DWARF unwinding), libunwind-free alternatives, libtraceevent, libperl and Python for scripting, and libslang for the TUI. On Debian and Ubuntu:
sudo apt install build-essential flex bison pkg-config \
libelf-dev libdw-dev libslang2-dev libtraceevent-dev \
libcap-dev libnuma-dev libpython3-dev libperl-dev \
libzstd-dev libssl-dev libiberty-dev binutils-devThe list is long because each library switches on a capability. libdw-dev enables --call-graph dwarf and symbol resolution from debug information; without it you get a tool that records fine and cannot unwind. libslang2-dev gives the interactive report. libcap-dev lets perf check CAP_PERFMON itself.
Build and read the feature report
From inside tools/perf:
make -j"$(nproc)"The first thing make prints is a feature detection summary. Read it, because it is the only place that tells you what was left out:
Auto-detecting system features:
... dwarf: [ on ]
... dwarf_getlocations: [ on ]
... glibc: [ on ]
... libbfd: [ on ]
... libelf: [ on ]
... libnuma: [ on ]
... libperl: [ on ]
... libpython: [ on ]
... libslang: [ on ]
... libtraceevent: [ on ]
... zstd: [ on ]Your list will be longer and the exact set varies with release. Any line showing [ OFF ] for a feature you need is a missing library. Install it, then rebuild from clean, because the detection results are cached in the build directory:
make clean
make -j"$(nproc)"The binary lands in the current directory:
./perf --versionperf version 6.12.0If the version carries a longer suffix, that is the kernel's EXTRAVERSION rather than a problem.
Install it
Install under your home directory to avoid disturbing the distribution's own tool:
make prefix="$HOME/.local" install INSTALL binaries
INSTALL tests
INSTALL libperf-jvmti.so
INSTALL perf-archive
INSTALL DocumentationThe program goes to $HOME/.local/bin/perf. Make sure that directory comes first in PATH, and check which perf the shell will run:
export PATH="$HOME/.local/bin:$PATH"
which perf
perf --version/home/dev/.local/bin/perf
perf version 6.12.0A prefix install keeps the build self-contained. A system-wide sudo make install writes into /usr/local by default and is also fine; the point of prefix is that removing it later is a single directory.
Check the build with a real run
Confirm that the build supports what you intended:
perf list | head -5
perf record -F 999 --call-graph dwarf -- ls > /dev/null
perf report --stdio | head -12List of pre-defined events (to list event groups use -v):
branch-instructions OR branches [Hardware event]
branch-misses [Hardware event]
bus-cycles [Hardware event]
cache-misses [Hardware event]If --call-graph dwarf errors with a message that DWARF support was not built in, libdw-dev was missing at build time. If perf report prints plain text instead of opening a TUI, you ran with --stdio, which is expected here.
Cross-building and containers
You can build perf on one machine for another with make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-, but the optional libraries then have to exist for the target architecture, which is why many cross builds are missing features. For a container host, build once and copy the binary and the libperf-jvmti.so; the tool needs only the libraries it linked against.
When not to bother
If your distribution's package matches your kernel, use it: it gets security updates and needs no maintenance. Build from source when there is no package, when you need an event added in a newer release, or when you test a patched kernel. Do not build a much newer perf than your kernel to get features; the new events need the new kernel anyway.
Troubleshooting the build
Build failures are almost always one of three things. A header error about libelf.h or gelf.h means libelf-dev is missing, and without it the tool will not build at all. A compile error on a very new GCC or Clang release usually means the kernel source is older than the compiler; the tree's tools/perf is built with the warning flags the kernel developers use, and a newer compiler can add warnings that the older source treats as errors. You can pass WERROR=0 to make to turn warnings back into warnings for a personal build. A runtime error when the tool starts, such as a missing libtraceevent.so, means you installed it under a prefix the loader does not search; set LD_LIBRARY_PATH or install the distribution's development package.
Two more flags are useful. make NO_LIBPYTHON=1 NO_LIBPERL=1 skips the scripting engines, which cuts build time and dependencies if you never run perf script with a script language. make DEBUG=1 builds without optimization so you can step through the tool with a debugger, which you will want only if you are fixing perf itself. Both are documented in the comments at the top of tools/perf/Makefile.perf, which is the authoritative list of switches for the release you downloaded.
Keep the feature summary from the first build. When a later report behaves oddly, such as a missing call graph or no disassembly, comparing against that summary tells you immediately whether the capability was ever compiled in, which saves reading the whole build log.
Check the result against your kernel
After the build, compare the tool and kernel versions side by side once, and write the pair down next to your profiling notes. A mismatch of more than a couple of releases is where odd failures start, and the check takes two commands. Also run perf list and confirm that the hardware events your analysis needs appear, since those depend on the processor and the kernel rather than on how the tool was built.


