Skip to main content
Cristhian Villegas
DevOps10 min read0 views

What is OpenCode? Complete Installation and Getting Started Guide

What is OpenCode? Complete Installation and Getting Started Guide

What is OpenCode?

OpenCode is an AI-powered coding assistant built for the terminal. Unlike proprietary tools, OpenCode is fully open source (MIT license) and works with over 75 AI model providers, including Claude (Anthropic), OpenAI, Google Gemini, AWS Bedrock, and even local models via Ollama.

Written in Go with Bubble Tea for its terminal UI (TUI), OpenCode has earned over 138,000 GitHub stars and boasts an active community of 800+ contributors. It's the most popular open-source alternative to tools like Claude Code, Cursor, and GitHub Copilot CLI.

OpenCode terminal interface showing an active coding session

Source: OpenCode — GitHub Repository

Key fact: OpenCode is not tied to any AI provider. You can switch between Claude, GPT-4, Gemini, or local models without changing your workflow.

Why Choose OpenCode Over Other Tools?

The AI coding assistant ecosystem has grown rapidly, but OpenCode stands out for several reasons:

  • No vendor lock-in: Works with any LLM provider — you're not trapped in a closed ecosystem
  • Truly open source: MIT license, you can audit, modify, and contribute to the code
  • Terminal-first: An elegant TUI that doesn't require a browser or specific IDE
  • Extensible: Native support for MCP (Model Context Protocol), LSP, and custom skills
  • Smart agents: Two built-in agents (build and plan) plus the ability to create your own
  • Privacy: Your data stays on your machine — the code is auditable
FeatureOpenCodeClaude CodeGitHub Copilot CLI
Open source✅ MIT
Multi-provider✅ 75+❌ Claude only❌ GPT only
Local models✅ Ollama
Native TUI
Skills/plugins
MCP servers

Prerequisites

Before installing OpenCode, make sure you have:

  • Operating system: macOS, Linux, or Windows (with WSL or native)
  • Terminal: Any modern terminal emulator (iTerm2, Windows Terminal, Alacritty, Kitty, etc.)
  • API Key: From at least one provider — Anthropic, OpenAI, Google, or set up Ollama for local models
  • Git: Installed and configured (OpenCode works directly with repositories)
💡 Tip: If you don't want to spend on API keys, you can use Ollama with local models like Llama 3 or Mistral completely free.

Step-by-Step Installation

OpenCode offers multiple installation methods. Here are the main ones:

Method 1: Quick Install Script (Recommended)

The simplest method works on macOS and Linux:

bash
1# One-line installation
2curl -fsSL https://opencode.ai/install | bash

This script detects your OS and architecture, downloads the correct binary, and places it in your PATH.

Method 2: npm (Cross-platform)

bash
1# Install globally with npm
2npm i -g opencode-ai@latest
3
4# Verify the installation
5opencode --version

Method 3: Homebrew (macOS)

bash
1# Add the tap and install
2brew install anomalyco/tap/opencode

Method 4: From Source

bash
1# Clone the repository
2git clone https://github.com/anomalyco/opencode.git
3cd opencode
4
5# Build (requires Go 1.22+)
6go build -o opencode ./cmd/opencode
7
8# Move to PATH
9sudo mv opencode /usr/local/bin/

Custom Installation Directory

You can control where OpenCode is installed using environment variables:

bash
1# Option 1: OpenCode-specific variable
2export OPENCODE_INSTALL_DIR=$HOME/.local/bin
3
4# Option 2: Standard XDG directory
5export XDG_BIN_DIR=$HOME/.local/bin
6
7# Then run the installer
8curl -fsSL https://opencode.ai/install | bash

Initial Configuration

Once installed, you need to authenticate with at least one AI provider:

bash
1# Log in (opens an authentication flow)
2opencode auth login
3
4# Or set the API key directly as an environment variable
5export ANTHROPIC_API_KEY="sk-ant-..."
6export OPENAI_API_KEY="sk-..."

Credentials are stored securely in ~/.local/share/opencode/auth.json.

Official OpenCode logo

Source: OpenCode — GitHub Repository

Selecting the Default Model

Create a configuration file at your project root:

json
1{
2  "model": "anthropic/claude-sonnet-4-5",
3  "small_model": "anthropic/claude-haiku-4-5",
4  "permission": {
5    "ask": "allow"
6  }
7}

Save it as opencode.json in your project. OpenCode merges configurations in this priority order:

  1. Project configuration (opencode.json)
  2. OPENCODE_CONFIG environment variable
  3. Global configuration (~/.config/opencode/opencode.json)
  4. Remote organizational configuration

Getting Started: Your First Session

Navigate to any repository and run:

bash
1# Start OpenCode in interactive mode (TUI)
2cd my-project
3opencode

You'll see OpenCode's terminal interface where you can type natural language instructions. But first, initialize your project:

bash
1# Inside the TUI, run:
2/init

The /init command analyzes your project and generates an AGENTS.md file that helps OpenCode understand your codebase structure, conventions, and patterns.

OpenCode welcome screen in the terminal

Source: OpenCode — GitHub Repository

Basic Interaction Examples

bash
1# Ask for code explanation
2> Explain how authentication works in this project
3
4# Create a new feature
5> Add a POST /api/users endpoint that validates email and name
6
7# Fix a bug
8> The test in auth.test.ts fails with "undefined is not a function", fix it
9
10# Refactor code
11> Refactor the payment service to use the Strategy pattern

Non-Interactive Mode: Automation and Scripts

OpenCode also works without the TUI, ideal for CI/CD and automation:

bash
1# Run a prompt directly
2opencode run "Explain how the Observer pattern works in this project"
3
4# Use a specific agent
5opencode run --agent plan "Analyze this file and suggest improvements" --file src/auth.ts
6
7# Quiet mode (no spinner, useful for scripts)
8opencode run -q "List all API endpoints"
9
10# Combine with Unix tools
11opencode run -q "Generate a summary of the README" | pbcopy

Server Mode: Avoiding Cold Starts

bash
1# Start the server in the background
2opencode serve
3
4# Connect to the existing server (instant startup)
5opencode run --attach http://localhost:4096 "Explain this error"

Server mode keeps MCP servers loaded, eliminating startup time on each query.

The Two Built-in Agents

OpenCode ships with two default agents that cover the most common workflows:

The build Agent (Default)

This is the primary agent with full access to the file system and terminal. It can:

  • Read and write files
  • Execute terminal commands
  • Install dependencies
  • Run tests
  • Create commits

The plan Agent

This is a read-only agent that analyzes without modifying. Ideal for:

  • Reviewing code before making changes
  • Generating implementation plans
  • Exploring the codebase without risk
  • Analyzing architecture
bash
1# Use the plan agent explicitly
2opencode run --agent plan "Create a plan to migrate from REST to GraphQL"

Essential Commands

Inside the OpenCode TUI, these are the most useful commands:

CommandDescription
/initInitialize the project and generate AGENTS.md
/undoRevert the last change made
/redoRestore a reverted change
/shareShare the current conversation
@fileReference a file with fuzzy search
💡 Tip: You can drag images directly into the OpenCode terminal to analyze screenshots, diagrams, or visual errors.

Session Management

OpenCode automatically saves your conversations. You can resume them at any time:

bash
1# List all sessions
2opencode session list
3
4# Continue the last session
5opencode session continue
6
7# Fork a session (create a conversation branch)
8opencode session fork

This is especially useful when working on complex features that require multiple iterations over days.

Conclusion

OpenCode democratizes access to AI coding assistants by being open source, multi-provider, and highly extensible. With its simple installation, elegant terminal interface, and support for over 75 model providers, it's a tool every developer should have in their arsenal.

In the following articles, we'll explore how to create custom Skills and Agents to adapt OpenCode to your specific needs, and how to configure MCP servers to integrate external tools like databases, APIs, and cloud services.

💡 Next step: Run curl -fsSL https://opencode.ai/install | bash and initialize your first project with /init. You'll see results in under 5 minutes!
Share:
CV

Cristhian Villegas

Software Engineer specializing in Java, Spring Boot, Angular & AWS. Building scalable distributed systems with clean architecture.

Comments

Sign in to leave a comment

No comments yet. Be the first!

Related Articles

Stay updated

Get notified when I publish new articles. No spam, unsubscribe anytime.