Skip to content

Instantly share code, notes, and snippets.

@polachok
Last active July 7, 2023 13:50
Show Gist options
  • Save polachok/ab0937985d57b0398ad8 to your computer and use it in GitHub Desktop.
Save polachok/ab0937985d57b0398ad8 to your computer and use it in GitHub Desktop.
Building statically linked binaries with rust

glibc is not statically linkable (because reasons, mainly nss), so you have to link against another libc. Fortunately, rust developers made binary builds of std linked with musl libc available in beta and nightly channels. Which means that all you need is:

  • std linked with musl

    • http://static.rust-lang.org/dist/$(date -v -1d +'%Y-%m-%d')/rust-std-beta-x86_64-unknown-linux-musl.tar.gz
  • "normal" std for build scripts

    • http://static.rust-lang.org/dist/$(date -v -1d +'%Y-%m-%d')/rust-std-beta-x86_64-unknown-linux-gnu.tar.gz
  • rustc

    • http://static.rust-lang.org/dist/$(date -v -1d +'%Y-%m-%d')/rustc-beta-x86_64-unknown-linux-gnu.tar.gz
  • cargo

    • http://static.rust-lang.org/cargo-dist/cargo-nightly-x86_64-unknown-linux-gnu.tar.gz

Unpack everything and run ./install.sh, it will set up things for you.

Now you can build your crate with: cargo build --target=x86_64-unknown-linux-musl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment