Knowledge Base
Software
2026-02-13
9 min

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.

WSL
Windows
Linux
Rust
Cross Compile
ARM64
Raspberry Pi
Cargo
aarch64

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)

  1. 1
    Install WSL and Ubuntu

    Open PowerShell and run:

    shell
    wsl --install -d Ubuntu-22.04

    Reboot if prompted.

  2. 2
    Launch the Linux shell
    shell
    wsl

    You 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:

bash
sudo apt update
sudo apt install build-essential curl git \
  gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
  pkg-config libssl-dev
â„šī¸
The gcc-aarch64-linux-gnu toolchain provides the cross-linker used by Cargo, and libssl-dev covers common TLS dependencies.

đŸĻ€Step 2 - Install Rust Inside WSL

  1. 1
    Install Rust via rustup
    bash
    curl https://sh.rustup.rs -sSf | sh
    source $HOME/.cargo/env
  2. 2
    Verify toolchain
    bash
    rustc --version
    cargo --version

đŸŽ¯Step 3 - Add the Raspberry Pi Target

bash
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:

bash
cd /mnt/c/dev/your-project
âš ī¸
Build inside WSL, but keep editing in Windows. VS Code handles this split cleanly and keeps file watching fast.

âš™ī¸Step 5 - Tell Cargo to Use the ARM64 Linker

Add a project-local Cargo config so the cross-linker is used automatically.

bash
mkdir -p .cargo
nano .cargo/config.toml
toml
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"
✅
This is the critical step. Without it, Cargo will try to link with the host compiler and the build will fail.

đŸ—ī¸Step 6 - Build for Raspberry Pi

bash
cargo build --release --target aarch64-unknown-linux-gnu

Your ARM64 binary will be in:

text
target/aarch64-unknown-linux-gnu/release/your-project

đŸ“ĻStep 7 - Copy to the Pi

bash
scp target/aarch64-unknown-linux-gnu/release/your-project \
  user@pi:/tmp/

Then on the Raspberry Pi:

bash
chmod +x /tmp/your-project
/tmp/your-project

đŸ§¯Troubleshooting

  • Missing linker: ensuregcc-aarch64-linux-gnu is installed and that the linker is set in .cargo/config.toml.
  • OpenSSL errors: confirmlibssl-dev and pkg-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.
Last updated: 2026-02-13