Local LLM Deployment Guide: Model Selection, Tools, and Inference Tuning
This guide explains how to plan local LLM deployment around hardware memory, choose quantization and model size, get started quickly with Ollama, and fine-tune inference with llama.cpp. It covers GPU layer offload, context length and KV cache sizing, Flash Attention, chat templates, performance measurement, troubleshooting, and security considerations.
1. Start with the Hardware Budget
The first step in local deployment is to determine whether the hardware can fit the model. Runtime memory usage is roughly the sum of three parts: the model weight file size, the KV cache used to store historical context, and runtime overhead, which usually requires 1–2GB.
Quantization determines the weight file size. The following values are estimates for common GGUF quantization files; actual sizes should be checked on the model page:
| Parameter size | Q4_K_M (approx.) | Q5_K_M (approx.) | Q8_0 (approx.) |
|---|---|---|---|
| 7B | 4.7GB | 5.1GB | 7.7GB |
| 14B | 9GB | 10.5GB | 15.7GB |
| 32B | 20GB | 23GB | 35GB |
A few practical rules:
- A GPU with 12–16GB of VRAM is suitable for running a 14B Q4 quantized model.
- A 32B Q4 model requires roughly 20GB or more of VRAM, or 32GB or more of unified memory, such as on an Apple silicon Mac. If VRAM is insufficient, some layers can be offloaded to the CPU, but speed will drop noticeably.
- When running on CPU only, a 7B model can run on a machine with 16GB of RAM, but generation speed depends on memory bandwidth and core count. Before production, benchmark it with
llama-benchincluded with llama.cpp. - In terms of quantization precision, Q4 and above cause little quality loss in most tasks. Below Q3, output quality degradation is usually more obvious. After choosing a model, validate it with your own real tasks.
2. Quick Deployment with Ollama
If the goal is to get a local model running as quickly as possible, Ollama is the lowest-barrier option. It handles model downloads, version management, and provides a compatible HTTP interface. Linux users can install it with the official installation script, while Windows and macOS users can download installers from the official website.
# Pull a model with the desired specification; 7b and 14b in the tag indicate parameter sizeollama pull qwen2.5:7b# Chat directly in the terminalollama run qwen2.5:7b# List downloaded modelsollama list
After the model starts, you can call it through the API:
curl http://localhost:11434/api/generate \-d '{"model":"qwen2.5:7b","prompt":"Introduce yourself in one sentence","stream":false}'
By default, Ollama only listens on the local address 127.0.0.1. If other machines on the LAN need access, you can set the OLLAMA_HOST environment variable. Doing so expands the service exposure, so you must also configure firewall rules or an authenticated reverse proxy.
3. Fine-Tuning with llama.cpp
Ollama is suitable for quick start, but if you need to control VRAM allocation, context length, and startup parameters, using llama.cpp directly is more appropriate. llama.cpp is an inference engine written in C/C++, and its llama-server can wrap a model as an HTTP service compatible with the OpenAI API.
Windows users can download a package matching their GPU backend from the llama.cpp GitHub Releases page, such as CUDA, Vulkan, or CPU-only builds. After extraction, you will get llama-server.exe. The following command runs in Windows Command Prompt, where ^ is the line continuation character. If you use PowerShell, the continuation character should be a backtick.
llama-server.exe ^-m "D:\models\qwen2.5-14b-instruct-q4_k_m.gguf" ^-ngl 99 ^-c 16384 ^-n 2048 ^-fa on ^--jinja ^--host 127.0.0.1 ^--port 8080
After the service starts, you can test it with the following command on Linux or macOS:
curl http://127.0.0.1:8080/v1/chat/completions \-H "Content-Type: application/json" \-d '{"messages":[{"role":"user","content":"Hello"}]}'
Parameter Details
1. Model file (-m)
Points to the local GGUF file path. If the path contains spaces or backslashes, wrap it in double quotes. Compare the file against the checksum provided on the model page to confirm the download is complete.
2. GPU layer offload (-ngl)
Determines how many neural network layers are placed on the GPU for computation. Setting the value to 99 or 999 means offloading as many layers as possible to the GPU. This usually reduces time to first token and improves generation speed noticeably. If startup reports insufficient VRAM, lower this value so the remaining layers are computed by the CPU.
3. Context length (-c) and KV cache-c defines the context length the model can hold at once, in tokens. Two things need to be distinguished here. KV cache VRAM usage grows linearly with context length. The amount of attention computation grows quadratically with length, which affects time to first token when processing long prompts, not VRAM.
KV cache size can be estimated as follows:
per-token usage = 2 × layers × KV heads × head dimension × bytes per element
For a model with 32 layers, 8 KV heads, head dimension 128, and FP16 cache, each token uses about 128KiB. With -c 16384, the KV cache requires about 2GiB. The number of layers, KV heads, and head dimension can be found in the GGUF metadata or model documentation. Values can vary widely between model architectures.
If VRAM is tight, you can quantize the KV cache with -ctk q8_0 -ctv q8_0, which roughly halves VRAM usage. This usually causes a small loss in generation quality. Note that quantizing the V cache requires Flash Attention to be enabled at the same time.
4. Maximum generated tokens (-n)
Limits the maximum number of tokens generated in a single response. It can prevent the model from falling into repetitive output and also controls the duration of a single request.
5. Flash Attention (-fa)
Reduces memory reads and writes during attention computation, with a more noticeable effect in long-context scenarios. Newer versions of llama.cpp use -fa on, while older versions only need -fa. Check llama-server --help for the exact syntax.
6. Chat template (—jinja)
When enabled, the server assembles system, user, and assistant messages according to the model’s built-in Jinja template. If the output shows role confusion or format issues, first confirm that this parameter is effective and that the llama.cpp version you use supports it.
7. Listen address and port (—host, —port)--host 127.0.0.1 allows only local access and is the default safe choice. For LAN access, you can change it to 0.0.0.0, add the --api-key parameter to set an access key, and allow only trusted addresses in the firewall.
How to Measure Performance
Before and after tuning, compare results using the same prompt length, the same hardware, and the same model. Two common metrics are time to first token (TTFT) and generation speed (tokens/s). Throughput can be measured with llama-bench. TTFT needs to be recorded from a streaming request by measuring when the first data chunk is returned. Warm up once before testing to avoid mixing model first-load time into the results. When reporting performance numbers, include the GPU model, driver version, quantization method, and context length.
Common Troubleshooting
- Startup reports out of memory or CUDA errors: Lower
-nglor-c, or switch to a smaller quantization file. - Port already in use: On Windows, use
netstat -ano | findstr 8080to find the process; on Linux, usess -ltnp | grep 8080. - Model fails to load: Check whether the file was downloaded completely, whether the path is correct, and compare it with the checksum on the model page.
- Garbled output or broken chat format: Confirm that
--jinjais effective and check whether the model matches the chat template.
4. Security and Compliance
Local deployment reduces the amount of data passing through external networks, but that does not mean the data is already secure.
- By default, the service should listen only on localhost. If LAN access is truly needed, set an access key and restrict source addresses. Do not expose the inference port directly to the public internet.
- Every model has its own license agreement. Some licenses restrict commercial use or require attribution, so confirm each one before deployment.
- Review the tools and plugins you use.
