#Building perf from source

The linux kernel tool perf is a great cpu performance profiler. On ubuntu it is usually easily installed from the apt repository. It has to match the kernel version, so for each official kernel version of the distribution, there is (should be?) an official package for perf.

To install the right version, simply let uname fill in the version part of the package name:

sudo apt install linux-tools-$(uname -r)

which may end up installing linux-tools-5.3.0-28-generic or something similar.

If you are in the unfortunate position that you have a custom kernel, for which there is no matching linux-tools-* package, you need to download the kernel’s source and build perf yourself (you may also succeed with installing the version of linux-tools-xxx that is most similar to your kernel, but who knows).

For example to fetch the source for kernel version 4.14 from the kernel’s repository you can clone it via git as such:

git clone -b linux-4.14.y https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git linux-4.14.y

where -b specifies the branch and the last parameter is the directory name.

Then install some dependencies of perf:

sudo apt install flex bison libelf-dev systemtap-sdt-dev libaudit-dev libslang2-dev libperl-dev libdw-dev

With that done, go to linux-4.14.y/tools/perf and execute

make

That should build perf. You should find the executable perf directly in the current directory.



Home