Monthly Archives: December 2023

Cheatsheet: Raspberry Pi + Alpine Linux + OctoPrint

The last upgrade to Raspbian Raspberry Pi OS broke the WiFi connection to my 3D printer in an inconvenient way, as it runs headless with just a 4-pin power-and-UART connection. I’ve been having pretty good luck lately with Alpine Linux on various systems (including a Docker host and a cluster of Raspberry Pi 4s I knocked together to try to wrap my head around Kubernetes), so I thought I’d put it on the Compute Module 4 that drives my printer. I backed up the OctoPrint config that was on it and got a replacement configuration running on a spare Raspberry Pi 3 at first. Here’s what I came up with in the way of what needs to be done to put Alpine Linux on a Raspberry Pi and bring up OctoPrint on the resulting system.

  • Download the Alpine Linux tarball that’s appropriate for your Raspberry Pi from here. (For the RPi 3 and up, you most likely want the aarch64 image. This is especially true for the RPi 4 and up with 4 GB or more of RAM.) Instructions on how to do this are available elsewhere, but the short version is that you want to unpack the tarball to a FAT-formatted MicroSD card (or USB stick if you’re targeting a Compute Module 4 with onboard eMMC), boot from the device, and run setup-alpine to install. Toward the end, you want to install it in “sys” mode. Reboot to bring up the new system, and then log in. You would’ve needed a monitor and keyboard to set up Alpine, but if you’ve set it up right, you can ssh into it from here on out.
  • Edit /etc/apk/repositories to enable the community repository. (This is needed for vcgencmd.)
  • Install needed software:
    doas apk add gcc make musl-dev linux-headers libffi-dev nginx raspberrypi-utils-vcgencmd python3-dev
  • Install OctoPrint:
    python -m venv --upgrade-deps octoprint
    octoprint/bin/pip install https://gitlab.alfter.us/salfter/marlin-binary-protocol/-/archive/v0.0.8/marlin-binary-protocol-v0.0.8.tar.gz
    octoprint/bin/pip install octoprint

    (The marlin-binary-protocol installation is needed if you want to use the Firmware Updater plugin and need to use its marlinbft driver. My printer uses a BTT SKR 1.4 Turbo, an LPC1769-based board that can use this driver for firmware uploads.)
  • Edit /etc/fstab to disable tmpfs on /tmp. (Restoring backups from another OctoPrint instance will probably fail if you don’t.)
  • Copy the following to /etc/nginx/nginx.conf:
    worker_processes 1;

    events {
    worker_connections 1024;
    }

    http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

    map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
    }

    upstream "octoprint" {
    server 127.0.0.1:5000;
    }

    upstream "mjpg-streamer" {
    server 127.0.0.1:8080;
    }

    server {
    listen 80;
    server_name localhost;

    location / {
    proxy_pass http://octoprint/;
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Scheme $scheme;
    proxy_http_version 1.1;

    client_max_body_size 0;
    }

    location /webcam/ {
    proxy_pass http://mjpg-streamer/;
    }

    # redirect server error pages to the static page /50x.html
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }
    }
  • Create /etc/local.d/octoprint.start with the following and make it executable:
    #!/usr/bin/env ash
    su - salfter -c "nohup /home/salfter/octoprint/bin/octoprint serve 2>&1 >/dev/null &"

    (change “salfter” to whatever user you’re using)
  • Create /etc/local.d/octoprint.stop with the following and make it executable:
    #!/usr/bin/env ash
    pkill octoprint
  • Check /etc/inittab to make sure the serial console isn’t enabled.
  • Add the following to /boot/usercfg.txt:
    enable_uart=1
    gpu_mem=16
    dtoverlay=pi3-disable-bt

    [cm4]
    otg_mode=1

    Reboot so the changes take effect.
  • Enable the nginx and local startup scripts:
    doas rc-update add nginx
    doas rc-update add local
  • Add the user under which OctoPrint runs (in my case, that would be salfter) to whatever group /dev/ttyAMA0 belongs (root, in my case).
  • Edit /etc/doas.d/doas.conf to allow doas to work without a password (needed so OctoPrint can restart itself):
    permit nopass :wheel
  • Reboot and wait for OctoPrint to come up on port 80. Restore your backup (if you have one) and you’re done!