Cohub
中文Start a Space

How to run Claude Code in the cloud inside a Cohub Space

To run Claude Code in the cloud on Cohub, install it in a Space's sandbox with a startup script. The walkthrough checks CLI authentication with placeholder keys, then shows how to publish a server and recover project files with Saves.

Who
Developers who use Claude Code or Codex CLI and want it running on a cloud machine next to a project that other people can open.
When
The laptop can't stay on, the result should be a live URL rather than localhost, or a teammate needs to see the files and the running app.
Why
A Cohub Space gives the CLI a Linux sandbox with persistent files, two public ports that publish as Apps, and Saves to return to when a run goes wrong.

Running Claude Code in a Cohub Space cloud sandbox puts the CLI directly beside your /workspace files on a hosted Linux machine, so your laptop does not need to stay awake and teammates can open a live URL instead of localhost. Every command and behavior below was verified on cohub.live on 2026-11-17 with Claude Code 2.1.326, Codex CLI 0.169.0, and cohub CLI 10.29.1; no live API credentials or authenticated model calls were executed.

Cloud sandbox CLI vs Local Runtime harness

In Cohub, Claude Code and Codex are built-in local harnesses: running cohub runtime up --harness claude or --harness codex attaches a Space to the CLI already installed on your own workstation. Meanwhile, a cloud Space's built-in chat agent executes on the Cohub harness.

This walkthrough uses a third path: installing the CLIs inside the Space's cloud Linux sandbox and invoking them as regular shell programs against /workspace. To weigh local execution against a hosted workspace, see Cohub vs local Claude Code and Codex. For installing and signing in to the cohub tool used throughout these steps, see ai agent cli.

Create the Space and persist CLIs across sandbox resets

Start by creating a Space and assigning a short slug, which becomes part of your published App URL later:

cohub spaces create --name "CLI agents in the cloud"
cohub spaces update <space-id> --slug cli-agents-cloud

Commands executed from your laptop pass -s cli-agents-cloud; inside the sandbox shell, cohub detects its current Space automatically.

One storage detail matters immediately: npm install -g writes binaries into /home/node/.npm-global, outside /workspace. Because only /workspace persists across idle stops and sandbox replacements, global packages vanish whenever a fresh container boots. Place a .cohub/setup.sh startup script inside /workspace so every new sandbox reinstalls missing CLIs on boot:

#!/usr/bin/env bash
set -euo pipefail
command -v claude >/dev/null || npm install -g --allow-scripts=@anthropic-ai/claude-code @anthropic-ai/claude-code
command -v codex >/dev/null || npm install -g @openai/codex

The sandbox ships with npm 11.20, which blocks package install scripts by default, so Claude Code requires --allow-scripts. When we launched a new Space from a Save holding this script, .cohub/setup.sh finished in 6.1 s and both claude --version and codex --version responded cleanly. Check startup script status and log locations anytime with cohub spaces setup <space-id>.

Terminal: .cohub/setup.sh installs @anthropic-ai/claude-code and @openai/codex, cohub spaces setup reports both script runs as succeeded, and claude --version and codex --version print 2.1.326 and codex-cli 0.169.0

Verify authentication wiring and headless commands

Right after installation, claude -p reported Not logged in and codex login status confirmed no active session. For the 2026-11-17 test, we added placeholder values only for ANTHROPIC_API_KEY and CODEX_API_KEY under Space settings → Environment → Variables. Both CLIs reached their upstream APIs and returned invalid-key errors, confirming network reachability and variable loading. Keep in mind that every process in the sandbox can read Space variables—never place personal keys in a shared Space or commit them into workspace files.

Once authorized on a trusted runner, a non-interactive file edit looks like this:

cd /workspace && git init -q
claude -p "Add a /health route to server.js" --permission-mode acceptEdits

By default, claude -p refuses actions that require interactive confirmation unless you pass a permission mode (such as acceptEdits for file writes) or --allowedTools. You can also kick off the exact same command from your local terminal:

cohub -s cli-agents-cloud run --command 'claude -p "..." --permission-mode acceptEdits'

Pass --command whenever the invocation contains quotes or semicolons, and add --async if the task runs long.

Publish a sandbox port as a live App

Each sandbox exposes ports 3000 and 5173 for live preview. Start your server detached from the command session so it survives after the shell exits, then bind it to an App slug:

setsid nohup node server.js > server.log 2>&1 < /dev/null &
cohub -s cli-agents-cloud apps publish release-notes --port 3000

The published App resolves at cohub.live/<username>/cli-agents-cloud/w/release-notes. Because preview URLs on ports 3000 and 5173 answer requests without authentication, treat any service bound to those ports as public. For static directories, single-file Apps, and version rollbacks, read Publishing from a sandbox.

Terminal: curl localhost:3000 returns the Release notes page, then cohub -s <space> apps publish release-notes --port 3000 prints App published and a row with slug release-notes, target port 3000 and a public URL
A published directory App as visitors see it, in dark theme: the Northwind Atlas map page, with 12 coastal stations on a map, station counts and October highlights

Snapshot with Saves and recover edited files

Take an immutable Save before letting an automated run touch working code, then inspect file diffs afterward:

cohub -s cli-agents-cloud spaces checkpoints create "Working server + setup script"
# after the run
cohub -s cli-agents-cloud spaces files diff
cohub -s cli-agents-cloud spaces files diff server.js

In cohub CLI 10.29.1, Saves cannot be restored in place into the same Space. To recover earlier versions of specific files, fork a temporary Space from the Save and copy the files back:

cohub spaces create --name "Release notes (restored)" --checkpoint <save-id>
cohub spaces files cp <restored-id>:server.js <restored-id>:package.json cli-agents-cloud:

The forked Space automatically executed .cohub/setup.sh on boot, leaving both CLIs ready there as well. During the recorded run, the pending diff in cli-agents-cloud took a few minutes to clear after copying files back. Note that Saves skip .git, node_modules, dist, build, .cache, and *.log, so handle version history and build artifacts separately.

Terminal: a Save is created, a later change shows in the pending diff, a new Space is started from the Save and the two files are copied back, after which the diff is empty
cohub CLI 10.29.1 against cohub.live, 2026-11-17

Operational boundaries to plan around

  • Idle shutdown stops live servers. Spaces created via CLI default to autoDestroy: idle with a 3600-second TTL. Files under /workspace remain intact, while running processes—including servers behind published ports—stop. Run cohub spaces config <space-id> --auto-destroy never when a server must stay up.
  • Startup scripts cannot supervise daemons. .cohub/setup.sh must exit cleanly; use it to restore packages, not to keep background servers alive.
  • Shared secret visibility. Any process or collaborator able to execute commands inside the Space can read and print Space environment variables.
  • Separate billing streams. Sandbox runtime charges draw from your Cohub balance, while model requests from Claude Code or Codex bill to Anthropic or OpenAI under your API key.
  • Codex inner sandbox failure. On this host (max_user_namespaces=0), Codex CLI 0.169.0 could not launch bwrap namespaces. When isolated shell execution under Codex is required, see choosing a host for Codex.
  • No in-place Save restore yet. cohub CLI 10.29.1 supports creating, viewing, and diffing Saves, but restoring files back into an active Space requires the fork-and-copy flow shown above.

Reference in the docs

This page: How to prepare a cloud project for Claude Code and keep the server and files usable across sandbox replacements.

  • Files & Sandbox: what the sandbox is, ports and live preview, and what survives a restart
  • Saves: Saves (checkpoints): creating one, diffs, and building on a known-good state
  • Apps: publishing a port as an App, its URL and versions
  • CLI: the cohub commands used here: spaces, run, checkpoints, files, apps

FAQ

Can I use a Claude subscription instead of an API key?

Claude Code documents subscription authentication for headless use. Its token is a personal credential; don’t put it in shared Space variables or project files. The cloud test used only a placeholder API key.

Is this the same as Claude Code's own cloud sessions?

No. Claude Code cloud sessions run in Anthropic-managed VMs and start from claude.ai/code or claude --cloud. Here the CLI runs inside your Cohub Space's sandbox, next to its files, Saves, Apps and members.

Can the web composer pick Claude Code for a cloud Space?

The Claude Code and Codex harnesses run through a connected local Runtime (cohub runtime up). In a cloud sandbox you run the CLIs as programs, as shown here.

Sources

Published · Updated