Running Small Open-Source Models Locally: Model Selection and Ollama Setup
Learn how to evaluate the cost of local inference, choose a model that fits your memory budget, and run it with Ollama. This guide covers installation, model downloads, CLI and API usage, memory estimates, common troubleshooting steps, and licensing and security considerations.
1. Why Run Models Locally—and When Does It Pay Off?
Over the past two years, open-source models have changed significantly in both capability and licensing. Some use permissive licenses such as Apache 2.0, which allow commercial use, modification, and redistribution. However, licensing is not consistent across models, and additional terms can vary considerably. Before using a model commercially or redistributing it, read its model card and full license.
Small models have also become more capable. Multimodal input, long context windows, and function calling—features once associated mainly with much larger models—are now available in some models with fewer than 10 billion parameters.
For frequent, relatively straightforward tasks such as everyday questions, text summarization, and structured information extraction, local inference offers two immediate benefits:
- No per-request API fees.
- Data does not need to be sent to an external service, reducing exposure.
Compare local and API costs
Whether local deployment saves money depends on your workload. Local costs include hardware depreciation, electricity, and maintenance time. API costs depend on pricing and usage.
Use the following template:
Monthly local cost =Hardware depreciation + Electricity + Maintenance hours × Hourly labor costHardware depreciation =Equipment purchase price ÷ Depreciation period in monthsElectricity =System power consumption (kW) × Operating hours per day × 30× Electricity price per kWhMonthly API cost =Monthly token volume in millions × Average price per million tokens
For a hypothetical example, using Chinese yuan (CNY):
- Equipment costs CNY 8,000 and is depreciated over 36 months: approximately CNY 222 per month.
- The system draws 0.15 kW, runs eight hours per day, and electricity costs CNY 0.6 per kWh: approximately CNY 22 per month.
- Maintenance takes two hours per month at CNY 100 per hour: CNY 200 per month.
The total local cost is approximately CNY 444 per month. If the average API price is CNY 2 per million tokens, monthly usage would need to exceed roughly 222 million tokens before local deployment might become cheaper.
Use three consecutive months of actual usage when making this comparison, rather than extrapolating from a single day. Cloud APIs are usually cheaper at low usage levels. Local deployment is more likely to make economic sense when usage is high and consistent.
Running locally does not automatically satisfy compliance requirements. You still need disk encryption, access controls, and sensitive-data redaction in logs. For user-facing services, also check whether the model license restricts the intended use.
2. Choose a Model Size Based on Available Memory
Parameter count is a major factor in model memory requirements. A rough estimate for weight storage is:
Weight storage ≈ Parameter count × Effective bits per parameter ÷ 8
For example, a 3B model at exactly 4 bits per parameter would theoretically require:
3 billion × 4 ÷ 8 = 1.5 GB
That estimate is optimistic. Four-bit quantization typically stores weights in blocks, with additional scale values and, in some formats, zero-point metadata. Common Q4 formats use approximately 4.5–4.8 effective bits per weight, so use that range for a more realistic estimate.
Model file size is not the same as runtime memory usage. You also need room for:
- KV cache: Stores attention keys and values and grows with context length.
- Runtime buffers and the Ollama process.
- The operating system and other applications.
Estimate KV cache requirements
A useful approximation is:
KV cache ≈2 × Number of layers × Number of KV heads × Head dimension× Context length × Bytes per element
The factor of two accounts for storing both keys and values.
Qwen2.5-7B, for example, has 28 layers, four KV heads using grouped-query attention (GQA), and a head dimension of 128. With fp16 storage at two bytes per element, its KV cache requires approximately 57 KB per token.
That translates to approximately:
- 0.47 GB for an 8,192-token context.
- 1.9 GB for a 32,768-token context.
Long-document workloads therefore need a separate allowance for KV cache memory.
Reference memory requirements
The following figures are approximate references for the default, roughly 4-bit quantized Qwen2.5 models available through Ollama.
| Model | Parameters | Approximate model file size | Minimum memory | Recommended memory | GPU guidance |
|---|---|---|---|---|---|
qwen2.5:0.5b |
0.5B | 0.4 GB | 4 GB | 8 GB | Not required |
qwen2.5:1.5b |
1.5B | 1.0 GB | 6 GB | 8 GB | Not required |
qwen2.5:3b |
3B | 1.9 GB | 8 GB | 16 GB | Not required; a discrete GPU can improve speed |
qwen2.5:7b |
7B | 4.7 GB | 12 GB | 16 GB | At least 8 GB VRAM recommended |
qwen2.5:14b |
14B | 9.0 GB | 16 GB | 32 GB | At least 12 GB VRAM recommended |
qwen2.5:32b |
32B | 20 GB | 32 GB | 48 GB | At least 24 GB VRAM recommended |
These are practical estimates based on Ollama’s default context settings, not guaranteed limits. Apple silicon uses unified memory, so consider the available unified memory when reading the table.
Check actual usage with ollama ps and your system monitor. Start with the minimum-memory estimate and allow at least 20% additional headroom.
The smallest model that meets your quality requirements is usually the most economical choice.
3. Install Ollama, Download a Model, and Run It
Ollama handles model downloads and loading, and exposes models through a local HTTP API.
Install Ollama
macOS: Download the installer from the official website, or install it with Homebrew:
brew install ollama
Linux: Download the installation script, inspect it, and then run it:
curl -fsSL https://ollama.com/install.sh -o install.shless install.shsh install.sh
Windows: Download the installer from the official website. After installation, run ollama in PowerShell.
Verify the installation:
ollama --version
You should see a version line similar to:
ollama version is 0.x.x
If the command is not found, reopen the terminal or check that the installation directory is in your PATH.
Alternatively, run Ollama in Docker. The following command binds the port to 127.0.0.1, allowing access only from the local machine:
docker run -d --name ollama -v ollama:/root/.ollama -p 127.0.0.1:11434:11434 ollama/ollama
Download a model
ollama pull qwen2.5:3bollama list
The pull command displays download progress. Once it finishes, ollama list shows the model name, ID, size, and modification time.
Models are generally stored under an Ollama model directory; the exact default location depends on how Ollama is installed and run. Use the OLLAMA_MODELS environment variable to move model storage to another disk.
Downloads require temporary space. Keep more than twice the model file size available on the target disk.
Prefer the official model library. If you use a third-party GGUF file, verify the publisher’s identity and check the file against the hash supplied by the publisher.
Run the model
ollama run qwen2.5:3b "Explain KV cache in two sentences."
This command prints the answer and exits. Running it without a prompt starts an interactive session; enter /bye to exit.
After running the model, check whether it is loaded and how execution is distributed between the CPU and GPU:
ollama ps
In the PROCESSOR column:
100% GPUindicates that the model is fully loaded on the GPU.100% CPUindicates CPU execution.- A mixed CPU/GPU percentage indicates that the model is split between the two.
Call the local API
Ollama listens at http://127.0.0.1:11434 by default. First, check that the service is reachable:
curl http://127.0.0.1:11434/api/tags
A JSON response containing a models field indicates that the service is responding. A connection-refused error generally means the service is not running or is not listening at that address.
Send a generation request:
curl http://127.0.0.1:11434/api/generate -d '{"model": "qwen2.5:3b","prompt": "List three suitable use cases for local model deployment.","stream": false,"options": {"num_ctx": 4096, "temperature": 0.2}}'
Key parameters include:
model: Required model name.stream: Set tofalseto receive a single complete JSON response.options.num_ctx: Context length. Larger values require more memory.options.temperature: Lower values generally produce more consistent output.keep_alive: How long the model remains loaded in memory; the default is approximately five minutes.
The response’s response field contains the generated text. A done value of true indicates that generation has finished.
For multi-turn conversations, use /api/chat and pass the conversation history in the messages array:
curl http://127.0.0.1:11434/api/chat -d '{"model": "qwen2.5:3b","messages": [{"role": "user","content": "Rewrite this in a formal tone: Let us wrap up the meeting here."}],"stream": false}'
You can also call the API from Python using requests:
import requestsresp = requests.post("http://127.0.0.1:11434/api/generate",json={"model": "qwen2.5:3b","prompt": "Hello","stream": False,},timeout=120,)print(resp.json()["response"])
Allow a timeout of at least 120 seconds, since the first request may need to load the model into memory.
4. Troubleshooting, Security, and Compliance
Out-of-memory errors
Common symptoms include:
- An out-of-memory error while loading the model.
- The operating system terminating the process.
- Extremely slow responses or long periods without output.
Check the loaded models and available memory:
ollama psfree -h
The SIZE column in ollama ps reports the loaded model’s size. On Linux, free -h shows memory availability. On macOS, check memory pressure in Activity Monitor.
Work through these steps in order:
- Switch to a smaller model.
- Reduce the context length with
num_ctx. - Close other memory-intensive applications.
- Remove models you no longer need.
For example:
ollama rm qwen2.5:14b
Confirm removal by checking that the model no longer appears in ollama list. Removing a downloaded model frees disk space; do not treat its disappearance from the list alone as confirmation that runtime memory usage has decreased. Check memory usage separately.
Failed or stalled downloads
Symptoms include an interrupted pull, progress stuck at a particular percentage, or a connection timeout.
First, check disk space:
df -h ~
Ensure the disk holding your models has more than twice the model file size available.
Then retry:
ollama pull qwen2.5:3b
Ollama supports resumable downloads, so retrying will generally continue from the interrupted point.
If your network requires a proxy, configure the proxy environment variables for the process running the Ollama service. Setting them only in your current terminal is not sufficient when Ollama runs as a separate service.
Keep local deployment secure
Local inference reduces the need to send data to external services, but it does not remove security or licensing obligations. Keep access restricted, encrypt stored data, redact sensitive information from logs, and review each model’s license before commercial use, redistribution, or deployment in a user-facing service.
