OpenCode安装指南2026
OpenCode是最火的开源编程智能体,但安装文档默认你懂生态。
OpenCode Install Guide 2026: Setup in 5 Minutes on Windows, Mac and Linux
OpenCode is an open-source AI coding agent for the terminal (191,500 GitHub stars). This guide walks through installation on all three platforms and connecting your first model.
Prerequisite: Node.js 20+
node --version
OpenCode is distributed via npm. Install Node.js LTS if missing.
Install
npm install -g opencode
That is the whole install. Verify:
opencode --version
Connect a Model
On first run, OpenCode asks which provider to use. Three fastest paths:
Path A - Anthropic (best quality):
opencode auth login
# follow the browser prompt to authorize
Path B - OpenRouter (one key, many models):
export OPENROUTER_API_KEY=sk-or-...
opencode --model openrouter/anthropic/claude-sonnet-4
Path C - Free local model via Ollama:
ollama pull qwen2.5-coder:7b
opencode --model ollama/qwen2.5-coder:7b
First Session
cd /path/to/your/project
opencode
Type a task: "explain the architecture of this repo and add a test for the auth module". OpenCode reads the repo, proposes changes, and asks for approval before editing.
Useful Flags
| Flag | Purpose |
|---|---|
| opencode --model |
Choose model per session |
| opencode --help | All options |
| opencode sessions | List previous sessions |
| opencode --print | Non-interactive output (scripting) |
FAQ
Does OpenCode work on Windows? Yes, native Windows via Node. Some MCP servers prefer WSL2, but core features work fine.
How much does it cost? The tool is free; you pay model API usage (a few cents per session with small models).
Can I use it with VS Code? Yes, there is a VS Code extension that embeds OpenCode.
