# Cross-compiling static Rust binaries in Docker for Raspberry Pi

DevFeed: [Cross-compiling static Rust binaries in Docker for Raspberry Pi](<https://devfeed.tech/articles/cross-compiling-static-rust-binaries-in-docker-for-raspberry-pi-20926.md>)

Original publisher: [Read original article](<https://jakewharton.com/cross-compiling-static-rust-binaries-in-docker-for-raspberry-pi/>)

Published: 2021-05-27T00:00:00Z

Content type: article

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Rust](<https://devfeed.tech/topics/rust.md>), [Raspberry Pi](<https://devfeed.tech/topics/raspberry-pi.md>), [Docker](<https://devfeed.tech/topics/docker.md>), [Arm](<https://devfeed.tech/topics/arm.md>), [Linux](<https://devfeed.tech/topics/linux.md>), [Optimization](<https://devfeed.tech/topics/optimization.md>), [Deployment](<https://devfeed.tech/topics/deployment.md>)

Tags: [arm](<https://devfeed.tech/tags/arm.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [deployment](<https://devfeed.tech/tags/deployment.md>), [docker](<https://devfeed.tech/tags/docker.md>), [gcc](<https://devfeed.tech/tags/gcc.md>), [linux](<https://devfeed.tech/tags/linux.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [raspberry-pi](<https://devfeed.tech/tags/raspberry-pi.md>), [rust](<https://devfeed.tech/tags/rust.md>)

## AI overview

A practical guide to cross-compiling small, statically linked Rust binaries in Docker for ARMv7 and ARMv6 Raspberry Pi devices. It explains choosing musl-based Rust targets, configuring the cross-linker, installing the required linker on Ubuntu or macOS, and using Cargo settings such as size optimization and link-time optimization.

## Source excerpt

Earlier this year I built a web-based garage door controller using Rust for the Raspberry Pi called Normally Closed. My deployment includes a Pi 3b and a Pi Zero which are ARMv7 and ARMv6 devices, respectively. I deploy services with Docker and wanted to continue using it here for simplicity. Getting all of this set up and working together was not easy. There's also a lot of words in that title which might not mean much to you. That's okay! This is the blog post that I needed two weeks ago, so let's take a look at the steps required to accomplish this task. Cross-compiling and static linking Rust has excellent facilities for cross-compiling and static linking through Cargo. I got started following this guide on cross-compiling Rust for the Raspberry Pi. The guide recommends using the armv7-unknown-linux-gnueabihf Rust target which would support my Pi 3b. For the Pi Zero we can infer from Rust's platform support list that we need arm-unknown-linux-gnueabihf. However, these targets dynamically link against GNU libc whereas I wanted to statically link with musl. Referencing the platform support list again we can find the armv7-unknown-linux-musleabihf and arm-unknown-linux-musleabihf targets to use instead. $ rustup target add armv7-unknown-linux-musleabihf $ rustup target add arm-unknown-linux-musleabihf In addition to the target, the linker needs to be changed since it otherwise will use the one from your machine and its architecture. This can be specified in .cargo/config: [target.armv7-unknown-linux-musleabihf] linker = "arm-linux-gnueabihf-ld" [target.arm-unknown-linux-musleabihf] linker = "arm-linux-gnueabihf-ld" On Ubuntu you can install this linker by running sudo apt install gcc-arm-linux-gnueabihf. On my Mac I was able to install it with brew install arm-linux-gnueabihf-binutils. (Despite using the musl compilation target, the GNU linker will still work.) This is enough to compile working binaries! $ cargo build --target armv7-unknown-linux-musleabihf --relea