• Indonesian
  • English
  • How to Install OpenCode on VPS: AI Coding from Terminal

    Kecepatan:

    Introduction

    A few months ago, I got stuck. I was at a café, laptop died, and my charger was at home. But there was a production bug that needed to be fixed right now. My phone was the only device still alive.

    “If only there were an AI coding tool that could run from the terminal,” I thought. And turns out… there is. It’s called OpenCode.

    I still remember that feeling the first time I fixed a production bug from my phone — SSH into the VPS, open the file, and the AI agent gave me a solution directly in the terminal. No IDE needed, no expensive GPU, no sitting at a desk. All you need is an internet connection, an SSH client, and a small VPS.

    OpenCode isn’t as popular as Cursor or Windsurf yet. But that’s exactly its strength — lightweight, CLI-native, open-source, and you can install it on any VPS. No GUI, no desktop environment required. For those of us who live in the terminal (yeah, sysadmins and NOC engineers like me), this is a total game-changer.

    This article will walk you through installing OpenCode on a Linux VPS step by step — from dependencies to API key configuration, from performance optimization to security tips. I’ve tested this repeatedly on Ubuntu 24.04 and Debian 12. Ready? Let’s dive in.

    What Is OpenCode?

    OpenCode is a CLI AI coding agent you can talk to right in the terminal. Imagine having a senior developer sitting next to you 24/7 — just ask, and they answer. Just give instructions, and they get it done.

    But here’s the difference: it doesn’t just answer. It can read your entire project, understand the code structure, edit files directly, and run commands in the terminal. All from the CLI. Zero GUI.

    Supported AI models:

    • Claude (Anthropic) — Sonnet 4, Opus 4
    • GPT (OpenAI) — o4-mini, GPT-4.1
    • Local models — via Ollama (Llama, Mistral)

    Why OpenCode on a VPS?

    I get it — installing on your laptop is easier. But hear me out. There are three huge advantages to running OpenCode on a VPS:

    1. Run 24/7 — the agent keeps working even when your laptop is off. Just attach to a tmux session, and your code keeps getting processed.
    2. Dedicated resources — the VPS has its own RAM. Your laptop won’t lag from indexing large projects.
    3. Access from anywhere — SSH from your phone, tablet, or a friend’s laptop. As long as you have internet, you can code.

    Prerequisites

    Minimum VPS Specs

    I’ve already covered VPS specs in detail in a previous article. But here’s the recap:

    Component Minimum Recommended
    RAM 4GB 8GB
    CPU 2 vCPU 4 vCPU (AMD EPYC)
    Storage 20GB SSD 50GB NVMe
    OS Ubuntu 22.04 Ubuntu 24.04 LTS

    API Keys You’ll Need

    At minimum, you need one of these:

    Installing OpenCode on Your VPS

    1. Update System

    Always. Always update before installing anything.

    $ sudo apt update && sudo apt upgrade -y
    $ sudo apt install -y git curl wget tmux build-essential
    Reading package lists... Done
    Building dependency tree... Done
    Setting up tmux (3.4-8build1) ...
    Setting up git (1:2.46.0-2ubuntu1) ...
    Processing triggers for man-db (2.13.0-1) ...

    2. Install OpenCode

    According to OpenCode’s official documentation, the recommended installation method is via the official installer script. Just one command — the script will automatically detect your OS, install Go if it’s not already present, and install the OpenCode binary:

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

    Here’s what the output looks like during installation:

    ✓ Detected OS: linux/amd64
    ✓ Installing Go 1.24.0...
    ✓ Go installed successfully
    ✓ Building OpenCode from source...
    ✓ OpenCode installed to /home/user/.local/bin/opencode
    ✓ PATH updated in ~/.profile
    
    OpenCode installed successfully!
    
    Run 'opencode' to get started.

    After installation completes, reload your shell:

    $ source ~/.profile
    $ opencode version
    OpenCode v1.x — AI Coding Agent

    💡 Pro Tip: This installer script handles everything — from installing Go, compiling the binary, to updating your PATH. No need for a separate manual Go installation. If you need more granular control, visit opencode.ai for manual installation options.

    3. Configure API Key

    OpenCode reads API keys from environment variables. Pick one (or both):

    # For Claude (Anthropic) — highly recommended for coding
    $ echo 'export ANTHROPIC_API_KEY="sk-ant-api03-yOUR-kEY-hERE"' >> ~/.profile
    $ source ~/.profile
    
    # For GPT (OpenAI)
    $ echo 'export OPENAI_API_KEY="sk-proj-yOUR-kEY-hERE"' >> ~/.profile
    $ source ~/.profile

    ⚠️ Warning: Never store API keys in files that get committed to Git. Always add ~/.profile (or ~/.bashrc) to your global .gitignore. A leaked API key on GitHub = $1000 in charges within hours.

    For advanced configuration, you can create a ~/.opencode/config.json file:

    $ mkdir -p ~/.opencode
    $ cat > ~/.opencode/config.json << 'EOF'
    {
      "model": "claude-sonnet-4-20250514",
      "temperature": 0.7,
      "max_tokens": 8192,
      "project_root": "~/projects"
    }
    EOF

    Available fields:

    • model — AI model to use (default: Claude Sonnet 4)
    • temperature — response creativity (0 = precise, 1 = creative)
    • max_tokens — maximum response length
    • project_root — default directory for opening projects

    4. Test Run

    $ cd ~/projects/my-app
    $ opencode

    Here’s what you’ll see the first time you open OpenCode:

      ____  ____  _____ ____ ___   ___  _____ 
     / __ |  _ | ____/ ___/ _  / _ | ____|
    | |  | | |_) |  _|| |  | | | | | | |  _|  
    | |  | |  __/| |__| |__| |_| | |_| | |___ 
     ____/|_|   |____________/ ___/|_____|
    
    OpenCode v1.x - AI Coding Agent
    Model: claude-sonnet-4-20250514
    Project: /home/user/projects/my-app
    
    Type /help for commands. Type your question to start.
    
    > 

    Try asking something to test it:

    > explain this project's structure and what it does

    OpenCode will scan all files in your project, index them, and provide an explanation. The initial scan might be slow due to indexing — just wait, it’ll be fast after that.

    Advanced Configuration

    Available AI Models

    You can switch models in config.json based on your needs:

    {
      "model": "claude-opus-4-20250514",   // ○ Most expensive, smartest
      "temperature": 0.3,                   // ○ Coding = precision, skip the creativity
      "max_tokens": 16384,                  // ○ Longer responses
    }

    Model use cases:

    • Claude Sonnet 4 — fast coding, debugging, normal refactoring
    • Claude Opus 4 — complex architecture, deep analysis, code review
    • GPT-4.1 — fallback when Anthropic is down or rate-limited
    • o4-mini — light coding, cost-efficient, fast iterations

    Custom Prompts & Aliases

    Save frequently used prompts as shell aliases:

    $ cat >> ~/.bashrc << 'EOF'
    alias code-review="opencode --prompt 'Review this code and suggest improvements'"
    alias fix-bug="opencode --prompt 'Debug this error and provide a solution'"
    alias explain-code="opencode --prompt 'Explain what this code does'"
    EOF
    $ source ~/.bashrc

    Productivity Tips

    Use tmux for Persistent Sessions

    This is mandatory. Don’t SSH directly into OpenCode — if your connection drops, your session is gone.

    $ ssh user@vps-ip
    $ tmux new -s opencode
    $ cd ~/projects/my-app
    $ opencode

    If your connection drops: SSH back in, then:

    $ tmux attach -t opencode

    Your session is still there. The AI agent keeps running.

    API Cost Management

    AI API costs can balloon if you don’t monitor them. My personal budget runs around $25-40/month. Here’s what I’ve learned:

    • Use the right model — you don’t need Opus for fixing a typo. Sonnet handles 90% of tasks.
    • Set reasonable max_tokens — don’t use 16K for small fixes, 4K-8K is usually enough.
    • Batch your tasks — if you have 3 questions, ask them all in one prompt. Not 3 separate ones.
    • Check your usage — both the Anthropic console and OpenAI dashboard give real-time breakdowns.

    VPS Security

    Since your API keys are on the VPS, security is crucial:

    # Firewall: only keep SSH open
    $ sudo ufw default deny incoming
    $ sudo ufw default allow outgoing
    $ sudo ufw allow 22/tcp
    $ sudo ufw enable
    
    # Real-time monitoring — install Netdata
    # See the full guide: /en/noc-article/install-netdata-server-linux-monitoring-realtime/
    $ wget -O /tmp/netdata-install.sh https://get.netdata.cloud/kickstart.sh
    $ sh /tmp/netdata-install.sh
    
    # SSH key only (no password)
    $ ssh-keygen -t ed25519 -C "opencode-vps"
    $ ssh-copy-id user@vps-ip
    # Edit /etc/ssh/sshd_config: PasswordAuthentication no
    $ sudo systemctl restart sshd

    Troubleshooting

    OpenCode runs but responses are slow — Could be indexing a large project, or high latency to the API provider. Try picking a VPS closer to the US/Asia region. Or upgrade your RAM.

    “API quota exceeded” or “Rate limited” — You’ve hit your daily/monthly limit. Upgrade your plan in the Anthropic/OpenAI console, or wait for the reset.

    FAQ

    Q: Can OpenCode run on a 1GB RAM VPS?

    A: Technically yes, but indexing large projects will trigger OOM (Out of Memory). 4GB minimum, 8GB recommended — as I discussed in my VPS buying guide.

    Q: Is it safe to give an AI agent access to edit my code?

    A: Git is your best friend. Always commit before letting the AI agent edit your code. If the result isn’t what you wanted, just git checkout -- . and you’re back. Think of the AI agent as a junior developer — they’re good, but they need review.

    Q: Which is better for coding, Claude or GPT?

    A: From my experience: Claude is more precise for coding (less verbose, more to the point). GPT sometimes talks too much. But Claude gets rate-limited more often. So: use Claude as primary, GPT as fallback.

    Q: Can I access OpenCode from my phone?

    A: Absolutely! Since OpenCode runs on the VPS, all you need is an SSH client. Install Termux on Android (or Blink Shell on iOS), SSH into the VPS, run tmux attach, and you’re coding. I fix bugs on the go like this all the time.

    Q: Are there alternatives to OpenCode?

    A: Sure — Aider, Continue.dev, and Codex CLI by OpenAI. But in my opinion, OpenCode is the lightest and most CLI-native of the bunch. The others tend to need more resources or have more complicated setups.

    Related Issues

    Conclusion

    OpenCode on a VPS is a powerful combo for AI coding. You get a portable coding experience (code from anywhere), powerful performance (dedicated RAM and CPU), and cost savings (no need to buy the most expensive MacBook Pro to run an AI agent locally).

    The initial setup might take 30 minutes: run the installer, set your API key, configure tmux. After that? Just SSH into your VPS and start coding. AI coding from the terminal — no limits.

    If you run into issues or get stuck at any step, drop a comment below. I’ve been through the trial-and-error of installing this myself, so I know the pain points.

    Author: NOC Engineer — Network Operations Center