AI & prompting

How to Use llama.cpp: Install, Run a Model, Start the Server

To use llama.cpp, install it (for example winget install llama.cpp or brew install llama.cpp), then run a model straight from Hugging Face with llama-cli -hf ggml-org/Qwen3.5-0.8B-GGUF. For an OpenAI-compatible API and a browser chat UI, run llama-server with the same model; it listens on http://127.0.0.1:9931 by default.

llama.cpp is a plain C/C++ inference engine for large language models. It runs models in the GGUF format on CPUs and on NVIDIA (CUDA), AMD (HIP), Apple (Metal), Intel (SYCL) and Vulkan GPUs, and it can split a model between GPU and CPU when the model is bigger than your VRAM. Commands and defaults below are from the project's documentation as of October 2026; the project moves fast, so llama-cli --help on your build is the final word.

Install llama.cpp

Pick one of these. All of them put the tools on your PATH except the zip download.

SystemCommand
Windowswinget install llama.cpp
macOS or Linux (Homebrew)brew install llama.cpp
macOS (MacPorts)sudo port install llama.cpp
Any (conda-forge)conda install -c conda-forge llama.cpp
Nixnix profile install nixpkgs#llama-cpp

The README also offers an install script (curl -LsSf https://llama.app/install.sh | sh, or irm https://llama.app/install.ps1 | iex in PowerShell). Read any script before piping it into a shell.

To install by hand, download a zip from the GitHub releases page. Windows builds are named by backend, for example llama-b11898-bin-win-cpu-x64.zip (23 MB, CPU only) or llama-b11898-bin-win-cuda-13.4-x64.zip for NVIDIA GPUs, which also needs the matching cudart-llama-bin-win-cuda-13.4-x64.zip runtime. Unzip and run the programs from that folder. A build contains about 20 programs; you need three:

  • llama-cli: chat with a model in the terminal.
  • llama-server: HTTP server with an OpenAI-compatible API and a web UI.
  • llama-quantize: convert a GGUF file to a smaller quantization.

Newer builds also include a single llama program, and the README's quick start uses it as llama cli and llama serve. These do the same jobs as llama-cli and llama-server; this guide uses the separate program names, which every build has.

Run your first model with llama-cli

llama-cli -hf ggml-org/Qwen3.5-0.8B-GGUF

-hf takes a Hugging Face repository in the form user/model[:quant]. llama.cpp downloads the file, caches it, and starts an interactive chat. If you don't name a quantization, it picks Q4_K_M, or the first file in the repository if there is no Q4_K_M. To choose one explicitly:

llama-cli -hf ggml-org/Qwen3.5-0.8B-GGUF:Q8_0

Type at the prompt and press Enter. llama-cli -cl lists the models already in the cache, and the LLAMA_CACHE environment variable changes where they are stored.

If you already have a GGUF file, point at it with -m instead:

llama-cli -m ./models/my-model-Q4_K_M.gguf

A 0.8B model is a quick test that runs almost anywhere. For real use, pick a model that fits your memory: see how much VRAM you need and what Q4_K_M and the other GGUF types mean.

The llama-cli options you'll actually use

OptionWhat it doesDefault
-m FILELoad a local GGUF filenone
-hf user/model[:quant]Download and load from Hugging Facequant Q4_K_M
-c NContext size in tokens0 = the model's own context length
-ngl NLayers to put on the GPU: a number, auto or allauto
-sys "TEXT"System promptnone
-p "TEXT"Start with this promptnone
-stSingle turn: answer once, then exitoff
-n NMaximum tokens to generate-1 = no limit
-t NCPU threads for generation-1

A one-shot answer, useful in scripts:

llama-cli -hf ggml-org/Qwen3.5-0.8B-GGUF -sys "Answer in one sentence." -p "What is a GGUF file?" -st

Most options can also be set with environment variables, which the help lists next to each flag, for example LLAMA_ARG_CTX_SIZE for -c and LLAMA_ARG_N_GPU_LAYERS for -ngl.

Context size and memory

With -c 0 (the default), llama.cpp uses the context length stored in the model. Some models declare very long contexts, and the memory for the context (the KV cache) grows with it, so a model that fits easily can fail to load or spill to the CPU at its full context. If that happens, set a smaller value, such as -c 8192.

GPU offload with -ngl

The default -ngl auto puts as many layers on the GPU as fit. Set a number to control it yourself, all to force everything onto the GPU, or 0 to run on the CPU only. On macOS, Metal is enabled in the build by default; on Windows and Linux, the GPU is used only if you installed a build for your backend (CUDA, HIP, Vulkan or SYCL). A CPU-only zip ignores the GPU.

Run llama-server for an API and a web UI

llama-server -hf ggml-org/Qwen3.5-0.8B-GGUF -c 8192

The server listens on 127.0.0.1:9931 by default. Open http://127.0.0.1:9931 in a browser for the built-in chat UI. Check that the model has loaded:

curl http://127.0.0.1:9931/health

It returns HTTP 503 with "Loading model" while loading, and {"status": "ok"} with HTTP 200 once ready. Many tutorials, and the README's own Docker example, use port 8080 instead; if a guide's URLs don't connect, check which port your server is on, or start it with --port 8080.

Send a chat request to the OpenAI-compatible endpoint:

curl http://127.0.0.1:9931/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "system", "content": "You are a concise assistant."},
      {"role": "user", "content": "Explain GGUF in two sentences."}
    ]
  }'

On Windows, use curl.exe in PowerShell and put the JSON in a file (-d @request.json) to avoid quoting problems. The README's own example sends "model": "gpt-3.5-turbo" to a local model, so with one model loaded the name in that field does not have to match.

Any OpenAI SDK works by changing the base URL. In Python:

from openai import OpenAI

client = OpenAI(base_url="http://127.0.0.1:9931/v1", api_key="sk-no-key-required")
reply = client.chat.completions.create(
    model="local",
    messages=[{"role": "user", "content": "Say hello in French."}],
)
print(reply.choices[0].message.content)

The server also offers /v1/completions, /v1/embeddings, /v1/models, an Anthropic Messages-compatible /v1/messages, and its own /completion endpoint, which is not OpenAI-compatible. For structured output and tool calls, see getting JSON from an LLM.

Sharing the server on your network

--host 0.0.0.0 listens on all interfaces so other machines can connect. Add --api-key (or LLAMA_API_KEY) so that requests must send Authorization: Bearer <key>, and don't forward the port from your router to the internet.

Make your own GGUF from a Hugging Face model

Most popular models already have GGUF versions on Hugging Face. If one doesn't, the repository's Python script converts the original weights, and llama-quantize shrinks the result. From a clone of the llama.cpp repository:

python3 -m pip install -r requirements.txt
python convert_hf_to_gguf.py --outfile model-bf16.gguf --outtype bf16 --remote org/model-name
llama-quantize model-bf16.gguf model-Q4_K_M.gguf Q4_K_M

--remote reads the model straight from Hugging Face; drop it and give a local directory if you already downloaded the model. The quantize README notes that its requirements install transformers 4, while some newer models need transformers 5 (pip install -U transformers). The -bf16 file is about 2 bytes per parameter, so check your disk space before converting a large model.

If something goes wrong

  • Very slow, GPU idle: you have a CPU-only build, or -ngl is 0. Install the build for your GPU backend, and run llama-cli --list-devices to print the devices llama.cpp can use.
  • Out of memory while loading: lower -c, choose a smaller quantization, or set -ngl to a number below the model's layer count so the rest runs on the CPU.
  • Garbled or endless output: the chat template may be wrong for the model. llama.cpp reads the template from the model's metadata by default; --chat-template overrides it with a built-in one such as chatml.
  • A guide's command fails with an unknown option: flags are renamed from time to time. Run llama-cli --help and look for the new name; older aliases such as --n-gpu-layers for -ngl are often kept.

If you only want to chat with a model and don't need every setting, Ollama wraps a similar workflow in fewer commands.

Frequently asked questions

What is the difference between llama.cpp and Ollama?
llama.cpp is the inference engine and its command-line tools; you choose the model file, quantization and every runtime flag yourself. Ollama is a separate application that downloads and manages models for you and runs them with its own defaults behind a simpler interface. llama.cpp gives you more control and the newest features first; Ollama is quicker to start with.
Which GGUF quantization should I download?
Q4_K_M is the usual starting point, and it is what llama.cpp picks by default when you use -hf without naming a quantization. Choose Q5_K_M or Q8_0 if you have memory to spare and want higher quality, or a Q3 or Q2 variant only if nothing else fits.
Can llama.cpp run models that are bigger than my GPU's VRAM?
Yes. llama.cpp supports CPU and GPU hybrid inference: the layers that fit go to the GPU and the rest run on the CPU from system RAM. Control the split with -ngl (number of layers on the GPU). It works, but generation slows down a lot as more layers run on the CPU.
Is llama-server safe to expose on my network?
By default it listens only on 127.0.0.1, so other machines cannot reach it. If you set --host 0.0.0.0 to share it, add --api-key so requests without the key are refused, and keep it behind your firewall. It is not meant to be exposed directly to the internet.

Sources