How to Run OpenCode for VPS with tmux
Run OpenCode for VPS under unprivileged user, keep your API key private, and use tmux so coding sessions survive when your SSH connection drop.
Wetin OpenCode be, and wetin you dey set up
OpenCode na open source AI coding agent wey dem build for terminal. You start am inside project directory, and e go read your code, suggest changes, edit files, and run commands, all from terminal user interface (TUI). E get MIT license, e connect to more than 75 model providers, and e get about 224,000 GitHub stars as of 3 November 2026. To run OpenCode for VPS, install am under dedicated unprivileged user, put your model API key inside private file, and start am inside tmux so the session go survive if your connection drop. This guide dey do exactly that, for that order.
One naming matter fit prevent confusion. The canonical repository na anomalyco/opencode, and Anomaly team dey maintain am (dem bin know am before as SST). The project bin dey at sst/opencode before. Another unrelated repository wey dem name opencode-ai/opencode, and dem don archive am now, still dey GitHub. So check say na the correct project documentation you dey read. The official site na opencode.ai.
Wetin make you run OpenCode for VPS
Coding agent session fit long. OpenCode fit spend plenty minutes dey work through refactor or test suite. If e dey run for your laptop, closing lid or Wi-Fi connection wey drop fit kill the session before task finish. For VPS inside tmux, agent go continue work after you disconnect, and you fit attach again later to read wetin e do. Na the same pattern as running Claude Code for VPS with tmux, and na the biggest quality-of-life benefit of moving agent comot from your laptop.
The second reason na where e dey run. VPS dey close to the code wey you deploy: repository, build tools, test database, and often staging environment dey already there or near there. Agent wey dey edit code and run tests go work best for the machine where those tests really dey run. And because na server wey you control, you fit intentionally give agent contained environment, as the next section explain.
If you still dey choose tool, running coding AI agent for VPS compare the wider field, including Aider and Goose.
Give OpenCode im own user
Make we start with the plain truth: coding agent dey edit files and run commands. Na im work be that, and na the risk too. OpenCode go run builds, tests, and any shell commands wey the task seem to need, and the model judgement good but e no perfect. The account wey agent dey run as na the limit of wetin bad command fit reach, so no run am as root, and no run am as the same user wey dey administer the server.
This guide dey run OpenCode interactively, so the user need real shell and home directory:
sudo useradd --create-home --shell /bin/bash opencode
sudo -iu opencodeKeep the projects wey you want am to work on under /home/opencode, and make that user clone dem. No give the account sudo rights. If the agent run destructive command, e fit damage anything wey this account get permission to change. Na the same reason why you dey run services as unprivileged user. Work inside git repository too, and commit before you give the agent task, because if bad edit affect committed work, you fit return am with git revert or git restore instead of losing am. Changes wey you never commit no get protection.
Install OpenCode
The project docs list several installation methods, and two of dem fit server. The install script na the quickest one, and if you run am as the opencode user, everything go remain inside that user's home:
curl -fsSL https://opencode.ai/install | bashThe normal curl | bash habit still apply here: for server wey matter to you, download the script first, read am, then run am. After installation, start a new shell so the PATH change wey installer make go take effect, then check whether the binary dey answer:
opencode --versionIf you prefer package manager and Node.js already dey installed for the box, the npm method installs the same tool inside npm's global prefix:
sudo npm install -g opencode-aiWith system-wide Node.js, that prefix normally dey for /usr/local or /usr, and its bin directory dey on every user's PATH. Version manager or custom prefix fit put the binary somewhere else, so run npm prefix -g first if you need opencode to dey found by the opencode user.
Either way, the check na the same: opencode --version prints version number. A command not found after the script install means the current shell has not read the updated PATH yet, so log out and back in as the opencode user.
Both commands install the OpenCode 1.x line, wey still releases versions (1.19.5 on 28 October 2026), and na that line this guide dey describe. OpenCode 2 don come out now as separate install, from https://opencode.ai/v2/install or the npm package @opencode/cli, and e dey work differently: by default, the terminal dey work through shared background server, wey opencode service dey manage and wey dey run your sessions. The migration guide say make you remove package-managed 1.x install before you install 2, because both dey use the opencode command.
Put API key for private file
OpenCode need key for any model provider wey you dey use. That key fit spend your money, so treat am like password. Create file wey only the opencode user fit read, with mode 600. Keep the key there instead of typing am inside commands, where e go enter your shell history:
install -m 600 /dev/null ~/opencode.env
nano ~/opencode.envPut your provider variable inside am, for example ANTHROPIC_API_KEY=... or the equivalent for your provider. OpenCode dey pick the standard provider environment variables. Load the file into your shell before you start the agent:
set -a; source ~/opencode.env; set +aOpenCode get interactive alternative too: the /connect command inside the TUI go guide you to add provider and save the credential to ~/.local/share/opencode/auth.json for the user's home directory. If you use this method, confirm say the file private with chmod 600 ~/.local/share/opencode/auth.json. Both methods keep the key out of your command lines. Choose one and use am consistently.
Start OpenCode inside tmux
tmux na wetin make VPS setup worth am, because tmux session dey continue to run when your SSH connection end. Start one, enter your project, then launch the agent:
tmux new -s opencode
cd ~/my-project
opencodeYou suppose see the TUI open, with prompt for bottom and your project name for the interface. Give am task with plain language, and e go start read files and propose changes. When you wan comot, detach with Ctrl-b then d, and the agent go continue work even when you close your laptop. Reattach later with:
tmux attach -t opencodeThe session, the conversation, and any task wey dey run go remain exactly where you leave dem. This one survive disconnection, but e no survive server reboot, so after reboot you go start fresh tmux session the same way. Nothing stop you from opening second tmux window and running another agent beside the first one. But OpenCode sessions remain independent from each other, while Claude Code sessions for the same box fit send messages to each other, and this na another way to split one job into two.
Point am to model
OpenCode no tie to one provider. E dey use AI SDK and Models.dev catalog to support more than 75 providers, so the same tool fit work with Anthropic, OpenAI, Google, and dozens of others, including local servers. The quick method na the /connect command inside TUI, wey dey list providers and handle the credential. For setup wey you fit commit and reproduce, put an opencode.json for project root and set the model as provider/model-id:
{
"$schema": "https://opencode.ai/config.json",
"model": "anthropic/claude-sonnet-5-5"
}Choose model wey your provider still dey serve: Anthropic retire claude-sonnet-4-20250514 on 15 June 2026, so configs wey still name am go fail.
Local model dey work through the same file, because any OpenAI-compatible server fit be declared as provider. If you serve model with Ollama for the same VPS, the config go point to its local API, and the model name na whatever ollama list dey show for your box:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"ollama": {
"npm": "@ai-sdk/openai-compatible",
"name": "Ollama (local)",
"options": { "baseURL": "http://127.0.0.1:11434/v1" },
"models": { "your-model-name": { "name": "Local coding model" } }
}
}
}One built-in habit good make you adopt from day one. OpenCode ships two agents wey you fit switch between with Tab key: Build, the default agent with full access, and Plan, restricted agent for analysis wey refuses file edits outside its own plan directory without asking. Plan no be strictly read-only, though, because e still fit run shell commands, and shell command fit change files. Start new task for Plan, allow am read the code and propose approach, and switch to Build only when you agree with the plan. If you want that first pass to truly remain read-only, denying edits and shell commands for Plan no dey enough by itself. Plan fit hand work to the built-in Explore subagent, wey keeps its own shell access, because parent agent restrictions no dey carry over to subagents wey e starts. So deny edits, shell commands, and subagent delegation (the task permission) for Plan inside opencode.json, because OpenCode treats deny as blocking the action, while ask only prompts you for approval:
{
"$schema": "https://opencode.ai/config.json",
"agent": {
"plan": {
"permission": { "edit": "deny", "bash": "deny", "task": "deny" }
}
}
}For server, first pass wey cannot change anything na cheap insurance. Claude Code describes the same choice as permission modes instead of agents, and the shift to auto mode as the default worth reading if you run both tools, because the mode wey session starts with decides how much an unwatched agent fit change.
The blast radius, honestly
Coding agent no dey passive, so talk plainly wetin this setup dey contain and wetin e no dey contain. E limit file damage to only wetin the opencode account get permission to change; the project directory itself no be sandbox. Mode 600 stop other unprivileged accounts from reading the key file, but OpenCode dey run as the file owner and fit read am. E no limit the things wey the account legitimately fit do, so if project directory get production deploy credentials, the agent fit use dem; keep those credentials completely outside the agent's account.
OpenCode 1.x support both the interactive TUI wey we use here and standalone, non-interactive server operation with opencode serve. The headless HTTP server default na 127.0.0.1:4096; set OPENCODE_SERVER_PASSWORD to require HTTP basic authentication. This tmux setup no need systemd unit, but if you enable server mode, review the listening address and firewall rules before you expose am. OpenCode 2 dey use shared background server by default, so check this point again before you migrate. The containment na the user account's operating system permissions, and na why the first section of this guide matter pass.
The box around am still need standard care, because coding VPS still be public server: key-only SSH with root login disabled, as e dey for SSH hardening for VPS, default-deny firewall, and routine updates. Also review wetin the agent produce. Read the diffs before you push dem, just as you go read pull request from new contributor, because na you deploy the result.
Finally, keep the tool itself current. OpenCode dey release often, and updates carry fixes wey matter for program wey dey run commands for your server. To update am, use the same method wey you use install am: rerun the install script as the opencode user, or run sudo npm update -g opencode-ai if you install through npm, then confirm the new version with opencode --version. Both keep you for the 1.x line; moving to OpenCode 2 na separate migration. One minute maintenance from time to time cheaper than debugging behaviour wey months-old build don already fix.
FAQ
OpenCode fit use local model instead of paid API?
Yes. OpenCode dey treat any OpenAI-compatible server as provider, so model wey Ollama dey serve for the same VPS go work: declare the provider inside opencode.json with the local baseURL and the model name wey Ollama report. The main issue na hardware, because model wey good enough for real coding work need serious memory. So size the server for the model before you download am.
How I fit make OpenCode continue to run after I close my laptop?
Run am inside tmux for the VPS. Start the agent inside named session with tmux new -s opencode, detach with Ctrl-b then d, and the session go continue to run for the server after your SSH connection end. Reattach any time with tmux attach -t opencode, and the conversation plus any task wey dey run still dey there. Server reboot go end the session, so start new one after reboot.
E safe to allow OpenCode run commands for my VPS?
You fit manage am if you contain am. Give OpenCode dedicated unprivileged user wey no get sudo. Keep its projects for git and commit before each task, so you fit roll back bad edit. Store the API key inside file with mode 600. Use its Plan agent for first pass before you allow the Build agent change anything. If you want make that pass strictly read-only, set edits, shell commands, and subagent delegation (task) to deny for Plan, because subagents like Explore get their own permissions and fit still run shell commands. The agent still fit access anything wey the operating system permissions of its account allow. OpenCode permission system no be security sandbox.
Wetin be the difference between OpenCode and Claude Code?
OpenCode na open source (MIT) and e no tie to one provider. E connect to more than 75 model providers, including local ones, through one interface. Claude Code na Anthropic own terminal agent, built around Anthropic models. If you want one tool wey fit work across many providers, or fully self-hosted stack with local model, OpenCode na the better fit. Both dey run well for VPS inside tmux with the same unprivileged-user setup.