How to Cross-Compile Rust for Linux from macOS or Windows

Install the Linux target with rustup and build your Rust project using the --target flag to cross-compile from macOS or Windows.

Use rustup to install the target toolchain and then compile with the --target flag.

  1. Install the target toolchain for your desired Linux architecture.
rustup target add x86_64-unknown-linux-gnu
  1. Compile your project for the Linux target using Cargo.
cargo build --release --target x86_64-unknown-linux-gnu

The resulting binary will be in target/x86_64-unknown-linux-gnu/release/.