快速安装指南
🎯 安装概览
OpenCode 支持 75+ 种 LLM 提供商,无论你是新手还是专家,都能快速上手。
📋 系统要求
最低配置
- 操作系统:Windows 10+、macOS 10.15+、Linux (Ubuntu 18.04+)
- Node.js:版本 16 或更高
- 内存:建议 4GB 或更多
- 存储:至少 500MB 可用空间
推荐配置
- 操作系统:Windows 11、macOS 13+、最新版 Linux
- Node.js:版本 18 或更高
- 内存:8GB 或更多
- 存储:2GB 或更多(用于缓存)
🚀 快速安装(3种方式)
方式一:全局安装(推荐)
bash
# 使用 npm
npm install -g @opencode/cli
# 使用 pnpm(更快)
pnpm add -g @opencode/cli
# 使用 yarn
yarn global add @opencode/cli方式二:项目级安装
bash
# 在项目目录中
npm install @opencode/cli
# 使用 npx(无需安装)
npx @opencode/cli方式三:源码安装(开发者)
bash
# 克隆仓库
git clone https://github.com/opencode-cn/opencode.git
# 进入目录
cd opencode
# 安装依赖
npm install
# 链接全局
npm link⚙️ 初始化配置
自动初始化(推荐新手)
bash
# 运行向导式配置
opencode init向导会询问:
- 🎯 使用目的:开发、写作、学习等
- 🌐 网络环境:国内直连、代理、离线
- 🤖 模型偏好:免费、付费、本地
- 🎨 主题设置:深色、浅色、自动
手动配置(推荐高级用户)
基础配置文件
bash
# 创建配置文件
opencode config set auto_init true
opencode config set default_provider openai
opencode config set timeout 30000高级配置示例
json
{
"providers": {
"primary": "openai",
"fallback": ["anthropic", "zhipu"]
},
"network": {
"proxy": "http://127.0.0.1:7890",
"timeout": 30000,
"retries": 3
},
"ui": {
"theme": "auto",
"language": "zh-CN",
"show_line_numbers": true
},
"performance": {
"cache_size": "1GB",
"max_tokens": 4000,
"stream": true
}
}🌐 网络配置详解
国内用户优化
1. 直连国产模型
bash
# 智谱 AI
opencode config set provider zhipu
opencode config set api_key your_zhipu_api_key
# DeepSeek
opencode config set provider deepseek
opencode config set api_key your_deepseek_api_key
# MiniMax
opencode config set provider minimax
opencode config set api_key your_minimax_api_key2. 代理配置
bash
# HTTP 代理
opencode config set proxy http://127.0.0.1:7890
# SOCKS5 代理
opencode config set proxy socks5://127.0.0.1:1080
# 系统代理
opencode config set proxy system3. 自定义镜像
bash
# OpenAI 自定义端点
opencode config set openai_base_url https://api.openai.com/v1
opencode config set openai_base_url https://api.openai-proxy.com/v1
# 兼容性配置
opencode config set compatible_mode true国外用户配置
bash
# OpenAI 官方
opencode config set provider openai
opencode config set api_key your_openai_api_key
# Claude
opencode config set provider anthropic
opencode config set api_key your_anthropic_api_key
# Google Gemini
opencode config set provider google
opencode config set api_key your_google_api_key🤖 本地模型配置
Ollama 集成
bash
# 安装 Ollama
curl -fsSL https://ollama.ai/install.sh | sh
# 配置 OpenCode 使用 Ollama
opencode config set provider ollama
opencode config set ollama_base_url http://localhost:11434
# 下载模型(首次使用)
ollama pull llama2
ollama pull codellamaLM Studio 集成
bash
# 配置 LM Studio
opencode config set provider lmstudio
opencode config set lmstudio_base_url http://localhost:1234
# 支持的模型格式
opencode config set model_format gguf✅ 验证安装
基础验证
bash
# 检查版本
opencode --version
# 输出示例:OpenCode CLI v2.1.0
# 检查配置
opencode config list
# 显示当前所有配置
# 健康检查
opencode doctor
# 检查环境兼容性连接测试
bash
# 测试与提供商连接
opencode test --provider zhipu
# 测试特定模型
opencode test --model llama2
# 批量测试
opencode test --all功能测试
bash
# 启动交互模式
opencode
# 测试基本对话
输入:你好 OpenCode!
# 测试代码生成
输入:写一个 Python 的 Hello World
# 测试文件操作
输入:帮我读取当前目录的文件列表🎯 常见问题解决
安装问题
问题:权限不足
bash
# macOS/Linux
sudo chown -R $(whoami) /usr/local/bin/opencode
# Windows(以管理员身份运行)
# 右键点击终端,选择"以管理员身份运行"问题:网络连接失败
bash
# 测试网络连接
curl -I https://api.openai.com/v1/models
# 配置代理
opencode config set proxy http://your-proxy:port
# 使用镜像
opencode config set openai_base_url https://api.openai-proxy.com/v1问题:模型响应慢
bash
# 调整超时设置
opencode config set timeout 60000
# 启用流式响应
opencode config set stream true
# 限制并发请求
opencode config set max_concurrent 3配置问题
问题:配置文件错误
bash
# 重置配置
opencode config reset
# 查看配置文件位置
opencode config show config_path
# 手动编辑配置
opencode config edit问题:环境变量冲突
bash
# 清理环境变量
unset OPENAI_API_KEY
unset ANTHROPIC_API_KEY
# 使用配置文件管理密钥
opencode config set secure_storage true📚 进阶配置
工作区管理
bash
# 创建专用工作区
mkdir my-project && cd my-project
opencode init --workspace
# 配置项目特定设置
opencode config set project_name "My Project"
opencode config set workspace_type "development"插件系统
bash
# 安装常用插件
opencode plugin install git-helper
opencode plugin install docker-assistant
opencode plugin install database-tool
# 启用/禁用插件
opencode plugin enable git-helper
opencode plugin disable docker-assistant
# 查看已安装插件
opencode plugin list自定义指令
bash
# 创建自定义指令
opencode alias create "review" "请帮我审查这段代码的质量和安全性"
# 查看所有指令
opencode alias list
# 删除指令
opencode alias delete review🎉 安装完成!
下一步学习路径
现在你已经成功安装了 OpenCode,建议按以下路径继续学习:
🚀 如果你是完全新手
💻 如果你是开发者
✍️ 如果你是内容创作者
📊 如果你是效率追求者
🔗 有用资源
官方资源
- 📖 完整文档
- 🎥 视频教程
- 🐙 GitHub 仓库
- 💬 Discord 社区
社区资源
学习资源
🎊 恭喜你成功加入 OpenCode 大家庭!
现在你已经掌握了安装的所有技能,下一步就是开始实践。记住,最好的学习方式就是多使用、多探索、多创造!
立即开始你的 OpenCode 之旅吧! 🚀

