How to cross compile from Mac to Linux?

Well, it is because Rust has no runtime (unlike e.g. Java's JVM) that you can't just compile code on one OS and expect it to run on a different one; what you are looking for is cross-compilation. I haven't done it in Rust, but from what I can gather you can find relevant information on different cross-compilation Rust strategies on this GitHub repo.


Rust not having a runtime means that it doesn't have a lot of code running as part of the language (for example a garbage collector or bytecode interpreter). It does still need to use operating system primitives (i.e. syscalls), and these are different on MacOS and Linux.

What you want is a cross compiler. If you're using rustup, then installing a cross compiler should be simple:

# Install the toolchain to build Linux x86_64 binaries
rustup target add x86_64-unknown-linux-gnu

Then building is:

cargo build --release --target=x86_64-unknown-linux-gnu

Caveat: I don't have an OS X machine to test this on; please comment or edit to fix this if it works!