Cloud Engineer: Compiling Python Daemons for ARM: Packaging Raspberry Pi Apps using PyInstaller

2021-09-18 03:08:34+00:00

Deploying Python monitoring daemons to Raspberry Pi edge nodes is challenging if you try to compile dependencies directly on the device. Raspberry Pis typically have constrained CPU power and limited RAM, which makes compiling heavy C extensions (such as cryptography, cffi, or NumPy) slow and prone to Out-Of-Memory (OOM) failures. The correct approach is to cross-compile the application inside an ARM-emulated environment on a high-powered build server.

Using Docker with QEMU emulation allows us to compile ARM-compatible standalone executables using PyInstaller cleanly and quickly.


1. Setting Up the Emulated Docker Build Environment

We register QEMU user emulation on our host machine to execute ARM instructions, then build a target container matching the Raspberry Pi runtime environment:

# Register QEMU interpreters on the host system
$ docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

# Create the Dockerfile for building the ARM binary
# Dockerfile.arm32v7
FROM arm32v7/python:3.9-slim-buster

RUN apt-get update && apt-get install -y \
    build-essential \
    libssl-dev \
    libffi-dev \
    python3-dev \
    && rm -rf /var/lib/apt/lists/*

RUN pip install --no-cache-dir pyinstaller

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# Compile code to a standalone executable
CMD ["pyinstaller", "--onefile", "edge_daemon.py"]

2. Orchestrating the Build Job

We execute the build script to spin up the container and output the compiled binary back to our local staging directory:

# Build the compiler container image
$ docker build -f Dockerfile.arm32v7 -t arm-compiler .

# Run container and map output directory
$ docker run --rm -v $(pwd)/dist:/app/dist arm-compiler

# Verify that the output binary is compiled for ARM architectures
$ file dist/edge_daemon
dist/edge_daemon: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked...

3. Handling GPIO and Edge Hardware Libraries

When compiling edge daemons, packages like RPi.GPIO or spidev require hardware registers. By compiling within the emulated container containing native headers, PyInstaller correctly maps and bundles these system dependencies into the binary, delivering a single file that can run directly on Raspberry Pi installations.

Reproducible Cross-Architecture ARMv7/ARM64 Builds with PyInstaller

Package self-contained Python daemons and shared C libraries inside QEMU/Docker build containers so field deployments never fail due to missing system packages.


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs