Skip to content

Install

An eBPF tool built with gobpfgo has two halves: a BPF program in C, which clang compiles into an object file, and a Go program, which loads that object into the kernel. This page sets up both halves and checks that your kernel can run the tutorial.

  • Linux 5.10 or newer, on amd64 (x86-64) or arm64. gobpfgo is tested on six kernels from 5.10 to 7.4-rc4. Newer features need newer kernels; see the support table linked below.
  • Root, or the CAP_BPF and CAP_PERFMON capabilities, to load and attach tracing programs. Networking programs need CAP_NET_ADMIN instead of CAP_PERFMON. The tutorial runs its programs with sudo.
  • clang with the BPF target, and the LLVM tools that come with it, to compile the C side. clang -print-targets lists bpf when the target is there. The examples are tested with clang 23.
  • The libbpf headers, for the C side only: bpf_helpers.h and the linux/bpf.h UAPI header. gobpfgo does not ship BPF-side headers. The examples build against a libbpf source tree.
  • Go 1.27 or newer, and make.

The Go side needs no C toolchain and no libbpf library. clang and the headers are only needed where you compile the BPF objects.

  1. Clone gobpfgo and libbpf into the same directory. The examples’ Makefile looks for libbpf next to the gobpfgo checkout.

    Terminal window
    git clone https://github.com/gobpfgo/gobpfgo
    git clone https://github.com/libbpf/libbpf
  2. Check the Go version.

    Terminal window
    go version

    It must print go1.27 or newer.

  3. Build the examples’ Go code. The examples are their own Go module, which uses the gobpfgo checkout one directory up.

    Terminal window
    cd gobpfgo/examples
    go build ./...

    The command prints nothing when it succeeds.

In your own project, add gobpfgo as a dependency instead:

Terminal window
go get github.com/gobpfgo/gobpfgo

Application code imports github.com/gobpfgo/gobpfgo/bpf; the libbpf core is github.com/gobpfgo/gobpfgo/libbpf.

From the examples directory, make compiles every tutorial/*/*.bpf.c into a .bpf.o next to it:

Terminal window
make

On an x86-64 host it runs:

clang -O2 -g -target bpf -D__TARGET_ARCH_x86 \
-I../../libbpf/include/uapi -I../../libbpf/src \
-c tutorial/step01/open_count.bpf.c -o tutorial/step01/open_count.bpf.o

Each flag has a job:

  • -target bpf makes clang emit BPF bytecode instead of code for the host CPU, in the host’s byte order.
  • -O2 optimizes the code. Build BPF objects with it: the examples are tested at -O2, and an unoptimized build of even the first program carries BTF that the kernel refuses.
  • -g makes clang write BTF, the type information described in What is eBPF, and the line information the verifier puts in its log.
  • -D__TARGET_ARCH_x86 tells libbpf’s bpf_tracing.h which CPU’s registers hold function arguments. The Makefile sets x86 or arm64 from uname -m. The first program does not need it; the kprobe chapter does.
  • -I../../libbpf/include/uapi finds linux/bpf.h in libbpf’s copy of the kernel UAPI headers, and -I../../libbpf/src finds bpf_helpers.h.

If libbpf is somewhere else, point the Makefile at it:

Terminal window
make LIBBPF=/path/to/libbpf

To use your distribution’s libbpf development package instead of a source tree (libbpf-dev on Debian and Ubuntu, libbpf-devel on Fedora, libbpf on Arch), add its header directory to the compiler command:

Terminal window
make CLANG="clang -I/usr/include/bpf"

On Debian and Ubuntu, if clang stops with fatal error: 'asm/types.h' file not found, the kernel headers keep asm/ in a per-architecture directory that clang does not search for the BPF target. Add it the same way:

Terminal window
make CLANG="clang -I/usr/include/$(uname -m)-linux-gnu"

make clean removes the built objects.

The first program needs a tracepoint and an array map, which every kernel from 5.10 supports. Later chapters need more, so check these now:

Terminal window
uname -r
ls /sys/kernel/btf/vmlinux
sudo ls /sys/kernel/tracing/events/syscalls/sys_enter_openat
  • uname -r prints the kernel release. It must be 5.10 or newer. Distribution kernels often backport BPF features, so an older-looking version can still support a feature; gobpfgo probes the kernel at load either way.
  • /sys/kernel/btf/vmlinux exists when the kernel was built with BTF. See the second permission note below.
  • The last command lists the tracepoint the first program attaches to: enable, filter, format, id and more. tracefs is mounted at /sys/kernel/tracing and is usually readable only by root.

If bpftool is installed, sudo bpftool feature probe kernel lists the program types, map types and helpers the running kernel supports.

The minimum kernel for each feature, on amd64 and on arm64, the kernel configuration options and the kernels the test suites run on are listed in Kernel support.

bpf() needs privilege. Creating maps, loading programs and attaching them are privileged operations. Run your tool as root, or give it CAP_BPF and CAP_PERFMON for tracing programs, or CAP_BPF and CAP_NET_ADMIN for networking programs. Without them, the load fails with EPERM. In the tutorial, the kernel first refuses the object’s BTF, which is optional, so the load goes on, and then refuses to create the map:

libbpf: Error loading .BTF into kernel: -EPERM. BTF is optional, ignoring.
step01: bpf_map_create: operation not permitted: counts

Opening an object, linking objects and reading BTF need no privilege. sudo resets PATH, so the tutorial runs sudo env PATH="$PATH" go run . to let root find your go command.

BTF-aware hooks need a kernel built with BTF. fentry, fexit, LSM, struct_ops, iterator and tracing multi programs read the kernel’s types from /sys/kernel/btf/vmlinux, which exists only when the kernel was built with CONFIG_DEBUG_INFO_BTF=y. CO-RE relocations need it too, unless the open options point at a BTF file for the target kernel. Tracepoints, kprobes, uprobes, XDP and tc programs without CO-RE work without kernel BTF, so the first program runs either way. Most current distribution kernels enable BTF.

gobpfgo does not provide skeleton generation, big-endian linker input, split-BTF dedup, fixed-address arenas or perf_buffer__new_raw. The reasons, and every intentional difference from libbpf, are in Differences from libbpf.