How to run Claude Code in a VM, from Firecracker by hand to a hosted microVM

Last updated

Running Claude Code in a VM puts the agent and everything it installs behind a kernel of its own: a bad command wrecks the VM, not your laptop. Below: why a VM rather than a container, four DIY routes, hosted microVMs, what happens to your credentials, and what a session costs by the second.

Why run Claude Code in a VM instead of a container?

A container shares the host's kernel; a VM brings its own. Anthropic's sandbox guide calls a dedicated VM "the strongest separation", names Firecracker, and recommends a VM for untrusted repositories. In practice:

  • --dangerously-skip-permissions stops being a bet on your laptop. Anthropic says to "always run --dangerously-skip-permissions sessions inside a container, a VM, or the sandbox runtime". In a VM, a stray rm -rf hits a disk you throw away. What the flag removes: running --dangerously-skip-permissions safely.
  • The agent gets a real machine. Root, systemd, its own ports, and its own Docker daemon, without mounting the host's Docker socket into a container.
  • Agents don't collide. Two VMs share no ports, databases or node_modules, which matters once you run Claude Code agents in parallel.

What a VM does not fix, in Anthropic's words: "Any approach that allows network egress can still leak data the agent can read."

What does Claude Code need inside the VM?

From the setup docs: Ubuntu 20.04+, Debian 10+ or Alpine 3.19+, 4 GB of RAM or more, x64 or ARM64, and an Internet connection. Install with the native installer (curl -fsSL https://claude.ai/install.sh | bash), a signed apt/dnf/apk repository, or npm (Node.js 22+), which installs the same native binary.

Two things trip people up:

  1. Root. "On Linux and macOS, Claude Code refuses to start with this flag when running as root, so run the container, VM, or sandbox runtime as a non-root user." Many VM images log you in as root: create a user first (useradd -m -s /bin/bash agent).
  2. Size. Under 4 GB of guest RAM is below Anthropic's minimum, before the dev server, tests and containers the agent starts.

For scale, the client alone is not heavy. Measured on 2026-10-31 on an Apple M1 Pro, Claude Code 2.1.314, one trivial prompt:

$ /usr/bin/time -l claude -p "Reply with the single word: ok"
ok
        5.41 real         0.86 user         0.25 sys
           269484032  maximum resident set size

About 257 MiB peak and 1.1 s of CPU over 5.4 s: the client mostly waits on the model. One run on macOS, not a Linux benchmark; your builds and tests will dominate.

How do you run Claude Code in a VM yourself?

Four routes, from most to least work.

Firecracker by hand (Linux host with KVM)

Firecracker needs Linux on x86_64 or aarch64 and read/write access to /dev/kvm; on AWS that means a .metal instance, since EC2 only supports KVM there (the getting-started guide suggests a c5.metal). The steps: download a kernel and rootfs from Firecracker's CI bucket, build an ext4 image with your SSH key, create a tap device with NAT, start firecracker --api-sock, configure boot source, drive and network through the socket, InstanceStart, SSH in. Images, networking and lifecycle are yours to script.

Two lessons from building flockvm's first host, in Paris (retired on 2026-11-10), on Firecracker v1.17.0 (from its host/README.md): Firecracker's prebuilt CI kernel boots Ubuntu 24.04, but it has no nf_tables, so Docker's iptables-nft calls fail; a kernel rebuilt with nf_tables, xt_mark and the raw/mangle tables runs docker run hello-world with container networking. And on that host, a 2 vCPU / 4 GiB guest goes from InstanceStart to the sshd banner in 0.96 to 1.04 s on a first boot, in the host's self-tests.

To pause a session with its memory, see Firecracker snapshot and restore.

claude-vm (Linux, QEMU)

claude-vm is an MIT-licensed Bash wrapper around QEMU with KVM. It mounts the project at /workspace over virtiofs and, per its README, the VM "can't touch your host filesystem outside the project directory". It copies your Git identity, GitHub/GitLab CLI auth and Claude Code settings into the VM on creation, and documents no egress filtering. Its install steps cover Linux hosts only.

Lima (macOS)

Lima runs Linux VMs on a Mac, with Apple's Virtualization framework (vz) by default on macOS 13.5+. Its default template mounts your home directory read-only. Read-only stops deletions, not reading: ~/.ssh and ~/.aws are visible to the agent. For an agent VM, replace the home mount with the project directory alone and set writable: true on it.

A VPS

Any Linux VPS is a VM: persistent, monthly price, and snapshots, cleanup and agent separation are up to you. See Claude Code on a VPS vs a microVM.

Which hosted services run Claude Code in a microVM or sandbox?

Service What runs Claude Code Claude Code preinstalled Claude login documented Usage price (checked 2026-10-31)
Fly.io Sprites Firecracker microVM, persistent ext4 disk, sleeps when idle Yes Sign in inside the Sprite $0.0385 per CPU-hour of use, $0.021875 per GB-hour of memory used
E2B Sandbox from the claude template Yes ANTHROPIC_API_KEY passed as an env var $0.000014 per vCPU-s, $0.0000045 per GiB-s
Daytona Linux container by default (VM sandboxes exist), reached with daytona ssh Yes, in the default snapshot Browser code on first run $0.0504 per vCPU-h, $0.0162 per GiB-h, per second
boxd Machine with "its own kernel" Yes One login per organization, served to machines Not on that page
flockvm Firecracker microVM, France Yes, on the default Agent image (not signed in) /login inside the VM, or the organization's API key from an environment (below) Billed per second

Fly also has a plugin mode where Claude Code stays local and only the code it runs moves into a Sprite; flockvm vs Fly.io Sprites compares the two for long agent sessions. E2B sandboxes have Internet access by default, with allow/deny rules; flockvm vs E2B covers its fork, limits and prices. exe.dev also gives each agent a whole VM over SSH, priced as a flat monthly pool; flockvm vs exe.dev compares the two. More in E2B alternatives and Docker Sandboxes alternatives.

What happens to your Claude credentials inside the VM?

Claude Code reads credentials in a fixed order (authentication docs). Two routes fit a VM, and both start inside it:

  • Log in inside the VM. Start claude there and run /login, with a Claude plan (Pro, Max, Ultra, Team, Enterprise) or a Claude Console account. The VM has no browser: "press c to copy the login URL to your clipboard, then paste it into your browser" on your laptop, and if the browser "shows a login code instead of redirecting back", paste it at the Paste code here if prompted prompt. On Linux the login lands in ~/.claude/.credentials.json with mode 0600.
  • An organization API key, ANTHROPIC_API_KEY, from the Claude Console, billed per token. In an interactive session Claude Code asks once whether to use it; in -p runs "the key is always used when present". You revoke it in the Console.

Don't copy a token from your laptop into the VM: sign in from the VM itself, or give it a key the organization can revoke.

Either way the credential sits inside the VM, readable by an agent that skips permissions. Treat it as exposed: prefer a key you can revoke, cap its spend, revoke it if a run goes wrong. Avoiding this takes a proxy outside the VM, as Anthropic's cloud sessions do for the GitHub token: "a separate proxy holds your GitHub token outside the sandbox".

flockvm v1 does not have that proxy yet. The login or key you give Claude Code stays in the VM until you run /logout or delete the VM, and you revoke the key if needed. The VM protects your laptop; it does not stop the agent from reading what you gave it.

What does a Claude Code session cost per second?

The VM is billed for the time it runs, whatever the agent is doing. For a 2 vCPU / 4 GiB VM, at the prices in the table:

Service Formula Per hour Per second 30-minute session
E2B 2 × $0.000014 + 4 × $0.0000045 per s $0.1656 $0.000046 $0.0828
Daytona 2 × $0.0504 + 4 × $0.0162 per h $0.1656 $0.000046 $0.0828
Sprites, fully used 2 × $0.0385 + 4 × $0.021875 per h $0.1645 $0.0000457 $0.0823
Sprites, 0.2 CPU and 1 GB used on average (example) 0.2 × $0.0385 + 1 × $0.021875 per h $0.0296 $0.0000082 $0.0148

Daytona adds storage at $0.000108 per GiB-h above 5 free GiB; Sprites adds hot storage at $0.000683 per GB-hour while awake. Sprites bills usage rather than size, so an agent waiting on the model costs less there; the last row is an illustration, not a measurement. E2B's free Hobby plan caps sessions at one hour; Pro ($150/month plus usage) allows 24. Model tokens are a separate bill, paid to Anthropic.

How does it work on flockvm?

Sign-ups for flockvm are open (Google, GitHub or an email link), with a 7-day free trial: card required, nothing charged for 7 days, cancel anytime. Each agent gets its own Firecracker microVM, Ubuntu 26.04 with Claude Code, Codex and other agent CLIs, Node.js 26, Git, Docker and Playwright on the default Agent image, on one host in France (OVHcloud); other regions are planned. The VM boots to SSH in about one second, you SSH in as user (passwordless sudo; root works too) with your key, and it is billed per second. Pausing writes the VM's memory to disk, and resuming brings back the same processes, with the same PIDs, and open SSH connections survive. On the host in France, at most fifteen small VMs (2 vCPU, 4 GiB) run at once, across all customers.

To run Claude Code there today: SSH in as user (not root, so the flag is accepted; Claude Code is already installed), sign Claude Code in with /login inside the session or your organization's ANTHROPIC_API_KEY, clone your repository, and start claude --dangerously-skip-permissions under tmux so the session survives a dropped connection.

What it does not do yet: no /dev/kvm inside the VM (Docker works, nested VMs don't; Firecracker doesn't document nested virtualization), no fork of a running VM, no egress allowlist, no secrets proxy. Swarms of 10 to 20 agents and forking are what flockvm is building next: see coding agent swarms.

When is something other than a microVM the better choice?

  • Fewer prompts on your own machine, on code you trust: the sandboxed Bash tool, configured with /sandbox.
  • A team standard in the repo: Anthropic's dev container.
  • You need nested virtualization (Android emulators, Firecracker inside the agent's machine): a VM type that exposes /dev/kvm, such as a bare-metal server or a VM whose hypervisor passes the CPU's virtualization extensions through.
  • You already pay for a subscription and want zero setup: Claude Code's cloud sessions run in an Anthropic-managed VM (Claude Code in the cloud compares them with a VM of your own).

flockvm gives each Claude Code session its own throwaway Firecracker VM in France, billed per second. Start your 7-day free trial.

Run your agents in their own microVMs

flockvm gives each coding agent a Firecracker microVM that pauses with its memory and resumes, billed per second. France is live, and you can start with a 7-day free trial.

Start your 7-day free trial

Card required. Nothing charged for 7 days.