Building executable for Alpine linux


I’m building a Rust executable which has to run on an Alpine Linux distribution.

Unfortunately, the Linux build (which we run on Ubuntu) has:

$ ldd lodex =>  (0x00007ffff11a8000) => /lib/x86_64-linux-gnu/ (0x00007f4a483a9000) => /lib/x86_64-linux-gnu/ (0x00007f4a481a1000) => /lib/x86_64-linux-gnu/ (0x00007f4a47f83000) => /lib/x86_64-linux-gnu/ (0x00007f4a47d6c000) => /lib/x86_64-linux-gnu/ (0x00007f4a479a5000)
    /lib64/ (0x0000558610a7e000)

and this fails to run on the (virtual) machine we want to deploy this to, which uses Alpine Linux.

How can I either build a Linux executable which doesn’t load anything else, or target the Alpine Linux distribution properly?

I have looked at the linkage section of the Rust manuals and it seems I cannot control the system libraries which are dynamically linked by this means.


You can cross compile to the x86_64-unknown-linux-musl target:

$ rustup target add x86_64-unknown-linux-musl
$ cargo build --target x86_64-unknown-linux-musl

If your crate or its dependencies depend on C libraries, you may need to cross compile them with musl-gcc.


I’ve installed the target on my Mac, but the targeted build fails because it cannot be linkedited (cc complains about the parameters it is given).

I guess I need a toolchain on my Mac to link for musl. This is tricky, so instead I’m going to build a targeted executable on our (Ubuntu) build machine. I need to extend the Rust currently installed there, and archive the (new) build from target/release/x86_64-unknown-linux-musl/ instead of target/release/.

Wish me luck.

PS: It is implies (elsewhere on the forum) that this will give me a statically linked binary that runs on any linux. Is this true?


Yep, you’ll end up with a fully static binary.

Here’s an example of a musl build for reference:


And I did end up with one. Brill. Thank you.