🤖 AI Tools
· 6 min read
Last updated on

How to Use OpenCode with Ollama — Free Local AI Coding Setup


OpenCode + Ollama gives you a powerful AI coding agent that runs entirely on your machine. No API keys, no costs, no data leaving your network. OpenCode’s LSP integration and agentic capabilities make it one of the best terminal-based coding assistants for local models.

What is OpenCode

OpenCode is an open-source, terminal-based AI coding agent — similar to Aider or Claude Code but designed from the ground up for local model support. Key features:

  • LSP integration — connects to your language server for type-aware edits
  • Agentic workflows — can read files, run commands, and iterate on solutions
  • Multi-file editing — handles cross-file refactoring
  • Git integration — automatic commits with descriptive messages
  • Session persistence — resume previous conversations
  • Tool use — file operations, shell commands, search

The LSP integration is what sets OpenCode apart for local models. Because the language server provides type information, even a 27B model can do type-safe refactoring that would normally require a much larger model to infer.

Setup steps

1. Install Ollama

# macOS
brew install ollama

# Linux
curl -fsSL https://ollama.ai/install.sh | sh

# Start the service (runs in background)
ollama serve

2. Pull a coding model

# Best quality (16GB+ VRAM)
ollama pull qwen3.5:27b

# Great for coding specifically (14GB+ VRAM)
ollama pull devstral-small:24b

# Lower VRAM option (5GB+)
ollama pull qwen3.5:9b

3. Install OpenCode

# Via install script
curl -fsSL https://opencode.ai/install.sh | sh

# Or via Go
go install github.com/opencode-ai/opencode@latest

4. Configure OpenCode for Ollama

Create or edit ~/.opencode/config.json:

{
  "providers": {
    "ollama": {
      "baseUrl": "http://localhost:11434",
      "models": ["qwen3.5:27b", "devstral-small:24b", "qwen3.5:9b"]
    }
  },
  "defaultModel": "ollama/qwen3.5:27b",
  "editor": {
    "tabSize": 2,
    "formatOnSave": true
  }
}

5. Start coding

cd your-project
opencode

OpenCode launches in your terminal with an interactive interface. Type your request and it will read files, make edits, and run commands as needed.

Configuring models

Project-level configuration

Create .opencode.json in your project root to override global settings:

{
  "defaultModel": "ollama/devstral-small:24b",
  "systemPrompt": "You are working on a TypeScript React project. Use functional components and hooks.",
  "context": {
    "include": ["src/**/*.ts", "src/**/*.tsx"],
    "exclude": ["node_modules", "dist", "*.test.ts"]
  }
}

Model-specific settings

{
  "providers": {
    "ollama": {
      "baseUrl": "http://localhost:11434",
      "models": ["qwen3.5:27b"]
    }
  },
  "modelSettings": {
    "ollama/qwen3.5:27b": {
      "temperature": 0.2,
      "maxTokens": 4096,
      "contextWindow": 32768
    }
  }
}

Remote Ollama instance

If Ollama runs on a different machine (e.g., a GPU server on your network):

{
  "providers": {
    "ollama": {
      "baseUrl": "http://192.168.1.100:11434"
    }
  }
}

Best models for OpenCode

OpenCode works best with models that have strong instruction-following and can handle structured tool-use patterns. Here are the top choices:

ModelVRAMCoding qualitySpeedWhy it works
Qwen 3.5 27B16 GBExcellent~25 tok/sBest instruction-following
Devstral Small 24B14 GBExcellent~28 tok/s256K context, code-specialized
Gemma 4 27B16 GBVery good~25 tok/sStrong reasoning
Qwen 3.5 9B5 GBGood~40 tok/sFast, low VRAM
DeepSeek Coder V2 16B10 GBGood~32 tok/sCode-specialized

Top pick: Qwen 3.5 27B for its combination of coding ability and instruction-following. OpenCode’s agentic workflow requires the model to correctly interpret tool outputs and decide next steps — Qwen 3.5 handles this reliably. For models needing more VRAM than your local setup provides, cloud GPU providers offer on-demand access to A100s and H100s.

Runner-up: Devstral Small 24B if you primarily do coding tasks. Its 256K context window means it can hold more of your codebase in memory, and it’s specifically trained for agentic coding workflows.

Using OpenCode effectively

Basic commands

Once inside OpenCode’s interactive session:

> Fix the type error in src/auth/login.ts
> Add input validation to the signup form
> Refactor the database module to use connection pooling
> Write a migration to add the 'status' column to orders

OpenCode will read the relevant files, propose changes, and apply them after confirmation.

Working with context

> /add src/models/user.ts        # Add file to context
> /read src/types/index.ts       # Read-only reference
> /clear                          # Clear context
> /files                          # Show files in context

Running commands

OpenCode can execute shell commands as part of its workflow:

> Run the tests and fix any failures
> Install the missing dependency and update the import

It will run npm test, read the output, identify failures, and fix them iteratively.

Comparison with Aider + Ollama

Both are excellent terminal-based AI coding tools that work with Ollama. Here’s how they differ:

FeatureOpenCodeAider
InterfaceTUI (interactive)CLI (conversational)
LSP integration✅ Native❌ No
Agentic mode✅ Built-in⚠️ Limited
Git integration✅ Auto-commit✅ Auto-commit
Multi-file edits✅ Strong✅ Strong
Edit formatTool-basedDiff/whole file
MaturityNewerMore established
Model compatibilityGoodExcellent (more models tested)
CommunityGrowingLarge

Choose OpenCode if: You want LSP-powered type-safe edits, prefer a TUI interface, or need agentic workflows where the tool iterates autonomously.

Choose Aider if: You want maximum model compatibility, a proven track record, or prefer the conversational CLI style. Aider has been tested with more models and has a larger community.

Both work great with Ollama. You can even install both and use whichever fits the task.

Performance tips

1. Use a code-specialized model

General-purpose models work, but code-specialized models (Devstral Small, DeepSeek Coder) produce better results for pure coding tasks.

2. Keep context focused

Local models have limited context windows. Only add files directly relevant to your task:

> /clear
> /add src/auth/login.ts
> /read src/auth/types.ts
> Fix the authentication bug where expired tokens aren't rejected

3. Set appropriate context window

In your config, set contextWindow to match your model’s actual capability:

{
  "modelSettings": {
    "ollama/qwen3.5:27b": {
      "contextWindow": 32768
    }
  }
}

4. Use GPU acceleration

Ensure Ollama is using your GPU. Check with ollama ps. If it shows CPU, verify your GPU drivers are installed.

5. Consider a dedicated GPU server

If your development machine has limited VRAM, run Ollama on a separate machine with a better GPU and point OpenCode to it via the baseUrl config.

Troubleshooting

“Connection refused” error: Ollama isn’t running. Start it with ollama serve.

Model not found: Pull the model first: ollama pull qwen3.5:27b. Check available models with ollama list.

Slow responses: Verify GPU is being used (ollama ps). CPU inference is 5-10x slower.

Context too long errors: Reduce the number of files in context, or switch to a model with a larger context window (Devstral Small has 256K).

Tool use failures: Some smaller models (7B and below) struggle with OpenCode’s tool-use format. Use 14B+ models for reliable agentic behavior.

FAQ

Is OpenCode free?

Yes, completely. OpenCode is open-source (MIT license) and free to use. Combined with Ollama (also free), you get a full AI coding agent with zero cost. There are no premium tiers, no token limits, and no telemetry. The only cost is the hardware to run the local model — any machine with 8+ GB VRAM (or a Mac with 16+ GB RAM) works.

Which Ollama model works best with OpenCode?

Qwen 3.5 27B is the top recommendation — it has the best combination of coding ability and instruction-following needed for OpenCode’s agentic workflow. If you’re VRAM-constrained, Devstral Small 24B (14 GB) is excellent for coding-specific tasks and has a massive 256K context window. For machines with only 8 GB VRAM, Qwen 3.5 9B provides good results at faster speeds. Avoid models smaller than 9B — they struggle with OpenCode’s tool-use patterns.

How does OpenCode compare to Aider?

Both are strong terminal-based AI coding tools. OpenCode’s advantages: native LSP integration for type-safe edits, built-in agentic mode, and a TUI interface. Aider’s advantages: more mature, tested with more models, larger community, and better documentation. For local Ollama usage specifically, both work well with 14B+ models. OpenCode edges ahead on complex refactoring (thanks to LSP), while Aider is more reliable across a wider range of models and tasks. See our Aider + Ollama guide for comparison.

Related: OpenCode Complete Guide · Ollama Complete Guide · Aider with Ollama Setup · Best AI Models for Coding Locally