Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode Getting Started Tutorial

OpenCode Getting Started Tutorial

OpenCode is an open-source AI coding agent that supports interacting with AI in the terminal (Terminal), desktop apps, and mainstream IDEs (such as VS Code) to complete code-related tasks.

OpenCode can help us understand codebases, write new features, refactor code, fix bugs, and more, greatly improving development efficiency.

OpenCode is similar to Claude's Code mode or Cursor's Agent feature, but fully open source, privacy-first, supporting multiple large language models (LLM), with an emphasis on the terminal experience.

OpenCode supports 75+ model providers, with built-in free models like GLM-4.7 and MiniMax M2.1, can connect to commercial models such as OpenAI, Anthropic, and Google, and can also configure local models (such as Llama 3), adapting on demand to different scenarios like lightweight scripts and complex architectures.

Full OpenCode tutorial reference:https://www.runoob.com/opencode/opencode-tutorial.html

Key characteristics

Two built-in Agent modes:

**Toolset:** bash execution, file read/write, grep search, LSP diagnostics, etc.

**Context awareness:** Automatically analyzes project structure and generates an AGENTS.md guide.

**Sharing and collaboration:** One-click generation of session share links.


Install OpenCode

OpenCode supports multi-platform installation on macOS / Windows / Linux.

The universal one-click install script, this is the simplest method:


curl -fsSL https://opencode.ai/install | bash

After installation, you should be able to run from the command line:


opencode --version

If the output shows version number information like 1.1.19, the installation succeeded.

Package manager installation

macOS / Linux

brew install opencode

or:

npm install -g opencode-ai

Windows

choco install opencode

or:



scoop bucket add extras
scoop install extras/opencode

Arch Linux

paru -S opencode-bin

OpenCode runs in the terminal; we can use the default system terminal or some good modern terminal tools:

Desktop app

OpenCode also provides a desktop application, which can be obtained directly from the release page or opencode.ai/download for download.

System platform Download installer
macOS (Apple silicon) opencode-desktop-darwin-aarch64.dmg
macOS (Intel chip) opencode-desktop-darwin-x64.dmg
Windows opencode-desktop-windows-x64.exe
Linux .deb, .rpm or AppImage format

Launch and use

Starting OpenCode only requires entering the launch command in the terminal:

opencode

The first launch will guide you through basic configuration:

After a successful launch you enter the TUI interface and can start using core features.

We can enter /models in the terminal to view available free models:

In the popup view, those with Free on the right are the free ones:

Configure API key and models

If you connect an AI provider's API key, such as OpenAI or Anthropic Claude, run:


opencode auth login

Or after starting in the terminal, enter:


/connect

Select a model. Follow the prompts to log in and paste your API Key.

You can also use the Zen A model collection (high-quality models officially recommended and tested by OpenCode), saving you the trouble of managing multiple external accounts.

If you no longer want to use it, you can exit with the following command:

/exit 

Basic Usage

Start OpenCode

Enter the project directory you want to work on:


cd /path/to/your/project
opencode

For example, we create the directory opencode-runoob-test:

mkdir opencode-runoob-test
cd opencode-runoob-test

Then execute the command:

opencode

If there are permission issues, you can use:

sudo opencode

This opens OpenCode's terminal interactive interface (TUI).

Project initialization

In the OpenCode interface, run:


/init

This generates a .opencode/ folder for storing the project's vectorized index and custom instructions.

It scans the code structure of the current directory and generates an AGENTS.md file to record project information.

In the opencode-runoob-test directory you can see the AGENTS.md file:

Then we describe your requirements in natural language to start a task:


在当前目录下创建一个登录页面

Next the LLM will start thinking and create a login page:

Generated files:

Ask to explain code

You can ask OpenCode about codebase details directly in natural language:

What features does the file @index.html contain

Here @ is used to reference file paths in the project.

Daily interaction

Interactive mode (scripted):

opencode -p "修复 login 函数中的 bug"

Built-in tools introduction

OpenCode's AI Agent operates on the codebase through the following tools (can be opencode.json used to control permissions: allow/deny/ask):

Custom tools and MCP (Model Context Protocol) servers support extensions (such as connecting to databases).

Advanced usage

Create a simple Node.js API

  1. Create a new directory:mkdir my-api && cd my-api
  2. Initialize:npm init -y
  3. Start OpenCode:opencode
  4. Input /init
  5. Ask: create an Express.js service supporting the /hello route that returns JSON { message: 'Hello World' }, and add a README.

oh-my-opencode

oh-my-opencode is a powerful plugin/extension layer designed for OpenCode. It upgrades a single AI agent into a multi-agent collaborative team, providing out-of-the-box advanced features.

GitHub repository:https://github.com/code-yeongyu/oh-my-opencode

Core highlights include:

Installation

It is recommended to let OpenCode complete the installation automatically; copy the following prompt and paste it into your OpenCode dialog:


按照以下说明安装和配置 oh-my-opencode:
https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/refs/heads/master/docs/guide/installation.md

After installation it works out of the box by default, with no extra configuration needed to experience most features.

Basic trigger

Add the keyword ultrawork (or abbreviated ulw) to the OpenCode prompt, for example:

ultrawork: 请帮我实现一个 React 组件,支持暗黑模式。

This activates full mode: the Sisyphus main agent takes over, automatically assigns subtasks to specialized agents, and executes in parallel (background codebase mapping, deep exploration, automatic refactoring, etc.) until the task is 100% complete.


OpenCode TUI Common Slash Commands Cheat Sheet

OpenCode's Slash commands (starting with / prefix) are mainly used in the terminal user interface (TUI) for quickly controlling sessions, configuration, and operations.

Core configuration and initialization

Command Description Alias/shortcut
/connect Add or configure an LLM provider (API Key) None
/init Create or update project AGENTS.md File (analyze codebase) Ctrl+X I
/models List available models and switch Ctrl+X M

Session management

Command Description Alias/shortcut
/new Start a new session (clear current) /clear / Ctrl+X N
/sessions List and switch sessions /resume / /continue / Ctrl+X L
/share Share current session (generate link) Ctrl+X S
/unshare Cancel sharing current session None
/compact Compress/summarize current session /summarize / Ctrl+X C

Edit and undo

Command Description Alias/shortcut
/undo Undo last operation (requires Git repo, supports file change rollback) Ctrl+X U
/redo Redo undone operation (requires Git repo) Ctrl+X R

View and assistance

Command Description Alias/shortcut
/details Toggle tool execution detail display Ctrl+X D
/thinking Toggle thinking/reasoning process visibility None
/theme List and switch themes Ctrl+X T
/help Show help dialog Ctrl+X H
/editor Compose messages with an external editor Ctrl+X E
/export Export current conversation as Markdown and open for editing Ctrl+X X

Exit

Command Description Alias/shortcut
/exit Exit OpenCode /quit / /q / Ctrl+X Q

Note:


OpenCode CLI Common Parameters Cheat Sheet

OpenCode's command-line interface (CLI) is mainly used to start the TUI (terminal interface), run prompts in non-interactive mode, or set basic options. Running by default opencode will directly start the interactive TUI.

The following are common global parameters (flags):

Parameters Abbreviation Description Example
--help -h Show help information (list all available parameters) opencode --help
--debug -d Enable debug mode (output more logs for troubleshooting) opencode -d
--cwd -c Specify the current working directory (switch to that path at startup) opencode -c /path/to/your/project
--prompt -p Non-interactive mode: run a single prompt directly and output the response (suitable for scripts/automation) opencode -p "修复这个 bug"
--output-format -f Output format in non-interactive mode (text or json, default text) opencode -p "解释代码" -f json
--quiet -q Hide loading animation (spinner) in non-interactive mode opencode -p "生成 README" -q

Basic Usage Example

Note:


Reference links