Skip to content

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.

5 min read

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.

PlatformInstall command
macOS and Linuxcurl -fsSL https://chatgpt.com/codex/install.sh | sh
Windows (PowerShell)powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"
npm, any platformnpm install -g @openai/codex
Homebrewbrew 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.

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 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.

CommandWhat it does
/initWrites an AGENTS.md file with instructions for Codex
/statusShows the current session's configuration
/permissionsChooses what Codex may do without asking
/modelPicks the model and reasoning effort
/reviewReviews 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.

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.

FlagValuesMeaning
-s, --sandboxread-only, workspace-write, danger-full-accessWhat commands Codex runs may touch
-a, --ask-for-approvalon-request, neverWhether 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, Codex config.toml and the bypass flag.

Where Codex CLI keeps its files

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

PathWhat it holds
~/.codex/config.tomlYour settings
~/.codex/auth.jsonCached sign-in, unless you use the OS credential store
~/.codex/sessions/One JSONL file per session
~/.codex/history.jsonlThe prompts you typed
~/.codex/AGENTS.mdPersonal 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.

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 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 · CLI reference · Approvals and security · Authentication

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 or see a sample report. With Pro, Excellent blocks some risky steps and asks before others. Agent coverage · Pricing.

Related guides

View all