Guide
How to Run OpenCode With Local Models (Ollama)
OpenCode is model-agnostic, so you can point it at a local model running in Ollama instead of a cloud provider — useful for privacy, offline work, or zero per-token cost. This guide adds Ollama as an OpenAI-compatible provider in your OpenCode config and runs a coding session against it. The config and CLI steps were re-checked on OpenCode 1.18.27 on 2026-10-01.
Before you start
- OpenCode installed (see the install guide) — verify with: opencode --version
- Ollama installed and running (ollama serve), from ollama.com
- At least one model pulled in Ollama (e.g. ollama pull qwen2.5-coder)
- Enough memory for a long context: Ollama's OpenCode page (checked 2026-10-01) says OpenCode needs a context length of 64k or higher
- Node.js available (OpenCode fetches the provider package via npm on first use)
Steps
- 01
Shortcut: let Ollama launch OpenCode for you
Ollama's own integration page (checked 2026-10-01) documents a one-line path: ollama launch opencode starts OpenCode with an inline config for the Ollama model you select, so you can skip the config file entirely. The same page says OpenCode requires a context length of 64k or higher — pick a model that supports it. Use the manual steps below if you want the provider saved in opencode.json. We did not run this command ourselves (no Ollama on our test machine).
ollama launch opencode - 02
Pull a coding model in Ollama
Download a local model. A coding-tuned model like qwen2.5-coder works well; smaller tags (e.g. :7b) run on modest hardware. Confirm Ollama is serving on its default port 11434.
ollama pull qwen2.5-coder ollama list # confirm the model is present # Ollama serves an OpenAI-compatible API at http://localhost:11434/v1 - 03
Add Ollama as a provider in opencode.json
OpenCode reads config from ~/.config/opencode/opencode.json (global) or ./opencode.json (per-project). Add a provider that uses the OpenAI-compatible AI SDK package and points baseURL at your local Ollama endpoint. List each local model under "models" with the exact Ollama tag as the key.
{ "$schema": "https://opencode.ai/config.json", "provider": { "ollama": { "npm": "@ai-sdk/openai-compatible", "name": "Ollama (local)", "options": { "baseURL": "http://localhost:11434/v1" }, "models": { "qwen2.5-coder": { "name": "Qwen2.5 Coder (local)" } } } } } - 04
Confirm OpenCode sees the local model
List models for your new provider. Your local model appears as ollama/<tag> — that is the identifier you use to select it.
opencode models ollama # → ollama/qwen2.5-coder - 05
Run a coding task against the local model
Use the -m / --model flag in provider/model format for a one-shot run, or launch the TUI and pick the provider in the model selector. Everything stays on your machine — no cloud calls.
# One-shot, headless opencode run -m ollama/qwen2.5-coder "Explain what this repo does" # Or open the TUI and choose Ollama (local) in the model picker opencode
Troubleshooting
- Your model does not appear in opencode models ollama
- Check the baseURL includes the /v1 suffix (http://localhost:11434/v1) and that the "models" key exactly matches the Ollama tag from ollama list. Restart OpenCode after editing the config.
- Connection refused / fetch failed
- Ollama is not serving. Run ollama serve (or open the Ollama app) and confirm http://localhost:11434 responds. If you changed Ollama's port, update baseURL to match.
- The model answers, but tool calls (file edits, shell commands) fail
- This is usually the context window. OpenCode's provider docs say to try increasing num_ctx in Ollama, starting around 16k–32k, and Ollama's OpenCode page says OpenCode needs 64k or more (both checked 2026-10-01). Raise the context length for the model in Ollama, or choose a model that supports a longer context.
- Responses are slow or the model runs out of memory
- Local models are bound by your hardware. Use a smaller tag (e.g. qwen2.5-coder:7b), close other heavy apps, and prefer a quantized build. Large agentic tasks are slower locally than on a hosted model.
FAQ
- Does OpenCode work fully offline with local models?
- The coding sessions run against your local Ollama model with no cloud model calls. The one online step is the first run, when OpenCode fetches the provider package (@ai-sdk/openai-compatible) via npm; after that the model inference is local.
- Which local models can I use with OpenCode?
- Any model Ollama can serve — coding-tuned models like qwen2.5-coder or deepseek-coder, or general models like llama3.2. Because the provider is OpenAI-compatible, any local server that exposes an OpenAI-style /v1 endpoint (LM Studio, llama.cpp server) works the same way; just change baseURL.
- Where is the OpenCode config file?
- Global config is ~/.config/opencode/opencode.json (or opencode.jsonc); a project-level ./opencode.json overrides it for that directory. Add the provider block to whichever scope you want the local model available in.
- How do I select the local model after configuring it?
- Use opencode run -m ollama/<tag> "..." for a one-shot run, or launch opencode and pick "Ollama (local)" in the TUI model selector. The identifier is always provider/model, e.g. ollama/qwen2.5-coder.
Related