> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microsandbox.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Codex CLI

> Run OpenAI Codex CLI against a project on your host

This guide installs a pinned Codex CLI release inside a Node.js microVM and opens it in a project mounted from your host. Codex can work directly in the checkout, while its executable, authentication, configuration, and sessions stay inside the sandbox.

## Run Codex CLI

**1. Start Codex**

Local-only (Writable host-directory mounts are local-only. On microsandbox cloud, use the isolated-copy alternative and omit replace-on-create.)

Replace `./my-project` with the project directory you want Codex to work on:

```sh macOS & Linux theme={null}
msb run -t --name codex-demo --replace \
  --cpus 2 --memory 2G --root-disk 4G \
  --mount-dir ./my-project:/workspace:rw \
  --workdir /workspace \
  node:24-bookworm-slim -- sh -lc '
    apt-get update &&
    apt-get install -y --no-install-recommends ca-certificates git &&
    npm install -g @openai/codex@0.148.0 &&
    exec codex
  '
```

```powershell Windows theme={null}
msb run -t --name codex-demo --replace `
  --cpus 2 --memory 2G --root-disk 4G `
  --mount-dir ./my-project:/workspace:rw `
  --workdir /workspace `
  node:24-bookworm-slim -- sh -lc '
    apt-get update &&
    apt-get install -y --no-install-recommends ca-certificates git &&
    npm install -g @openai/codex@0.148.0 &&
    exec codex
  '
```

On first launch, choose Sign in with ChatGPT, Sign in with Device Code, or provide an API key. Browser-based sign-in happens on your host while the resulting Codex credentials are stored on the sandbox's root disk.

> [!TIP]
>
> For an isolated workspace, or when using microsandbox cloud, replace `--mount-dir ./my-project:/workspace:rw` with `--copy-dir ./my-project:/workspace`.

**2. Start another session**

After leaving Codex, return to the same sandbox and workspace with:

```sh Shell theme={null}
msb exec -t codex-demo -- codex
```

The sandbox root disk retains the files under `/root/.codex`, including authentication, configuration, and session history.

**3. Verify the installation**

Exit the TUI and run:

```sh Shell theme={null}
msb exec codex-demo -- codex --version
```

The pinned example prints `codex-cli 0.148.0`.

**4. Review or export changes**

With the default writable mount, changes are already in the host checkout. Review them inside the sandbox:

```sh Shell theme={null}
msb exec codex-demo -- sh -lc 'cd /workspace && git status --short && git diff --stat && git diff'
```

If you chose the isolated `--copy-dir` alternative, create a patch inside the sandbox:

```sh Shell theme={null}
msb exec codex-demo -- sh -lc 'cd /workspace && git add -N . && git diff --binary > /tmp/codex.patch'
```

Copy the patch to the host:

```sh Shell theme={null}
msb cp codex-demo:/tmp/codex.patch ./codex.patch
```

Check that it applies cleanly before applying it:

```sh Shell theme={null}
git apply --check ./codex.patch
```

**5. Clean up**

Remove the sandbox:

```sh Shell theme={null}
msb rm -f codex-demo
```

Removing the sandbox also removes its Codex credentials, configuration, and sessions. Changes made through the default workspace mount remain in the host checkout.

## Reference

* [Codex CLI](https://developers.openai.com/codex/cli/)
* [Codex CLI repository](https://github.com/openai/codex)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.