Coding Agents in a VM
AI coding agents such as Claude Code, OpenAI Codex CLI, Aider or OpenCode work fastest when they don't stop to ask before every command. On your Mac, that means an agent could read your home folder and SSH keys, change files outside the project, or run anything it downloads. Running the agent in its own virtual machine keeps all of that inside the VM: the agent gets a machine it is allowed to break, and you decide what comes back to the Mac.
This page shows how to set up such a VM with features VirtualProg already has: Linux cloud images, User Data, checkpoints, linked clones and vpvm.
📸 Screenshot:
Do you need a VM?
Not always. Pick the lightest option that matches how much you trust the task:
| Option | Isolation | Good for |
|---|---|---|
| The agent's own permission prompts | The agent asks before risky steps | Everyday work you watch |
The agent's built-in sandbox (such as Claude Code's /sandbox) |
macOS limits files and network for the agent's commands | Trusted projects, Mac-only tools |
Containers (Docker, dev containers, Apple's container) |
A Linux container sharing a VM's kernel | Linux projects with a container setup |
| A VirtualProg VM | A whole machine, with its own kernel, disk and network address | Unattended runs, unfamiliar code or dependencies, macOS builds, whole-machine rollback |
A VM is the right choice when the agent runs without prompts for a long time, when the project or its dependencies come from someone you don't know, when you need to roll back the whole machine (installed packages and services included), or when the build needs macOS.
What the VM protects, and what it doesn't
Be clear about the boundary before you rely on it.
Kept away from the agent:
- Your Mac's files, home folder, Keychain, SSH keys and browser sessions, as long as you don't share folders or the clipboard with the VM.
- Your git history and repository settings on the Mac, if you copy the project in as described below. Git hooks the agent writes in the VM never run on the Mac.
- The VM's own state: a checkpoint rolls back the disk, packages and services in seconds.
Not kept away from the agent:
- Anything you put in the VM. The API key or login you give the agent is readable by everything that runs there. Use a separate, limited key (see Sign the agent in).
- The internet. The agent needs it to reach its model, so the VM uses NAT networking. VirtualProg doesn't filter which sites a VM can reach, so code in the VM can send data anywhere. Don't put secrets in the VM that you wouldn't want to leave it.
- Your local network and your Mac's network services. A VM on NAT can reach other devices on your network and services the Mac offers on its network interfaces (file sharing, a development server listening on all addresses, and so on). See Lock it down.
- What you bring back. Changed code runs on your Mac the moment you build or test it. Review it first (see Review and bring the changes back).
⚠️ A VM limits what a misbehaving agent can do; it doesn't make its output trustworthy. Treat what comes back like a pull request from someone you don't know.
Requirements
- A Mac with Apple silicon (cloud images need it).
- macOS 27 or later for checkpoints and linked clones. On earlier macOS, use snapshots and templates instead; they work the same way but take longer and use more disk space.
- The project in a git repository on the Mac.
- An account with the agent's provider, or a local model server reachable from the VM.
Quick start: let Claude Code set it up
If you use Claude Code on your Mac, the VirtualProgCodingSandbox skill does steps 1 to 3
for you. It asks for a name, user, sign-in method and which agents to install (Claude Code,
OpenAI Codex CLI, GitHub Copilot CLI), creates an isolated Ubuntu VM from a cloud image,
pushes your project into it if you want, and gives you the ssh command.
-
Connect Claude Code to the MCP server. To sign in to the VM with a password instead of an SSH key, add
--allow-secrets: -
Install the skill for all your projects:
-
Start Claude Code (in your project's folder, if the skill should push it) and type
/VirtualProgCodingSandbox, optionally followed by the VM's name.
The skill stops when the VM is ready. Sign the agent in, run it and review its work as in steps 4 to 6.
1. Create the agent VM
Create a Linux VM from a cloud image, with User Data that installs the tools the agent needs. Ubuntu Server is a good default; use the distribution your project targets if it has one.
- Open Quick Create, choose Linux and set Source to Cloud Image.
- Pick Ubuntu Server and give the VM a name such as
agent-base. - Enter a User such as
devand choose your public SSH Key. A password isn't needed. - Give it at least 4 CPUs, 8 GB of memory and a 40 GB disk; builds and package installs inside the VM need room.
- Click User Data and paste a setup such as the one below, then click Create.
#cloud-config
package_update: true
packages:
- git
- curl
- build-essential
- tmux
- ripgrep
- nodejs
- npm
- python3-pip
- python3-venv
runcmd:
# Install the agents you use. Check each one's documentation for the current command.
- npm install -g @anthropic-ai/claude-code
- npm install -g @openai/codex
Add your project's own toolchain (a language runtime, database, compiler) in the same file, so every VM made from this one is ready to build. Keep API keys and tokens out of User Data: it is stored in the VM's settings and on its setup disk.
The VM list shows the setup's progress. When it reaches Ready, sign in:
If the setup fails, click Setup failed in the VM list for the Setup Logs.
From the command line, the same VM is:
vpvm create agent-base --image ubuntu-cloud --user dev \
--ssh-key ~/.ssh/id_ed25519.pub --cpu 4 --memory 8192 --disk 40 \
--user-data agent-setup.yaml --headless
2. Lock it down
Open the VM's configuration and turn off everything that connects it to your Mac:
| Setting | Where | Set to |
|---|---|---|
| Shared folders | Shared Folder | None. A shared folder gives the agent direct write access to Mac files. |
| Share Clipboard | Security | Off. Otherwise the agent can read what you copy on the Mac. |
| Copy/Paste Files | Security | Off. |
| USB devices | USB | None. |
| Network | Network | Default (NAT). The agent needs the internet to reach its model. |
On the Mac, check which services accept connections from the network, since the VM can reach them:
Services bound to 127.0.0.1 or localhost can't be reached from the VM; services bound to
* or 0.0.0.0 can. Stop or rebind the ones you don't want a VM to reach, and turn off sharing
services you don't use in System Settings → General → Sharing.
💡 Agents that use a local model don't need the internet. Put such a VM on a Host-only network and point the agent at the model server on the Mac: it then reaches only the Mac and other VMs on that network.
3. Copy the project in
Give the agent a copy of the project, not your working folder. Pushing with git over SSH is the simplest way: only committed files go in, and your git history and settings on the Mac stay untouched.
In the VM, create an empty repository:
vpvm ssh agent-base -- 'git init -q -b main ~/project && git -C ~/project config receive.denyCurrentBranch updateInstead'
On the Mac, in your project, add the VM as a remote and push the branch the agent should start from:
The first push asks you to confirm the VM's host key. Files that git ignores, such as .env,
build output or node_modules, stay on the Mac; the agent installs dependencies itself.
⚠️ Don't copy files with secrets into the VM, even if the project needs them to run. Give the VM test credentials of its own instead.
Alternative: clone from a hosting service. If the VM should clone and push on its own, create
a token limited to that one repository (for example, a GitHub fine-grained token or a deploy key)
with the least access the task needs, and revoke it when you're done. Never copy your personal
SSH key or gh login into the VM.
4. Sign the agent in
Sign in inside the VM, never in User Data:
vpvm ssh agent-base
cd ~/project
claude # follow the sign-in link it prints, in a browser on your Mac
Choose the credential with the VM's exposure in mind:
- A separate API key for agent VMs, with a spending limit set in the provider's console, is the safest choice. You can revoke it at any time without affecting your other work.
- Your subscription login works too, but the token stays in the VM; sign out (or delete the VM) when you're done.
- A local model: point the agent at the model server on the Mac (Ollama, LM Studio and others)
by the Mac's address on the VM's network, and no key is in the VM at all. The model server must
listen on that address, not only on
localhost.
To keep a key out of shell history, read it into the session instead of typing it on the command line:
5. Checkpoint, then run the agent
Before the first run, shut the VM down and take a checkpoint, so you can always return to the clean, signed-in state:
vpvm shutdown agent-base
vpvm checkpoint create agent-base clean --notes "Tools installed, signed in, project pushed"
vpvm start agent-base && vpvm wait agent-base
Run the agent inside tmux, so it keeps working if your SSH connection drops:
With Codex CLI, the matching option is codex --dangerously-bypass-approvals-and-sandbox. Detach
with Control-B, D and reattach later with tmux attach -t agent.
Ask the agent to commit its work on a branch, such as agent/fix-parser, so you can review it
as commits.
While it runs:
- Watch it in Live Resources or from the web dashboard and the mobile apps, from anywhere.
- Take a live checkpoint before a risky step:
vpvm checkpoint create agent-base before-migration --live. Restoring it resumes the VM with the agent's session still open. - Roll back if the run goes wrong:
vpvm checkpoint restore agent-base clean --restartreturns the VM to the clean state in seconds, packages and services included.
6. Review and bring the changes back
Fetch the agent's branch to the Mac. Fetching copies commits only; nothing in them runs:
Before you build, test or merge, look closely at files that run code on your Mac:
- Build and package scripts:
Makefile,package.jsonscripts,setup.py,build.gradle, Xcode build phases. - CI and automation:
.github/workflows/, other CI files,.githooks set up by scripts. - Agent and editor settings:
.claude/,.mcp.json,AGENTS.md,CLAUDE.md,.vscode/,.cursor/. These can tell the agent on your Mac to run commands next time you open the project. - New dependencies, and changes to lock files.
git diff --stat main...vm/agent/fix-parser -- Makefile package.json .github .claude .mcp.json AGENTS.md CLAUDE.md
When you're satisfied, merge the branch or cherry-pick the commits as usual. Restore the clean
checkpoint, or make a new VM, before the next task.
Several agents at once
To run several tasks in parallel, set up one VM as a base, then give each task its own copy. Every copy sets itself up as a new machine on its first start, with its own hostname, machine ID, SSH host keys and IP address.
Linked clones (macOS 27+) are the quickest: they take seconds and use almost no disk space until the agent changes something.
vpvm shutdown agent-base
vpvm checkpoint create agent-base base-ready
vpvm linkedclone create agent-base agent-1 --from-checkpoint base-ready
vpvm linkedclone create agent-base agent-2 --from-checkpoint base-ready
vpvm start agent-1,agent-2
Push the project to each clone and run one agent in each. Delete a clone when its task is done; the base VM stays as it was. See Linked Clones.
Templates work on any macOS: save the base VM as a template and create a VM from it for each task. On APFS, the copy shares the template's disk space until it changes. See Reusable Setups.
Each VM needs its own memory; with 8 GB per VM, check that the Mac has room for all of them alongside your own apps.
macOS VMs for Xcode projects
For projects that need Xcode or other macOS-only tools, use a macOS VM in the same way:
- Create it from a restore image; with Provisioning (macOS 27+) the account and Remote Login (SSH) are set up on the first start, without the Setup Assistant.
- Install Xcode or the Command Line Tools, Homebrew and the agent, then sign in, shut down and take a checkpoint as above. Don't sign in to the VM with your Apple Account unless the task needs it.
- Use linked clones for each task; a macOS VM is large, and copies are much quicker than new installs.
ℹ️ macOS allows at most two macOS VMs to run at the same time. Linux VMs have no such limit.
Clean up
When a task is finished:
- Bring back the changes you want, as in step 6.
- Delete the VM or clone (
vpvm delete agent-1), or restore itscleancheckpoint. - Remove the remote on the Mac if you no longer need it:
git remote remove vm. - Revoke any repository token you created for the VM. Revoke or rotate the agent's API key if you suspect it was exposed.
Security checklist
- [ ] No shared folders, clipboard sharing, file copy and paste or USB devices for the VM.
- [ ] The project copied in with git; no
.envfiles, real credentials or personal SSH keys. - [ ] A separate API key with a spending limit, or a local model.
- [ ] Any repository token limited to one repository and revoked afterwards.
- [ ] Mac services that the VM shouldn't reach bound to
localhostor turned off. - [ ] A checkpoint of the clean state before every unattended run.
- [ ] Changes reviewed, especially build scripts, CI and agent settings, before running them on the Mac.
Troubleshooting
git push vm says the connection was refused or timed out.
The VM isn't ready yet, or its address changed. Run vpvm wait agent-base, then update the
remote: git remote set-url vm ssh://dev@$(vpvm ip agent-base)/home/dev/project.
git push asks about the host key again after a clone or restore.
Each clone gets new SSH host keys and often a new IP address. Remove the old entry with
ssh-keygen -R <old address>, then push again.
The agent can't install packages or reach its model.
Check that the VM's network is Default (NAT), not Host-only, and that the Mac has an internet
connection. vpvm ssh agent-base -- curl -sI https://api.anthropic.com tests the connection from
inside the VM.
The agent's tools aren't installed.
The user data failed or is still running. Open the VM's
Setup Logs or run vpvm cloud-init logs agent-base.
The VM runs out of disk space during a build. Resize the disk in the configuration or the Disk Space Analyzer; cloud images grow their file system on the next start.
See also
- Linux Cloud Images · Cloud-Init settings
- Checkpoints · Linked Clones · Templates
- Command Line · MCP Server, to let an agent on your Mac manage VMs instead of running inside one
- A Computer for Your AI Agent, to let an agent use a VM's screen, for example to test apps
