MUSL support for fully static binaries

Minimum Rust version: 1.1

By default, Rust will statically link all Rust code. However, if you use thestandard library, it will dynamically link to the system's libcimplementation.

If you'd like a 100% static binary, the MUSL libc can be used on Linux.

Installing MUSL support

To add support for MUSL, you need to choose the correct target. The forgehas a full list oftargets supported,with a number of ones using musl.

If you're not sure what you want, it's probably x86_64-unknown-linux-musl,for 64-bit Linux. We'll be using this target in this guide, but theinstructions remain the same for other targets, just change the name whereverwe mention the target.

To get support for this target, you use rustup:

  1. $ rustup target add x86_64-unknown-linux-musl

This will install support for the default toolchain; to install for other toolchains,add the —toolchain flag. For example:

  1. $ rustup target add x86_64-unknown-linux-musl --toolchain=nightly

Building with MUSL

To use this new target, pass the —target flag to Cargo:

  1. $ cargo build --target x86_64-unknown-linux-musl

The binary produced will now be built with MUSL!