llama.cpp
The C/C++ engine that made CPU and mixed CPU/GPU inference practical, and the origin of the GGUF format.
GitHub stars
131,476
License
MIT
Install
binary
Latest release
Oct 15, 2026
Best for
Squeezing a model onto limited hardware, and controlling exactly how many layers live on the GPU.
Model formats
- GGUF
Hardware behaviour
-ngl flag decides how many layers go to the GPU, which is the single most useful knob when VRAM is tight.Not sure what your card can hold? Run the fit check — the entry point in our listings is the RTX 4060 8GB at 8.00 GB.
llama.cpp is a C/C++ inference engine built for single-user local model execution without Python or PyTorch runtime dependencies. Developed originally by Georgi Gerganov, it serves as the reference implementation for the GGUF model format. By eliminating virtual environment overhead, it lets operators run quantized language models directly on native hardware compute interfaces.
The tool gives operators low-level control over memory mapping, layer offloading, and physical batching. Rather than masking execution parameters behind high-level GUIs, it exposes explicit knobs to balance weight distribution between system RAM and dedicated VRAM.
What llama.cpp actually does
llama.cpp executes quantized language models using native C and C++ code via the ggml backend. It natively parses GGUF files, supporting integer and floating-point quantization schemes like INT4, INT8, and FP16. This architecture skips Python interpreter latency and PyTorch GPU memory overhead, running matrix multiplications directly on target hardware accelerators.
For terminal workflows, the project provides dedicated command-line utilities. You can launch interactive text sessions using llama-cli:
llama-cli -m ./model.Q4_K_M.gguf -ngl 99 -c 4096To expose model endpoints to external applications or scripts, llama-server runs a lightweight HTTP service:
llama-server -m ./model.Q4_K_M.gguf -ngl 99 -c 8192 --port 8080This binary serves OpenAI-compatible routes, including /v1/chat/completions, /v1/completions, and /v1/embeddings.
Installing it on Windows, macOS, and Linux
On macOS, installation works via Homebrew, which pulls pre-built binaries configured for Apple Silicon Metal acceleration:
brew install llama.cppTo build for NVIDIA hardware, compile from source using CMake with CUDA support enabled:
cmake -B build -DGGML_CUDA=ON && cmake --build build --config Release -jThis command generates binaries compiled for your installed CUDA toolkit.
For non-NVIDIA GPUs or AMD cards without ROCm support, compiling with Vulkan support via -DGGML_VULKAN=ON provides broad hardware coverage. Ensure system Vulkan development headers are installed so CMake links the loader library correctly.
When compiling on memory-constrained machines, parallel build jobs can exhaust host RAM and crash the compiler. Limit parallel jobs with -j2 or expand swap space to stabilize compilation.
Building on Windows can trigger BLAS macro naming collisions depending on the MSVC environment. On Linux with AMD hardware, compilation failures usually stem from missing HIP development headers in ROCm package paths. Check the llama.cpp GitHub repository for platform-specific flags.
Hardware and VRAM behaviour
The engine includes compute backends for CUDA, ROCm, Metal, Vulkan, SYCL, and plain CPU execution.
Weight offloading uses the -ngl flag, which specifies how many transformer layers shift to GPU VRAM while leaving the rest in host RAM. The -ngl flag is the most useful knob when VRAM is tight.
Memory allocation scales with context window length. You can reduce KV cache memory overhead using Flash Attention with the -fa flag. Quantizing the KV cache with -ctk q8_0 and -ctv q8_0 cuts context memory footprint further without dropping conversation context.
For multi-GPU setups, llama.cpp splits memory allocation using the --tensor-split flag. Supplying comma-separated ratios balances tensor splits across cards with mismatched VRAM capacities.
Certain backends have memory accounting edge cases. Vulkan buffers can under-report free memory under heavy load, while SYCL backends on integrated Intel APUs can desynchronize memory tracking during continuous context evaluation.
Configuration that matters (the 3-5 settings people get wrong)
Understanding -ngl / --n-gpu-layers for layer offloading
Passing an integer to -ngl sets how many transformer layers offload to VRAM. Passing -ngl 99 offloads all model layers to the GPU. If VRAM is full, reduce the layer count to split execution between host RAM and VRAM.
Logical batch (-b) versus physical batch (-ub)
The -b parameter sets the logical batch size for prompt evaluation, while -ub controls the physical micro-batch size processed by the GPU in a single pass. If prompt processing causes GPU allocation spikes or driver crashes, lower -ub to keep peak VRAM usage under control.
Thread allocation (-t) and physical CPU core pinning
By default, CPU inference spawns threads matching logical core counts. Setting -t equal to physical CPU cores prevents cache thrashing. On asymmetric CPUs with mixed core types, restrict threads to performance cores to prevent thread migration stalls.
Multi-GPU orchestration with --tensor-split and --split-mode
When distributing a GGUF model across non-identical GPUs, pass proportional ratios to --tensor-split. Pair this setting with --split-mode layer to configure pipeline parallelism across separate GPU buses.
Server execution parameters (-c, --port)
The -c flag sets the total context window size in tokens, like -c 8192. Memory allocation scales with context size, so setting an oversized context reserves VRAM immediately on server startup. Use --port 8080 to bind llama-server to your port.
Common errors and how to fix them
PCIe bus saturation during partial offload
When running models with intermediate -ngl values, token generation slows because layer tensors cross the PCIe bus on every forward pass. Offloading all layers to VRAM eliminates host-to-device transfer overhead.
llama-server memory leaks on repeated requests
Long-running llama-server instances can leak memory over repeated incoming requests. Over extended inference runs, this accumulation leads to host OOM crashes. Fix this in production by running the binary under a systemd daemon set to restart periodically, or by setting up container recycling.
Antivirus false positives on Windows
Pre-compiled release binaries on Windows can trigger false-positive alerts in security software due to direct memory allocation calls. Resolve this by adding a folder exclusion rule or compiling locally from source using Visual Studio Build Tools.
Compilation OOMs and missing library headers
Compiler crashes during compilation usually stem from RAM exhaustion caused by multi-threaded build jobs. Limit build threads by adding -j2 or -j1 to the build command. On ROCm or SYCL setups, verify driver headers like hip/hip_runtime.h are present in your environment PATH before running CMake.
How it compares with the alternatives
Compared to high-throughput engines like vLLM, llama.cpp focuses on single-user latency and CPU/GPU hybrid execution rather than high-concurrency PagedAttention batching.
Against packaged options like Ollama or desktop tools like LM Studio, llama.cpp gives direct access to execution flags without abstraction layers or background daemon overhead.
For specialized self-hosting, tools like KoboldCpp build on llama.cpp to add storytelling APIs, while web interfaces like text-generation-webui and Open WebUI use llama.cpp or its server binary as an underlying inference backend. Raw weights can be downloaded from Hugging Face Hub before quantizing or running them locally.
Who should use it (and who should not)
llama.cpp is best for operators running local models on consumer GPUs, Apple Silicon, or mixed hardware. It is the right choice when you need direct control over physical micro-batching (-ub), context limits (-c), and layer offloading (-ngl).
It is not built for multi-tenant API serving where high parallel concurrency is required; engines like vLLM provide better throughput for concurrent loads. Users who want a single-click installer without command-line configuration should use a desktop app instead.
The short version
llama.cpp is the core C/C++ engine for bare-metal GGUF model execution across varied hardware. Tuning flags like -ngl for GPU offloading, -ub for micro-batch sizes, and --tensor-split for multi-GPU balancing enables efficient hardware utilization. Refer to the llama.cpp documentation for complete endpoint and flag details.
Setup
Install llama.cpp
Copy-paste starting points. Always read the project's own instructions before running anything with elevated permissions.
Homebrew (macOS)
llama.cpp
brew install llama.cpp
Build with CUDA
Build with CUDA
cmake -B build -DGGML_CUDA=ON && cmake --build build --config Release -j
Drop -DGGML_CUDA=ON for a CPU-only build.
Chat with a local GGUF
llama.cpp
llama-cli -m ./model.Q4_K_M.gguf -ngl 99 -c 4096
OpenAI-compatible server
llama.cpp
llama-server -m ./model.Q4_K_M.gguf -ngl 99 -c 8192 --port 8080
Quick start
Getting the first model running
llama.cpp is two things: a library and a set of binaries. The two you will use are llama-cli for a terminal chat and llama-server for an OpenAI-compatible HTTP endpoint.
The flag that matters most is -ngl (number of GPU layers). Set it high to push everything onto the card; lower it until the model loads if you are short on VRAM. -c sets context length, which controls KV-cache size and is the other lever when memory is tight.
FAQ
llama.cpp questions people actually ask
Short answers to the problems that come up most often.
What does the -ngl flag actually do?
-ngl by a few layers is often the difference between loading and an out-of-memory error.Which GGUF quantization should I download?
Why do I get 'failed to allocate' or CUDA out-of-memory errors?
-c, lower -ngl so some layers stay on the CPU, pick a smaller quantization, or close whatever else is holding GPU memory — a browser and desktop compositor can hold a surprising amount.Does llama.cpp run without a GPU?
Why is llama.cpp so slow even though I have a GPU?
-ngl 99 (or 999) to push every layer that fits onto the GPU. Then watch nvidia-smi: if utilisation is still zero, the binary was built without CUDA/Metal/ROCm, or the model is larger than free VRAM and fell back.Can I convert a Hugging Face model to GGUF myself?
convert_hf_to_gguf.py in the repository, followed by llama-quantize to produce the quantized file. Most popular models already have community GGUF builds on Hugging Face, so check there before spending the compute.Alternatives
Compared with other inference engine options
Same job, different tradeoffs.
Ollama
One-command local model runner with an OpenAI-compatible API and automatic GPU offload.
inference engineKoboldCpp
Single-executable llama.cpp fork with a built-in web UI, aimed at storytelling and long-form generation.
inference servervLLM
High-throughput serving engine with PagedAttention and continuous batching, built for concurrent requests.
desktop appLM Studio
Desktop app for discovering, downloading, and chatting with local models, with a built-in OpenAI-compatible server.
web UItext-generation-webui
Gradio web interface that can drive several inference backends, with LoRA training and extensions built in.
Next step
A runner is only half the decision. Check which models fit your GPU with the VRAM calculator, then follow a setup guide or pick a ready-made stack.