Cross-Compiling Rust for Raspberry Pi via WSL2 on Windows
Step-by-step guide to using WSL2 (Linux) on Windows to cross-compile Rust for ARM64 Raspberry Pi targets. Covers toolchain setup, Rust installation, cargo linker configuration, build commands, and deployment to the Pi.
The cleanest way to cross-compile Rust for ARM64 Raspberry Pi from Windows is to build inside WSL2 and target aarch64-unknown-linux-gnu. You get a real Linux toolchain that matches Raspberry Pi OS, and you avoid the brittle MSVC to GNU cross-linker dance entirely.
This guide assumes you are editing your code on Windows and compiling inside WSL. That gives you fast iteration in VS Code with builds that mirror your Linux CI and Pi runtime.
đ§ Why WSL Is the Right Toolchain
- Correct glibc and ABI: Uses the same GNU toolchain family as Raspberry Pi OS.
- No Windows linker weirdness: Build pipeline stays Linux-native from end to end.
- Tokio and epoll support: Matches the kernel interfaces used in production.
- CI parity: Same toolchain as GitHub Actions Ubuntu runners.
- Repeatable builds: WSL keeps the Linux environment stable and predictable.
â Step 0 - Install WSL2 (Ubuntu 22.04)
- 1Install WSL and Ubuntu
Open PowerShell and run:
shellwsl --install -d Ubuntu-22.04Reboot if prompted.
- 2Launch the Linux shellshell
wslYou are now inside Linux. Everything from this point should run inside WSL.
đ§°Step 1 - Install Build Dependencies
Install the ARM64 cross-linker and common build dependencies inside WSL:
sudo apt update
sudo apt install build-essential curl git \
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
pkg-config libssl-devgcc-aarch64-linux-gnu toolchain provides the cross-linker used by Cargo, and libssl-dev covers common TLS dependencies.đĻStep 2 - Install Rust Inside WSL
- 1Install Rust via rustupbash
curl https://sh.rustup.rs -sSf | sh source $HOME/.cargo/env - 2Verify toolchainbash
rustc --version cargo --version
đ¯Step 3 - Add the Raspberry Pi Target
rustup target add aarch64-unknown-linux-gnuđStep 4 - Open the Project From Windows
Your Windows filesystem is mounted under /mnt/c. If your repository lives at C:\\dev\\your-project, open it like this:
cd /mnt/c/dev/your-projectâī¸Step 5 - Tell Cargo to Use the ARM64 Linker
Add a project-local Cargo config so the cross-linker is used automatically.
mkdir -p .cargo
nano .cargo/config.toml[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"đī¸Step 6 - Build for Raspberry Pi
cargo build --release --target aarch64-unknown-linux-gnuYour ARM64 binary will be in:
target/aarch64-unknown-linux-gnu/release/your-projectđĻStep 7 - Copy to the Pi
scp target/aarch64-unknown-linux-gnu/release/your-project \
user@pi:/tmp/Then on the Raspberry Pi:
chmod +x /tmp/your-project
/tmp/your-projectđ§¯Troubleshooting
- Missing linker: ensure
gcc-aarch64-linux-gnuis installed and that the linker is set in.cargo/config.toml. - OpenSSL errors: confirm
libssl-devandpkg-configare installed in WSL. - WSL path confusion: builds must run inside WSL, and the repo should be accessed via
/mnt/c(not a Windows path).
đ§ŠOptional - VS Code Remote WSL
If you want the best editor and compiler integration, install the Remote WSL extension and open the project inside the WSL context.
- Install the VS Code extension: Remote - WSL.
- From the WSL terminal, run
code .in your repo. - You now have a Windows editor with a Linux compiler.
References: