Coders In Hoods
  • Courses
  • Roles
  • Blog
  • Setup

Coders In Hoods AI Academy

Every course is free. Sign in to start, and your progress, quiz scores and bookmarks follow you.

AI Academy

  • Courses
  • Paths
  • Setup guides
  • How we teach

Role roadmaps

  • AI Engineer
  • Forward Deployed Engineer
  • Machine Learning Engineer
  • All roles

Coders In Hoods

  • The agency
  • Coaching for teams
  • Blog
  • LinkedIn
  • X (Twitter)

Contact

codersinhoods@gmail.com

© Coders In Hoods · since 2019

Setup guides

Setup guide · Models

Install llama.cpp

llama.cpp serving a GGUF model on your CPU or Apple silicon through an OpenAI-compatible API, called with the same three variables as every other model setup, plus the quantise, perplexity and conversion tools the serving exercises use.

20 min Updated 2026-11-13

llama.cpp runs GGUF models on ordinary CPUs and on Apple silicon, and its llama-server speaks the same OpenAI-compatible API as vLLM, so the code you write for a GPU endpoint runs against a laptop unchanged. It is the CPU path of Serving Open-Weight Models: the exercises on quantisation, structured outputs, adapters and deployment all have a llama.cpp variant, and this guide installs the server and the four tools they use. If you only want a local model to chat with from Claude Code, Ollama is simpler; it is built on llama.cpp.

Before you start

  • macOS on Apple silicon runs the model on the GPU through Metal, which is on by default. Intel Macs and Linux and Windows machines without an NVIDIA card run on the CPU: slower, but everything works.
  • Memory: the Q8_0 model below is 4.3 GB and the Q4_K_M one 2.5 GB, plus a few hundred MB for an 8K context. 8 GB of RAM is enough for Q4_K_M; 16 GB is comfortable for Q8_0.
  • Disk: about 7 GB if you keep both files.
  • Hugging Face account and CLI for hf download, and uv for the conversion scripts in step 4.

1. Install

macOS (and Linux with Homebrew)

brew install llama.cpp

The formula is updated with each llama.cpp release and installs llama-server, llama-cli, llama-quantize, llama-perplexity and the rest.

Windows

winget install llama.cpp

Linux

Download the prebuilt archive for your machine from the releases page (llama-<build>-bin-ubuntu-x64.tar.gz for a CPU build; there are CUDA, ROCm and Vulkan variants beside it), extract it and put the folder on your PATH. The one alternative is building from source, which needs CMake and a C++ compiler:

git clone https://github.com/ggml-org/llama.cpp
cd llama.cpp
cmake -B build
cmake --build build --config Release -j 8

The binaries land in build/bin/. Check the install with:

llama-server --version

2. Get a GGUF

The course's worked examples use Qwen3-4B-Instruct-2507, the same model as the vLLM guide. Qwen publishes no GGUF of this release itself, so use Unsloth's conversions, which keep the Apache-2.0 licence and the original chat template. Download the 8-bit file, or the 4-bit one if memory is tight:

hf download unsloth/Qwen3-4B-Instruct-2507-GGUF Qwen3-4B-Instruct-2507-Q8_0.gguf --local-dir models
hf download unsloth/Qwen3-4B-Instruct-2507-GGUF Qwen3-4B-Instruct-2507-Q4_K_M.gguf --local-dir models

Both are ungated, so no token is needed. (llama-server -hf unsloth/Qwen3-4B-Instruct-2507-GGUF:Q8_0 downloads and serves in one step, but the exercises refer to the files by path.)

3. Serve it

Pick an API key, start the server with an 8K context, the OpenAI-style port the course uses and a served name, and leave it running in its own terminal:

export LLAMA_API_KEY=$(openssl rand -hex 24)
llama-server -m models/Qwen3-4B-Instruct-2507-Q8_0.gguf -c 8192 --port 8080 \
  --api-key "$LLAMA_API_KEY" -a Qwen/Qwen3-4B-Instruct-2507

-c sets the context size (0, the default, takes the model's own, which for this model is far more KV cache than a laptop should allocate); --port overrides the default 9931; --api-key makes every /v1 request need a bearer token, which matters as soon as you bind to --host 0.0.0.0; -a sets the model name the API reports. Threads (-t) and GPU layers (-ngl) default to automatic, and the Jinja chat template, which tool calls need, is on by default. GET /health answers 503 while the model loads and 200 once it is ready.

llama.cpp then plugs into the shared configuration pattern as one more .env block:

.env
OPENAI_BASE_URL=http://localhost:8080/v1
OPENAI_API_KEY=<your LLAMA_API_KEY>
MODEL=Qwen/Qwen3-4B-Instruct-2507

Keep .env in .gitignore. Any OpenAI-SDK code in the answers now runs against the laptop, for example model.py from OpenRouter or a local model?:

import os
 
from openai import OpenAI
 
client = OpenAI()  # reads OPENAI_BASE_URL and OPENAI_API_KEY itself
reply = client.chat.completions.create(
    model=os.environ["MODEL"],
    messages=[{"role": "user", "content": "Reply with the single word: ready"}],
    temperature=0.7, top_p=0.8,
)
print(reply.choices[0].message.content)

The model card recommends temperature 0.7 and top-p 0.8 for this instruct model.

4. The other tools you'll use

  • llama-quantize turns a 16-bit GGUF into a smaller one: llama-quantize model-bf16.gguf model-Q4_K_M.gguf Q4_K_M. Add --imatrix imatrix.gguf to use an importance matrix, which the quantise docs say limits the accuracy loss, especially at 4 bits and below.
  • llama-perplexity measures quality loss. Record the reference model's logits once with --kl-divergence-base ref.kld, then run the quantised model with the same --kl-divergence-base ref.kld --kl-divergence to get the KL divergence, the change in correct-token probability and the share of tokens where both models agree on the top token. The logit file is large; the course's text corpus is short on purpose.
  • convert_hf_to_gguf.py makes the 16-bit GGUF from Hugging Face weights: python convert_hf_to_gguf.py --outfile model-bf16.gguf --outtype bf16 --remote Qwen/Qwen3-4B-Instruct-2507, or a local directory in place of --remote. It lives in the llama.cpp repository (clone it even if you installed binaries) and needs its Python requirements: uv pip install -r requirements.txt --index-strategy unsafe-best-match.
  • convert_lora_to_gguf.py, in the same repository with the same requirements, converts a PEFT adapter: python convert_lora_to_gguf.py --base-model-id Qwen/Qwen3-4B-Instruct-2507 --outfile triage-lora.gguf path/to/adapter. Serve it beside the base with llama-server … --lora triage-lora.gguf (comma-separate several; --lora-scaled file:0.5 sets a scale).
  • Constrained output: -j/--json-schema '{…}' or -jf/--json-schema-file triage_schema.json constrains every generation to a JSON schema, and --grammar / --grammar-file takes a GBNF grammar instead. Per request, response_format with {"type": "json_schema", "json_schema": {"name": …, "schema": {…}}} works as it does on hosted APIs.

Check it works

With the server running, in another terminal:

curl -s http://localhost:8080/v1/chat/completions \
  -H "Content-Type: application/json" -H "Authorization: Bearer $LLAMA_API_KEY" \
  -d '{"model":"Qwen/Qwen3-4B-Instruct-2507","messages":[{"role":"user","content":"Reply with the single word: ready"}],"max_tokens":8}'

Expected: a JSON object with "object": "chat.completion", a choices list whose first message has "role": "assistant" and "content": "ready" (possibly with a full stop), and a usage block with the prompt and completion token counts. Without the Authorization header you get a 401, which shows the key is enforced.

Troubleshooting

failed to load model or error loading model: … No such file. The path after -m is wrong, or the download was interrupted. Check ls -l models/ shows a file of about 4.3 GB (Q8_0) or 2.5 GB (Q4_K_M) and re-run hf download if not; it resumes.

It crashes at startup, or the machine swaps. The model plus the KV cache for -c doesn't fit in memory. Use the Q4_K_M file, lower -c (4096 is enough for a ticket), or on a Mac check that other apps aren't holding memory.

On Windows the path is cut at the first space. Quote it: llama-server.exe -m "C:\Users\you\models\Qwen3-4B-Instruct-2507-Q8_0.gguf" -c 8192 --port 8080. In PowerShell, set the key with $env:LLAMA_API_KEY and pass --api-key $env:LLAMA_API_KEY.

The first token takes seconds. On a CPU the prompt is processed at a few hundred tokens a second, so a 1,500-token system prompt costs several seconds the first time. The server caches the prompt prefix between requests, so repeated prompts with the same prefix start fast; on Apple silicon check nothing else is loading the machine.

Going further

  • Local models with Ollama, the easier route when you only need a model to chat with
  • Serve a model with vLLM, the GPU path with the same three variables
  • The Quillmoor triage kit, the schema and tickets the exercises serve

Sources

  • Install pre-built version of llama.cppDocs · ggml`brew install llama.cpp` and `winget install llama.cpp`; the formula and package track releases.
  • llama.cpp build guideDocs · ggmlThe CMake build, `-j` for parallel jobs, and Metal being on by default on macOS.
  • llama.cpp HTTP serverDocs · ggmlEvery `llama-server` flag used here (`-m`, `-c`, `--port`, `--api-key`, `-a`, `--lora`, `--json-schema`, `--grammar`), the `/v1` endpoints, `response_format` and `/health`.
  • llama.cpp quantizeDocs · ggml`convert_hf_to_gguf.py`, the Python requirements, `llama-quantize` and `--imatrix`, with the bits-per-weight table.
  • llama.cpp perplexityDocs · ggml`--kl-divergence-base` to record a reference model's logits and `--kl-divergence` to compare a quantised model against them.
  • Qwen3-4B-Instruct-2507-GGUFDocs · UnslothThe Apache-2.0 GGUF conversions of Qwen's instruct model used below (Qwen publishes no GGUF of the 2507 release itself); the Q8_0 and Q4_K_M files and the recommended sampling settings.

Exercises that use this

  • Two adapters, one serverServing Open-Weight Models · Serving your adapter
  • Quantise and prove itServing Open-Weight Models · Quantisation with evidence
  • Build a guided sampler, then use the server'sServing Open-Weight Models · Structured outputs and tool calls
  • Deploy with a runbook: shadow, promote, roll backServing Open-Weight Models · Deployment and rollout

On this page

  • Before you start
  • 1. Install
  • macOS (and Linux with Homebrew)
  • Windows
  • Linux
  • 2. Get a GGUF
  • 3. Serve it
  • 4. The other tools you'll use
  • Check it works
  • Troubleshooting
  • Going further
  • Sources
  • Exercises that use this