Skip to content
systemcoding.dev
Kernels

Set Up a Rust for Linux Development Environment

Install the Rust toolchain, bindgen and libclang, check them with make rustavailable, enable CONFIG_RUST in a Linux 6.18 tree and build the Rust samples.

7 min read
Summarize in AI
Flat illustration of settings switches, a slider and a gear, with the Rust, Linux and CMake logos.

Rust kernel code does not build with a plain cargo build. The kernel compiles core itself for the kernel target, generates Rust declarations from its own C headers, and links the result with Clang's linker setup, so it needs a specific set of tools in specific versions. Most failed first attempts are a missing component or a version one step too old, and the kernel has a check that tells you which. This post sets up a working environment for a Linux 6.18 tree: choosing a toolchain source, installing the pieces, verifying them with make rustavailable, turning on CONFIG_RUST, building the samples, and adding the editor and lint tooling. Once it works, the next step is to write your first Rust kernel module.

The steps follow the kernel's Rust quick start and the requirements table in Documentation/process/changes.rst.

What you need

The build uses six things. A C compiler and linker as for any kernel build, with the LLVM toolchain recommended: the documentation says building with a complete LLVM toolchain is the best supported setup, and using GCC works for some configurations but is very experimental. The Rust compiler rustc, at least version 1.78.0 for 6.18. The Rust standard library source, rust-src, because the build cross-compiles core for the kernel rather than using a prebuilt one. bindgen at least 0.65.1, which generates the Rust view of kernel C headers. libclang, which bindgen uses to parse those headers. And optionally rustfmt, clippy, rustdoc and rust-analyzer for formatting, linting, documentation and editor support.

Two things to know before you start. The required rustc is a minimum, and the newest stable release is usually the safest, because the kernel crate uses a list of unstable language features that newer compilers have stabilized, and the source says so in comments such as "stable since Rust 1.83.0". And bindgen and rustc should use the same LLVM major version where possible, which is one reason the documentation suggests getting both from your distribution.

Pick one source for the toolchain

There are three ways to get the tools, and mixing them is the main source of trouble.

The first is your distribution's packages, which the documentation lists for Arch, Debian, Fedora, Gentoo, Nix, openSUSE and Ubuntu. Fedora is a typical example, where the packages carry Rust, the standard library source, bindgen and the lints in one command:

Shell
dnf install rust rust-src bindgen-cli rustfmt clippy

On Debian testing or unstable the equivalent is apt install rustc rust-src bindgen rustfmt rust-clippy. On Ubuntu releases that ship an older Rust, the quick start explains how to use versioned packages and then set RUSTC, BINDGEN and related make variables to the versioned tools. An advantage of distribution packages, in the documentation's words, is that the distribution will typically match the LLVM used by Rust and Clang.

The second is the prebuilt LLVM-plus-Rust bundles on kernel.org, which are the same slim LLVM toolchains used for kernel builds with a Rust version the kernel supports added. The third is rustup for the compiler, plus cargo install --locked bindgen-cli for bindgen. With rustup, run these in the kernel tree so the override applies only there:

Shell
rustup override set stable
rustup component add rust-src

The components are installed per toolchain, so upgrading the compiler later means adding rust-src again. Note that cargo install downloads and builds bindgen from source, and by default it loads libclang at runtime; if it cannot find one, set LIBCLANG_PATH.

Check the toolchain with rustavailable

Do not start a long kernel build to find out whether your tools work. Run the target the kernel provides:

Shell
make LLVM=1 rustavailable
Text
Rust is available!

On success it prints that line. On failure it prints a block explaining which tool is missing or too old, for instance that the Rust compiler could not be found, or that the bindgen version is below the minimum, and then points at the quick start document. The same logic runs inside Kconfig to decide whether to offer CONFIG_RUST at all, so if the target fails, the option will not appear in menuconfig. Fix whatever it reports and run it again until it prints the line above.

Enable Rust support in the configuration

CONFIG_RUST lives in the General setup menu, shown only if the toolchain check passes. Start from a default configuration for your architecture, then switch on Rust and the samples. The kernel's scripts/config helper edits .config without the menu:

Shell
make LLVM=1 defconfig
./scripts/config -e RUST -e SAMPLES_RUST -m SAMPLE_RUST_MINIMAL
make LLVM=1 olddefconfig
grep -E '^CONFIG_(RUST|SAMPLES_RUST|SAMPLE_RUST_MINIMAL)=' .config
Text
CONFIG_RUST=y
CONFIG_SAMPLES_RUST=y
CONFIG_SAMPLE_RUST_MINIMAL=m

-e enables as built-in and -m as a module. olddefconfig resolves dependencies and writes the final file. If CONFIG_RUST is missing from the output, one of its dependencies is not met. The RUST entry in init/Kconfig lists them, among them that the architecture supports Rust, the toolchain is available, MODVERSIONS is off unless GENDWARFKSYMS is on, and structure randomization is off. Run make LLVM=1 menuconfig and press / to search for RUST to see the unmet dependency.

The quick start puts the samples under Kernel hacking, then Sample kernel code, then Rust samples. There is also a Rust hacking menu under Kernel hacking with CONFIG_RUST_DEBUG_ASSERTIONS, CONFIG_RUST_OVERFLOW_CHECKS (on by default) and CONFIG_RUST_KERNEL_DOCTESTS.

Build

Build with the same LLVM=1 and the same options you used for the configuration, every time, because mixing toolchains between invocations invalidates generated files:

Shell
make LLVM=1 -j"$(nproc)"

The first build takes a long time, as any kernel build does, and compiles core, the kernel crate and the samples. Rust objects appear with RUSTC in the build log lines. When it finishes, the minimal sample is at samples/rust/rust_minimal.ko. You can load it in a test VM or on a machine running this kernel:

Shell
insmod samples/rust/rust_minimal.ko
rmmod rust_minimal
dmesg | tail -n 4
Text
rust_minimal: Rust minimal sample (init)
rust_minimal: Am I built-in? false
rust_minimal: My numbers are [72, 108, 200]
rust_minimal: Rust minimal sample (exit)

The first two lines are printed from init, and the last two from the Drop implementation when the module is removed. Your dmesg has timestamps in front. The false shows it was built as a module rather than into the kernel. Do this on a test machine or a VM, since loading any module has risk. Loading it on a different kernel build than the one the module was compiled against will be refused.

Editor and lint tooling

rust-analyzer needs a project description file because the kernel does not use Cargo. Generate it with the make target and place it at the tree root:

Shell
make LLVM=1 rust-analyzer

That writes rust-project.json, which editors that support rust-analyzer read to give completion and go-to-definition across the kernel crate. Format with make LLVM=1 rustfmt and check formatting with make LLVM=1 rustfmtcheck. For lints, add CLIPPY=1 to the same build command; the documentation warns that Clippy may change code generation, so do not use it for the kernel you ship. And build the rendered API documentation for your configuration with:

Shell
make LLVM=1 rustdoc

The HTML ends up under Documentation/output/rust/rustdoc/kernel/index.html, and reading it is the best way to learn what abstractions exist in your tree, because it is generated from the exact sources you have.

Common failures

If rustavailable says bindgen cannot find libclang, set LIBCLANG_PATH or install the libclang package. If it complains the standard library source is missing, add rust-src to the same toolchain the build uses. If the build stops at the core crate, the compiler is likely too old or too new for this tree; check rustc --version against the minimum and against the unstable features the kernel crate enables. If symbols in GDB show mangled Rust names, the quick start says to use GDB 10.2 or newer, or Binutils 2.36 or newer, for Rust's v0 symbol mangling.

Where this does not fit

This is a setup for building Rust in-tree on a supported architecture. It does not make Rust available on an architecture the kernel does not support, such as 32-bit x86, and it does not give you a way to compile a kernel for another architecture without a cross toolchain for both C and Rust. For an out-of-tree Rust module, the CONFIG_RUST help text says the option is also what allows loading external modules written in Rust, but the kernel build and the module must use the same toolchain versions.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.