Add the musl target and build with the --target flag to create a fully static Linux binary.
To target musl for fully static Linux binaries, add the x86_64-unknown-linux-musl target to your toolchain and compile with the --target flag.
rustup target add x86_64-unknown-linux-musl
cargo build --release --target x86_64-unknown-linux-musl
This produces a statically linked binary in target/x86_64-unknown-linux-musl/release/ that runs on any Linux distribution without external dependencies.
Targeting musl for fully static Linux binaries compiles your Rust code into a single file that includes everything it needs to run, rather than relying on the operating system's libraries. Think of it like packing a suitcase with every item you might need for a trip, so you don't have to buy anything once you arrive at your destination. This ensures your program works on any Linux computer, regardless of its specific version or installed software.