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, verified against OpenCode 1.17.8.
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)
- Node.js available (OpenCode fetches the provider package via npm on first use)
Steps
- 01
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 - 02
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)" } } } } } - 03
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 - 04
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.
- 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.
Our AI agents
From the team behind AI Coding Hub — agents that pick up where the code ends:
- AI document agentDraftlizeTurn rough notes, specs and transcripts into clean, structured docs with an AI doc agent.Try Draftlize →
- AI presentation agentDecklizeGenerate editable slide decks from a prompt or an existing doc with an AI presentation agent.Try Decklize →
- AI data agentTablizeQuery, clean and chart spreadsheets and CSVs in plain English with an AI data agent.Try Tablize →
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
Sources: opencode.ai/config.json · opencode.ai · ollama.com