A publication for engineering teams on delegating real software work to cloud-based AI agents, at scale, across existing workflows.
Agent SetupLong read

Claude Code Setup Guide for Cloud VM Environments

Hardware, security, and tooling decisions for running Claude Code on cloud VMs.

· 9 min read
Cover illustration for “Claude Code Setup Guide for Cloud VM Environments”
Agent Setup · October 17, 2026 · 9 min read · 1,936 words

Running Claude Code on a cloud VM requires infrastructure planning as well as CLI installation. A reliable setup depends on deliberate choices about hardware, security, tooling, credentials, persistence, and isolation.

Why running Claude Code on a cloud VM is a different problem than running it locally

A local terminal inherits an environment that already contains tools and credentials, and losing the session when the terminal closes is often acceptable. A VM starts without those assumptions, so the engineer must supply the environment and its operating rules. In return, a disposable VM confines the blast radius of relaxed permission prompts and gives credentials and repository clones a home independent of a laptop.

There are two paths with different responsibilities. Anthropic-managed cloud sessions handle repository cloning, VM management, and persistence across devices for Pro, Max, Team, and qualifying Enterprise seats. With self-hosted VMs, the engineer does that work and has the control needed for local configuration, private network access, and custom tooling.

Choosing VM specs that match what Claude Code and the adjacent toolchain actually consume

The first sizing mistake is treating Claude Code as the only workload. Its footprint is modest, but the builds and development servers it invokes may not be. A machine with 2 vCPUs and 4 GB RAM is sufficient for the agent and a lightweight toolchain. For heavy npm or cargo builds, or a development server running beside the agent, 8 GB RAM is the comfortable floor. Cloud providers have much the same entry-level bundles, but these need enough capacity for both the build pipeline and the agent.

Ubuntu 24.04 LTS is the recommended base because the standard tooling has first-class packages there. ARM instances also work because Claude Code ships ARM64 builds, and they are typically cheaper than x86 equivalents. On smaller machines, swap lets a hungry build keep running at reduced speed. Run sudo fallocate -l 4G /swapfile, then sudo chmod 600 /swapfile, sudo mkswap /swapfile, and sudo swapon /swapfile. Persist it with echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab.

Hardening the VM before any agent touches it

Hardening belongs before toolchain installation and credential placement. From the first root account, run adduser dev and usermod -aG sudo dev to add a non-root user that can use sudo. Restrict SSH to key-based authentication by setting PasswordAuthentication no and PermitRootLogin no in /etc/ssh/sshd_config. Allow SSH through the firewall with ufw allow OpenSSH, then enable it with ufw enable. These are baseline controls for a machine that will hold real credentials and push real code, even if the machine itself is disposable.

Claude Code stores Linux credentials in ~/.claude/.credentials.json with owner-only permissions, mode 0600. Those permissions do not make the file harmless if the VM is compromised. A mesh VPN such as Tailscale can remove SSH from the public internet, while unattended-upgrades applies security patches automatically without reboots. The Ubuntu 24.04 bootstrap script also installs a reboot notification MOTD. Its root-account handling creates a non-root user, configures passwordless sudo, and re-executes as that user before proceeding.

Pre-loading the toolchain so the agent has everything it needs from the first task

Once the machine has a security baseline, the next task is to equip it deliberately. When an agent installs dependencies during a task, changing its environment takes time away from work on the repository, and those changes are harder to check. The Ubuntu 24.04 bootstrap inventory is a reference for selecting tools. Teams should trim it to their stack before the first run.

Its system foundation includes build-essential, git, curl, jq, and tmux, with ripgrep, fd, bat, fzf, eza, and direnv for terminal work. Infrastructure tools include Docker CE with Compose plugin and Buildx, kubectl, Helm, k9s, Terraform, and Ansible. PostgreSQL and MariaDB clients cover database access, while git-delta, lazygit, and glab support repository work. The script installs Claude Code through the native Bun installer rather than npm, which it identifies as deprecated, and adds Bash completion and a Starship prompt.

The script is idempotent, so it can both install and update the environment. Each run also writes ~/.claude/CLAUDE.md with a list of the tools that are installed. That inventory makes the environment legible to the agent without filesystem probing.

Authenticating Claude Code on a headless server without a local browser

Headless authentication is less confusing when the callback limitation is expected. Start claude on the VM and copy the login URL it prints, pressing c if the URL is not shown. Open the URL in the browser on the local machine. Because that browser cannot reach the callback server on the remote VM, GitHub shows a login code instead. Paste that code into the VM terminal to complete the flow.

The resulting credential survives reboots, so normal restarts do not require another login. Claude Code warns three days before expiry, and /login renews the credential. Do not copy credentials between machines because macOS uses the encrypted Keychain while Linux uses an owner-only file.

Scripts and CI have a separate path. Run claude setup-token to make a one-year OAuth token linked to a Pro, Max, Team, or Enterprise subscription. Manually copy the token and set it as CLAUDE_CODE_OAUTH_TOKEN.

Keeping sessions alive and resumable across disconnections

Successful authentication does not make the agent independent of SSH. Without a process manager, a dropped connection, closed laptop lid, or terminal timeout can kill the session mid-task. tmux separates the running process from the client connection, allowing another SSH client to reattach from any device. It is the standard persistence layer for a self-hosted interactive session.

Start with tmux new -s auth-refactor, then run claude inside that session. Detach with Ctrl-b + d when leaving the terminal. To return, connect with ssh dev-box, then run tmux attach -t auth-refactor on the VM. Names based on the task make sessions easier to recognize after time away than names such as session-4. The bootstrap script can also configure automatic attachment on SSH login so persistence becomes the default.

mosh can keep an interactive connection responsive on unreliable networks, but tmux already protects the agent from SSH disconnections. Neither mechanism moves the working environment to another machine. tmux preserves a process on its host.

Managing context and memory across sessions so the agent does not start from scratch

Project CLAUDE.md is human-authored, stable guidance checked into the repository. Auto memory is agent-authored, per repository, and shared across worktrees, holding learned build commands, debugging patterns, and recurring corrections. Auto memory stays on the machine and does not sync as project guidance does. A new VM therefore receives project CLAUDE.md through a git clone but starts without the old machine’s learned patterns.

Keep CLAUDE.md under 200 lines and use it for context that matters from one session to the next. A fresh session consumes roughly 8,000–14,000 tokens loading the system prompt, tool definitions, and CLAUDE.md before user input, so unnecessary detail competes with the task. Before a restart or machine change, ask Claude to write the current plan and remaining steps into a markdown file, then commit and push it.

MCP memory servers add another layer with a local process that makes memory-shaped tools available. Because they use the Model Context Protocol, the same server can work with Claude Code and other MCP-compatible agents. They complement project instructions rather than replace the need for portable, team-relevant guidance.

Configuring environments, network access, and setup scripts for production use

Anthropic’s cloud documentation defines environments as the layer controlling network access, environment variables, and an optional setup script. Pro and Max accounts receive one environment named “Default” during onboarding. Its “Trusted” network policy permits package registries and allowlisted domains, including GitHub, cloud SDKs, and container registries, but nothing else. Change it when an installation fails with a 403 or a task requires an API outside that list.

Self-hosted VMs require the equivalent decisions through outbound firewall rules, environment variables for credential injection, and the bootstrap script. Organizations with Zero Data Retention must run locally or on self-hosted infrastructure because they cannot use Anthropic-managed cloud sessions. On Team and Enterprise plans, “Quick setup” at Organization settings → Claude Code enables /web-setup and automatic environment creation during onboarding.

Managed setup scripts provide the pre-loading mechanism without maintaining a VM bootstrap. A bash script runs before Claude Code starts, gets roughly five minutes, and must exit with code 0. Its resulting filesystem is cached and reused across sessions for roughly seven days.

Connecting GitHub so the agent can clone, branch, and open pull requests

GitHub access completes the path from agent work to a reviewable pull request. If the GitHub CLI is configured, run /web-setup inside Claude Code to read the token printed by gh auth token and confirm the connection. It is an in-session slash command, so passing it as a shell argument with claude -p "/web-setup" fails. If workflows need to be edited, run gh auth refresh -s workflow before /web-setup so pushes that change GitHub Actions files can go through.

The browser path authorizes Claude on GitHub and can install the Claude GitHub App. Public repositories work immediately, while private repositories require the app on the owning account or organization with the repository included in its access. For an organization repository that remains unavailable, the missing step may be an administrator approving the app installation. If the organization uses SSO, each member must also authorize the app separately.

Team and Enterprise members face an additional gate. An Owner must enable the GitHub connector at Organization settings → Connectors for the prompt to display a sign-in button, and until then the prompt displays “GitHub access is required for Claude Code cloud sessions”. The connected token bounds repository access, while the app also enables Auto-fix responses to CI failures and review comments. GitLab tooling appears in the bootstrap through glab, separate from this GitHub integration.

Running parallel tasks and scaling to multiple agents without environment collisions

Anthropic-managed cloud sessions run independent tasks in separate sessions and branches, each with a fresh repository clone. Agents therefore work in separate directories with isolated uncommitted state, removing the need to manage multiple local worktrees for those tasks.

On self-hosted infrastructure, isolation must be explicit. Use git worktree add to give agents separate worktrees, or use separate VM instances per concurrent task. Worktrees are lighter but share host resources, while separate VMs provide full isolation. The choice therefore depends on whether the host has enough capacity for multiple builds to run on it.

Agent Teams extend the model to named agents collaborating with separate contexts and mid-task messaging. Default subagent concurrency is 20 and tunable, but isolated context is not a substitute for isolated working state. A VM-per-session model also makes usage, token consumption, and output attributable to individual tasks.

What a production-ready Claude Code VM environment actually looks like end to end

A production-ready environment is a chain of dependencies. Hardware supports the build, hardening protects the credentials, and the pre-loaded toolchain lets the authenticated agent begin useful work. tmux preserves the process through disconnections, while repository guidance preserves meaning across sessions. Network policy and GitHub permissions let the result reach review, and isolation keeps concurrent tasks from colliding. A gap in any layer remains a failure point under workload.

Managed sessions take responsibility for provisioning, setup-script caching, and session persistence. Self-hosted VMs keep the control needed for private network boundaries, custom tooling, or Zero Data Retention policies. Neither path makes configuration a finished event because credentials expire, project guidance drifts, and bootstrap scripts become stale.

Treat VM configuration as code. Keep the bootstrap script, CLAUDE.md, and environment variable definitions in version control, review them as infrastructure changes, and re-run the setup on a schedule.

Sources

  1. Setup a clean cloud Ubuntu vm for Claude to work in it · GitHub
Filed underAgent Setup

More in Agent Setup