Run an LLM on a Raspberry Pi 5 with llama.cpp
Why run an LLM on a Raspberry Pi?
A Raspberry Pi 5 costs less than a month of heavy API usage, draws a few watts, and runs silently on a shelf. It will never replace a cloud GPU cluster, but for a private home assistant, a summarizer for your own notes, or a always-on playground for learning how local inference works, it is genuinely useful. The whole stack runs offline: your prompts never leave your house.
The catch is expectations. A Pi 5 has a quad-core Arm CPU and 4 to 16 GB of RAM shared with the system. It has no discrete GPU worth using for inference. That rules out the big models — but small, well-quantized models in the 1B to 3B range run at a usable few tokens per second. One hands-on test of a 3B model on a Pi 5 measured about 5.6 tokens per second: slow by cloud standards, fine for a chat you read as it appears.
If you have already tried the easy path, this guide goes one level deeper. Our Ollama quickstart gets a model running in minutes on a desktop; here we build llama.cpp itself, which gives you finer control over threads, context, and serving — and teaches you how the whole pipeline fits together.
Source: TechBalkans — a Raspberry Pi running a local model.
What you need
- Raspberry Pi 5, 8 GB model recommended (4 GB works for 1B to 1.5B models only)
- An active cooler — sustained inference keeps all four cores busy, and a throttling Pi gets much slower
- 64-bit Raspberry Pi OS (Bookworm or newer) on a fast microSD card or NVMe SSD
- A stable 5V/5A power supply — the official Pi 5 supply — since brownouts under load cause mysterious crashes
- About 30 minutes and roughly 2 GB of free disk space for the model
Step 1: Prepare the Pi
Update the system and install the build tools. Building from source matters on Arm: a binary built for your exact CPU is noticeably faster than a generic one.
sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential cmake git
Two optional tweaks help. The Pi's GPU is useless for LLM inference, so you can shrink its memory carve-out, and a small swap file is a safety net against out-of-memory kills:
echo "gpu_mem=16" | sudo tee -a /boot/firmware/config.txt
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Step 2: Build llama.cpp
Clone the current repository (the project moved from ggerganov to the ggml-org organization) and build it. The -j4 flag uses all four cores; expect roughly 5 to 10 minutes.
cd ~
git clone https://github.com/ggml-org/llama.cpp.git
cd llama.cpp
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j4
When it finishes, the two binaries you need are build/bin/llama-cli for chatting and build/bin/llama-server for serving an API.
Step 3: Pick a model your Pi can actually run
This is the step most people get wrong. Model size in gigabytes roughly equals parameters times bytes per parameter: a 3B model at Q4_K_M quantization needs under 2 GB, while a 7B model needs around 4.5 GB plus context memory — and on an 8 GB Pi that can fail outright, as the terminal below shows.
Source: It's FOSS — a 7B model refusing to load on an 8GB Pi. Size the model to the hardware.
- Qwen2.5-3B-Instruct, Q4_K_M — a strong all-rounder for an 8 GB Pi 5, about 1.9 GB to download
- Qwen2.5-1.5B-Instruct, Q4_K_M — the safe pick for a 4 GB board, noticeably faster
- Llama-3.2-3B-Instruct, Q4_K_M — a solid alternative if you prefer Meta's instruction tuning
If quantization formats confuse you, our GGUF quantization guide explains what Q4_K_M actually trades away. Download the 3B model like this:
mkdir -p ~/models
cd ~/models
wget https://huggingface.co/Qwen/Qwen2.5-3B-Instruct-GGUF/resolve/main/qwen2.5-3b-instruct-q4_k_m.gguf
Step 4: Chat from the terminal
Before wiring up anything else, confirm the model loads and answers sensibly:
~/llama.cpp/build/bin/llama-cli \
-m ~/models/qwen2.5-3b-instruct-q4_k_m.gguf \
-t 4 -c 2048 --temp 0.7 -n 256 \
-p "Explain what a Raspberry Pi is in two sentences."
The flags that matter: -t 4 uses all four threads, -c 2048 caps the context window (larger windows eat RAM fast on a Pi), and -n 256 limits the reply length. Add --interactive instead of -p for a back-and-forth chat session. If output crawls, drop to the 1.5B model before touching anything else — model size dominates speed on this hardware.
Step 5: Serve it as an OpenAI-compatible API
This is where the Pi becomes a real home server. llama-server exposes the same /v1/chat/completions endpoint as OpenAI, so existing client code works with only a base URL change:
~/llama.cpp/build/bin/llama-server \
-m ~/models/qwen2.5-3b-instruct-q4_k_m.gguf \
-t 4 -c 2048 --host 0.0.0.0 --port 8080
Test it from another terminal:
curl http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{"model": "qwen2.5-3b", "messages": [{"role": "user", "content": "Hello"}]}'
And from Python, using the standard OpenAI SDK:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8080/v1", api_key="not-needed")
response = client.chat.completions.create(
model="qwen2.5-3b",
messages=[{"role": "user", "content": "Summarize this in one sentence."}],
)
print(response.choices[0].message.content)
Point any OpenAI-compatible tool at the Pi's address — a notes app, a home-automation script, or a coding assistant — and it talks to your local model. Keep --host 0.0.0.0 only on a trusted home network; llama-server has no authentication, so never expose port 8080 to the internet.
Step 6 (optional): Run it at boot
A one-file systemd service keeps the server up after power cuts. Create /etc/systemd/system/llama-server.service with the server command under ExecStart, then run sudo systemctl enable --now llama-server. This is the difference between a demo and an appliance: after a reboot, the API is simply there.
Troubleshooting
- "Killed" or out-of-memory errors: use a smaller model or cut
-cto 1024. Context memory grows with the window, and it is the hidden RAM cost. - Very slow output: check for thermal throttling with
vcgencmd get_throttled— a non-zero result means cooling or power is the problem, not the model. - Gibberish replies: you probably loaded a base model instead of an instruct model, or the chat template mismatched. Stick to
-InstructGGUF files. - Port already in use: another service owns 8080; change
--portand update your client URLs.
Where to go next
Once the API is up, the natural next steps are a web front end or a bigger machine. Our Open WebUI tutorial adds a ChatGPT-style interface on top of any local model, and if you outgrow the Pi, the VRAM sizing guide tells you what GPU actually buys you. The Pi remains the cheapest way to learn the whole stack — and to keep one small, private model always on.
nce.

Comments
Post a Comment