# Codex CLI: install, sign in and run it safely

> How to install OpenAI's Codex CLI on macOS, Linux and Windows, sign in, run your first task, script it with codex exec, and choose sandbox and approval settings.

Codex CLI is OpenAI's coding agent for the terminal. It reads your repository, edits files and runs commands on your machine. Install it with the standalone installer (`curl -fsSL https://chatgpt.com/codex/install.sh | sh` on macOS and Linux), npm (`npm install -g @openai/codex`) or Homebrew (`brew install --cask codex`). Then run `codex` in a project folder and sign in with ChatGPT or an API key. Before you let it work unattended, set the sandbox and approval policy.

## Install Codex CLI

OpenAI's Codex CLI page lists four ways to install. Running the same command again updates Codex, except Homebrew, which uses `brew upgrade --cask codex`.

| Platform | Install command |
| --- | --- |
| macOS and Linux | `curl -fsSL https://chatgpt.com/codex/install.sh \| sh` |
| Windows (PowerShell) | `powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 \| iex"` |
| npm, any platform | `npm install -g @openai/codex` |
| Homebrew | `brew install --cask codex` |

Check the install with `codex --version`. `codex doctor` checks your installation, config, sign-in and runtime health, and `codex update` updates in place.

A piped installer runs whatever the server sends. On a work machine, the npm or Homebrew route gives you a package you can pin and audit.

## Codex CLI on Windows

Run the PowerShell installer from a new PowerShell window. On Windows, Codex sandboxes commands with Windows' own sandbox (MXC) where the device and policy support it, with older `elevated` and `unelevated` modes as fallbacks. Inside WSL2 it uses the Linux sandbox instead. The security docs say WSL1 stopped being supported in Codex 0.115, when the Linux sandbox moved to `bwrap`, so use WSL2 if you run Codex under Linux on Windows.

## Sign in

Run `codex` in a project folder. The first time, choose **Sign in with ChatGPT** to use your plan, or sign in with an API key for usage-based billing. For scripts, pipe the key in so it never lands in shell history.

```bash
printenv OPENAI_API_KEY | codex login --with-api-key
codex login status
```

Credentials are cached in `~/.codex/auth.json` or your OS credential store. Treat that file like a password. [Codex API key: sign-in, auth.json and keeping keys safe](https://www.excellent.so/guides/codex-api-key) covers the options.

## Run your first task

Start Codex from the repository you want it to work in and describe the task in plain words, such as "Tell me about this project". A few slash commands are worth knowing on day one.

| Command | What it does |
| --- | --- |
| `/init` | Writes an `AGENTS.md` file with instructions for Codex |
| `/status` | Shows the current session's configuration |
| `/permissions` | Chooses what Codex may do without asking |
| `/model` | Picks the model and reasoning effort |
| `/review` | Reviews your changes and reports issues |

OpenAI's own advice is to make a Git checkpoint before and after each task so you can revert. `codex --worktree` goes further and runs the session in a new Git worktree, which keeps the agent's edits off your working branch.

## Script Codex with codex exec

`codex exec` runs a task without the interactive screen, for scripts and CI. It reads the prompt from an argument or from standard input.

```bash
codex exec "Update the changelog for the last release"
git diff | codex exec "Summarize this diff" -o summary.md
```

`--json` prints events as JSONL, `-o` writes the agent's last message to a file, and `codex exec resume --last` continues the previous run. `codex exec` expects a Git repository unless you pass `--skip-git-repo-check`.

## Choose sandbox and approval settings

Two settings decide how much Codex can do on its own. Set them with flags for one run or in `~/.codex/config.toml` for every run.

| Flag | Values | Meaning |
| --- | --- | --- |
| `-s`, `--sandbox` | `read-only`, `workspace-write`, `danger-full-access` | What commands Codex runs may touch |
| `-a`, `--ask-for-approval` | `on-request`, `never` | Whether Codex asks before acting |

`workspace-write` with `on-request` is the sensible default for local work. The security docs say the default sandbox has no network access and limits writes to the active workspace. `--dangerously-bypass-approvals-and-sandbox` turns all of it off. The CLI's own help calls it "EXTREMELY DANGEROUS" and meant only for environments that are already sandboxed. See [Codex sandbox](https://www.excellent.so/guides/codex-sandbox), [Codex config.toml](https://www.excellent.so/guides/codex-config-toml) and [the bypass flag](https://www.excellent.so/guides/codex-dangerously-bypass-approvals-and-sandbox).

## Where Codex CLI keeps its files

Everything lives under `CODEX_HOME`, which defaults to `~/.codex`.

| Path | What it holds |
| --- | --- |
| `~/.codex/config.toml` | Your settings |
| `~/.codex/auth.json` | Cached sign-in, unless you use the OS credential store |
| `~/.codex/sessions/` | One JSONL file per session |
| `~/.codex/history.jsonl` | The prompts you typed |
| `~/.codex/AGENTS.md` | Personal instructions for every project |

`codex resume` opens a picker of past sessions, and `codex resume --last` reopens the most recent. Session files record prompts and command output, so a key that reaches either ends up on disk. See [Where Codex keeps session logs](https://www.excellent.so/guides/codex-session-logs).

## Connect tools with MCP

`codex mcp` adds local or remote MCP servers and shows the tools they expose. Each server you add can read and act with your access, so check what it does first. [MCP server security](https://www.excellent.so/guides/mcp-server-security) lists what to look for.

Checked against the vendor documentation and the help output of Codex CLI 0.166.0 on December 4, 2026. [Codex CLI](https://learn.chatgpt.com/docs/codex/cli) · [CLI reference](https://learn.chatgpt.com/docs/developer-commands?surface=cli) · [Approvals and security](https://learn.chatgpt.com/docs/agent-approvals-security) · [Authentication](https://learn.chatgpt.com/docs/auth)

## See what Codex did

Codex shows each command and diff as it runs, but once the session ends that record sits in JSONL files. Excellent's free scan reads supported agent logs on the Mac, including Codex, and reports what the agents ran, including keys found in logs. [Run the free scan](https://www.excellent.so/scan) or [see a sample report](https://www.excellent.so/r/sample). With Pro, Excellent blocks some risky steps and asks before others. [Agent coverage](https://www.excellent.so/agents) · [Pricing](https://www.excellent.so/pricing).

Source: https://www.excellent.so/guides/codex-cli
