系统要求

OpenCode 支持 macOS、Linux 和 Windows 系统,建议使用 Node.js 18+ 和 Python 3.10+

Node.js

版本 18.0.0 或更高

Python

版本 3.10 或更高

npm / pip

包管理器

终端

命令行环境

npm 安装

通过 npm 安装 OpenCode SDK,这是最简单的方式:

Bash
# 安装 OpenCode SDK
npm install @opencode-ai/sdk @opencode-ai/plugin

# 或者全局安装 CLI
npm install -g @opencode-ai/cli

提示:全局安装 CLI 后,您可以直接在终端使用 opencode 命令

pip 安装

对于 Python 技能开发,可以使用 pip 安装:

Bash
# 安装 Python 技能依赖
pip install python-pptx pandas Pillow

# 安装 OpenCode Python SDK(如果需要)
pip install opencode-ai

配置文件

创建并配置 OpenCode 的主配置文件 ~/.config/opencode/opencode.json

JSON
{
  "$schema": "https://opencode.ai/config.json",
  "permission": {
    "read": { "*": "allow" },
    "write": { "*": "allow" },
    "bash": {
      "*": "allow",
      "rm -rf": "deny",
      "rm -r ": "deny",
      "rm -f ": "deny",
      "rm ": "allow",
      "trash": "allow"
    },
    "session_list": "allow",
    "session_read": "allow",
    "session_search": "allow",
    "session_info": "allow",
    "webfetch": "allow",
    "websearch": "allow",
    "lsp": { "*": "allow" },
    "mcp": { "*": "allow" }
  },
  "model": "minimax-cn/MiniMax-M2.1",
  "instructions": [
    "~/.config/opencode/PROJECT_GUIDELINES.md"
  ],
  "plugin": ["oh-my-opencode"],
  "provider": {
    "minimax-cn": {
      "npm": "@ai-sdk/anthropic",
      "options": {
        "baseURL": "https://api.minimaxi.com/anthropic/v1",
        "apiKey": "your-api-key"
      },
      "models": {
        "MiniMax-M2.1": {
          "name": "MiniMax-M2.1 (sun)"
        }
      }
    }
  }
}

注意:请将 apiKey 替换为您的实际 API 密钥

技能安装

OpenCode 的技能系统允许您扩展功能。技能注册位置:~/.config/opencode/skill/

1

创建技能配置

mkdir -p ~/.config/opencode/skill/my-skill
2

创建 skill.yaml

name: my-skill
description: 技能描述
author: 作者
version: 1.0.0
permissions:
  - read
  - write
  - bash
requires:
  python: ">=3.10"
3

复制代码到注册目录

cp -r ~/codebase/skills/my-skill/src ~/.config/opencode/skill/my-skill/

验证安装

安装完成后,运行以下命令验证安装:

Bash
# 检查版本
opencode --version

# 检查配置
opencode config check

# 运行测试任务
opencode run "Hello World"

常见问题

安装失败,提示权限错误

尝试使用 sudo 权限或检查 npm 的全局安装路径权限:

# 设置 npm 全局路径
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'

# 添加到 PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
无法连接到 API 服务

检查网络连接和 API 密钥配置,确保配置文件中的 apiKey 正确无误。

技能加载失败

检查 skill.yaml 配置文件是否正确,确保所有依赖都已安装。