# Run Codex CLI in the cloud

Run OpenAI Codex CLI inside an isolated Ubuntu 24.04 VM with root, Docker, and a persistent 51 GB disk, either interactively over SSH or headlessly through `arvm exec`.

## 1. Create a VM and connect

Create a `default` machine (4 vCPUs, 8 GB RAM, 51 GB disk) and open a shell on it.

```console
$ arvm new --idle-timeout 2h
$ arvm ssh calm-river-otter
```

The examples below use `calm-river-otter`. Replace it with the name `arvm new` prints.

## 2. Install and sign in to Codex CLI

Node.js LTS is preinstalled on the `ubuntu-24.04` image. Install Codex CLI and sign in inside the VM:

```console
dev@calm-river-otter:~$ npm install -g @openai/codex
dev@calm-river-otter:~$ codex login
```

For headless runs without browser sign-in, export `OPENAI_API_KEY` inside the VM or set it on the machine at creation with `--env`.

## 3. Run Codex interactively or headlessly

For an interactive session that survives SSH disconnects, start `tmux` (or [Rex](rex.md)) and launch `codex` in your repository:

```console
dev@calm-river-otter:~$ git clone https://github.com/example/app.git
dev@calm-river-otter:~$ cd app
dev@calm-river-otter:~$ codex
```

To drive Codex headlessly from your laptop or a CI script without opening SSH, run `codex exec` through `arvm exec`:

```console
$ arvm exec calm-river-otter --cwd /home/dev/app --timeout 30m -- codex exec "Run the test suite and fix failing tests"
```

## 4. Copy patches or artifacts back and stop the VM

Pull generated diffs or logs back to your local machine with `arvm cp`, then stop the VM so billing ends while the disk stays intact:

```console
$ arvm cp calm-river-otter:/home/dev/app/changes.patch .
$ arvm stop calm-river-otter
```

When you are finished with the VM permanently, remove it with `arvm rm calm-river-otter --yes`.

## Idle stop and billing

A `default` VM draws $0.018 an hour from your $20 monthly plan, $13.14 for a full month. A `small` VM (2 vCPUs, 4 GB RAM) draws $0.009 an hour ($6.57 for a full month), and a `large` VM (8 vCPUs, 16 GB RAM, 111 GB disk) draws $0.036 an hour ($26.28 for a full month). Stopped machines cost $0.

Idle stop shuts down a quiet VM after 60 minutes by default (or the duration passed to `--idle-timeout`). Both `arvm ssh` and `arvm exec` count as activity. See [Idle stop](../machines.md#idle-stop) and [Billing](../billing.md).

Disks live on one drive with no automatic backups. Push commits to Git before stopping or deleting a machine.

## Troubleshooting

| Symptom | What to do |
|---|---|
| `arvm exec` fails with `machine_not_running` | Run `arvm start calm-river-otter` first. Unlike `arvm ssh`, `arvm exec` does not wake a stopped machine. |
| `arvm exec` cuts off stdout or stderr | Each stream truncates at 8 MiB. Redirect verbose test output to a file inside the VM and download it with `arvm cp`. |
| Background process started inside `arvm exec` vanishes | Processes left behind when `arvm exec` exits are killed. Use `sudo systemd-run` for background daemons: see [Run a job that outlives the request](../exec-and-files.md#run-a-job-that-outlives-the-request). |
| `codex` cannot reach a local port from your laptop | Forward the port privately with `arvm forward calm-river-otter 8080` or expose it over HTTPS with `arvm expose calm-river-otter 8080`. |

## Related guides

- [Run Claude Code in the cloud](run-claude-code-in-the-cloud.md)
- [Use Rex with a cloud Linux VM](rex.md)
- [Run parallel coding agents across isolated VMs](parallel-agents.md)
- [Run commands and move files](../exec-and-files.md)
