How to use Codex: install and run the Codex CLI

A hands-on guide to OpenAI's terminal agent: install it, sign in, run a first task, and learn the commands, files and settings that matter.

Last updated

Abstract illustration of a glowing terminal prompt opening onto a path of small lit squares

To run the Codex CLI, install it with one command, open a terminal in your project folder, type codex, and choose Sign in with ChatGPT. Then describe what you want in plain words ("find why the signup test fails and fix it") and Codex reads your code, edits files and runs commands inside a sandbox, asking you before it goes beyond it.

That's the loop. This guide is the practical part: the install for each system, the first session, the slash commands you'll actually use, AGENTS.md, permissions, models, MCP, skills, scripting with codex exec, and how to update or remove it. If you want to know what Codex is first, where else it runs and what each plan costs, read what is Codex.

Everything below was checked against OpenAI's documentation and the openai/codex repository on November 30, 2026, and every command was confirmed in the --help output of Codex CLI 0.167.1 (the latest release on GitHub that day was 0.172.0, published November 24, 2026). Codex ships often; if something on your screen differs, codex --help and the official docs win.

What you need before you start

  • A ChatGPT plan that includes the CLI, or an API key. As of November 2026, OpenAI's pricing page lists the CLI from ChatGPT Plus ($20 per month) upward, or with an API key billed per use. Free and Go include Codex in the desktop app only. Details on what is Codex and, for how much you can do, Codex limits.
  • A supported system. The openai/codex install doc lists macOS 12 or later and Ubuntu 20.04+ or Debian 10+, with 4 GB of RAM minimum (8 GB recommended). For Windows, OpenAI's Windows docs say Windows 11 is the recommended baseline and Windows 10 version 1809 or newer is "best effort".
  • Git. Optional but recommended (2.23 or later, per the install doc). Codex checks whether your folder is under version control to choose its default permissions, and codex exec wants a Git repository unless you tell it otherwise.
  • A project folder. Any code folder works.

How to install Codex on each system

Pick one line. The standalone installers are the ones OpenAI's CLI docs show first.

macOS or Linux:

curl -fsSL https://chatgpt.com/codex/install.sh | sh

Windows, from a new PowerShell window:

powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"

With npm or Homebrew instead:

npm install -g @openai/codex
brew install --cask codex

Open a new terminal and check it:

codex --version

It prints something like codex-cli 0.167.1. If anything looks wrong later (sign-in, config, network), run the built-in health check:

codex doctor

A few system notes worth knowing up front:

  • Linux and WSL2: the sandbox needs bubblewrap (bwrap). Install it with your package manager; Codex warns at startup if it's missing. On Ubuntu 24.04 you may also need to load the bwrap-userns-restrict AppArmor profile.
  • Windows: Codex uses a native Windows sandbox in PowerShell and the Linux sandbox inside WSL2. OpenAI suggests WSL only when you need Linux-native tooling. /setup-default-sandbox inside a session sets up the elevated Windows sandbox.
  • Where it lands: the macOS and Linux script installs to ~/.local/bin/codex and adds that folder to your PATH; the Windows script uses %LOCALAPPDATA%\Programs\OpenAI\Codex\bin. Your settings live in ~/.codex (%USERPROFILE%\.codex on Windows). These paths come from the install scripts themselves.

How to sign in

Go to your project and start it:

cd your-project
codex

On first run, choose Sign in with ChatGPT and finish in the browser. That uses your plan's Codex allowance. You can also sign in from the shell:

Command What it does
codex login Opens the browser to sign in with ChatGPT
codex login --device-auth Device code sign-in, for SSH sessions and machines without a browser
`printenv OPENAIAPIKEY \ codex login --with-api-key`
codex login status Shows which method you're signed in with
codex logout Removes stored credentials

Credentials are shared by the CLI and the IDE extension. By default they can sit in a plain file, ~/.codex/auth.json, so treat it like a password; the cli_auth_credentials_store setting can move them to your operating system's keyring.

Your first session

OpenAI's own first prompt is a good one, because it's cheap and shows how well Codex understands your code:

Tell me about this project

Then a small, real change:

add validation so the signup form rejects empty emails, add a test, and run the tests until they pass

You can also start with the prompt already typed, or attach a screenshot:

codex "explain how authentication works in this app"
codex -i screenshot.png "make the header match this design"

Inside a session you can paste an image straight into the composer, attach a file or folder with /mention, and steer Codex while it works. Two small shortcuts from the docs: Ctrl+L clears the screen but keeps the chat, and Ctrl+O copies the latest response.

One habit from OpenAI's docs that saves you every time: make a Git commit before and after each task, so you can roll back anything the agent did.

The slash commands that matter

The Codex CLI has about 60 slash commands. Type / and keep typing to filter. These are the ones you'll use in your first month:

Command What it's for
/init Generates an AGENTS.md scaffold for the project
/permissions Changes what Codex may do without asking
/model Picks the model and reasoning effort
/plan Plan mode, for anything bigger than a small fix
/review Reviews your working tree for issues
/diff Shows the Git diff, including untracked files
/status Model, approval policy, writable folders and token usage
/usage Account token usage, or redeem a reset
/new A fresh chat in the same session
/compact Summarizes the chat to free up context
/resume Reopens a saved chat
/fork Branches the current chat into a new one
/worktree Runs the chat in a managed Git worktree
/mcp Lists MCP servers and their tools
/skills Browses and applies skills
/fast Turns the Fast service tier on or off, where the model offers one
/quit Leaves the CLI (/exit also works)

Write a short AGENTS.md

AGENTS.md is the file Codex reads before it starts working. It's where you write the things it can't work out from the code: how to run the tests, which tool your team uses, what not to touch. Run /init to get a scaffold, then trim it.

How Codex finds it, per OpenAI's AGENTS.md docs:

Location Scope
~/.codex/AGENTS.md Your preferences in every project
AGENTS.md at the repo root The project, shared through Git
AGENTS.md in a subfolder That part of the codebase; it comes later, so it overrides the root
AGENTS.override.md Replaces the regular file in the same folder

Codex walks from the project root down to the folder you're in and joins what it finds, and it stops adding text once the total reaches 32 KiB (project_doc_max_bytes). The instructions are read once per session, so restart Codex after you edit the file. To see what it actually loaded:

codex --ask-for-approval never "Summarize the current instructions."

OpenAI's best practices put it well: "A short, accurate AGENTS.md is more useful than a long file full of vague rules," and when Codex makes the same mistake twice, ask it for a retrospective and update the file. Agents also do much better in codebases with clear conventions and a real test suite, which is why RailsFast ships with an AGENTS.md and a CI script an agent can run.

Approval and sandbox modes

Two settings decide what Codex can do on its own. The sandbox limits what the commands it runs can touch. The approval policy decides when it stops to ask you.

Sandbox What commands can do
read-only Read files; edits and commands need approval
workspace-write Read, edit and run routine commands inside the project folder
danger-full-access Anything, with no file or network boundary

The approval policy is on-request (Codex asks when it needs to go beyond the sandbox) or never. When you start Codex in a folder under version control, it recommends workspace-write with on-request approvals; in a folder that isn't, it recommends read-only. In workspace-write, network access is off and .git, .codex, .agents and .aws stay read-only.

The easy way to change it is /permissions. OpenAI's permission modes page describes these modes (the CLI picker's labels can differ: the CLI slash command docs mention presets such as Auto and Read Only):

  • Ask for approval: works in the workspace, asks before editing outside it or using the internet. OpenAI recommends this for most work.
  • Approve for me: the same sandbox, but routine approval requests go to an automatic reviewer and only potentially unsafe actions reach you. The flag is --approve-for-me.
  • Full access: unrestricted access to the internet and any file on your computer.
  • Custom: named or custom permission profiles.

From the shell, or permanently in ~/.codex/config.toml:

codex --sandbox workspace-write --ask-for-approval on-request
sandbox_mode = "workspace-write"
approval_policy = "on-request"

[sandbox_workspace_write]
network_access = true

--dangerously-bypass-approvals-and-sandbox removes both. The CLI's own help calls it "EXTREMELY DANGEROUS" and meant for environments that are already sandboxed, like a throwaway container. If you need one extra folder to be writable, --add-dir <DIR> is the narrower fix.

Models and reasoning effort

If you don't pick a model, Codex uses a recommended one. As of November 2026, OpenAI's models page lists six, and you can name any of them with -m:

Model OpenAI's suggested use
gpt-6.2-astra Demanding projects, like a bug across services
gpt-6.2-sol Everyday tasks such as editing code
gpt-6-astra Reasoning plus computer use across code, apps and research
gpt-6.1-sol Close to GPT-6 Astra at a lower cost; long, repeated work
gpt-6.1-terra Fast and efficient (needs CLI 0.159.0 or later)
gpt-6-luna The most efficient, for focused high-volume tasks
codex -m gpt-6.2-sol

Inside a session, /model switches the model, and "More reasoning..." in that menu sets the effort: Low, Medium, High and Extra High, plus Max and Ultra on some models (Ultra uses subagents). To make a choice permanent, add model = "gpt-6.2-sol" to ~/.codex/config.toml; the desktop app, CLI and IDE extension share that file. Bigger models and higher effort use up your allowance faster, so drop down for simple jobs.

How to add MCP to Codex

MCP servers connect Codex to outside tools: docs, Figma, Sentry, GitHub, a browser. (New to the idea? Read what is MCP.) A local server goes after --:

codex mcp add context7 -- npx -y @upstash/context7-mcp

A remote one takes --url:

codex mcp add example --url https://mcp.example.com

Add --env KEY=VALUE for a local server's settings or --bearer-token-env-var for a remote server's token. Then:

  • codex mcp list shows what's configured, codex mcp remove <name> removes one.
  • codex mcp login <name> signs in to a server that uses OAuth (--no-browser on a remote machine).
  • /mcp inside a session shows active servers and their tools.

Servers are saved as [mcp_servers.<name>] tables in ~/.codex/config.toml. A project can have its own .codex/config.toml, which Codex only loads for projects you've marked as trusted. Connect only servers you trust.

Skills and plugins

A skill is a folder with a SKILL.md file: a name and description in the frontmatter, then the instructions. Codex picks one on its own when your request matches the description, or you call it with $ (type $ in the composer, or browse with /skills).

---
name: release-notes
description: Drafts release notes from merged PRs. Use for release notes or changelog requests, not for commit messages.
---

List the merged PRs since the last tag, group them into Features, Fixes and Breaking changes, and output Markdown.

Where Codex looks: .agents/skills in your repo for project skills, $HOME/.agents/skills for your own. Two come built in: $skill-creator writes a new skill from your description, and $skill-installer installs curated ones (for example $skill-installer linear). Restart Codex if a new skill doesn't show up.

Plugins bundle skills, MCP servers and hooks. Browse them with /plugins, or from the shell with codex plugin list, codex plugin add and codex plugin remove. Skills use the same SKILL.md format as Claude Code, so a good one can move between them; our Claude Code skills guide goes deeper on writing them.

How to resume a session

Every interactive chat is saved, so you can pick up where you left off:

codex resume          # picker of chats from this folder
codex resume --last   # straight back into the most recent one
codex resume --all    # include chats from every folder

You can also pass a session id or a name you gave it with /rename, as in codex resume <name>. Inside a session, /resume does the same. codex fork --last copies a chat into a new one and leaves the original alone, which is handy when you want to try a second approach.

Run Codex in scripts with codex exec

codex exec runs a task without the interactive screen. Progress goes to stderr and only the final message to stdout, so you can pipe it:

codex exec "summarize what changed in the last 10 commits"
npm test 2>&1 | codex exec "explain why these tests fail"

Two defaults to know: codex exec runs read-only, and it wants a Git repository. Loosen them on purpose:

codex exec --sandbox workspace-write "run the tests and fix any failures"
codex exec --skip-git-repo-check "..."

Useful flags for automation: --json prints events as JSON Lines, -o result.md writes the final message to a file, --output-schema schema.json makes the answer follow a JSON Schema, and --ephemeral doesn't save the session. codex exec resume --last "now add docs" continues the previous run. OpenAI's docs note that --full-auto is no longer supported; use --sandbox workspace-write instead.

In CI, OpenAI suggests setting CODEX_API_KEY for the single command rather than for the whole job, so build scripts can't read it, and recommends its GitHub Action for GitHub workflows.

For a quick review of your changes without opening a session:

codex review --uncommitted
codex review --base main

Use Codex in VS Code, Cursor and the cloud

The IDE extension. Codex for VS Code and compatible editors (Cursor and Windsurf included) is in the Visual Studio Code Marketplace under the ID openai.chatgpt. After installing it, click the Codex icon in the sidebar, or run "Codex: Open Codex Sidebar" from the Command Palette, and sign in. It shares your login and config.toml with the CLI, and permissions are set from a control under the composer. Xcode and JetBrains IDEs have their own Codex integrations.

Cloud tasks. Longer work can run on OpenAI's machines against a connected GitHub repository. From the terminal, the experimental codex cloud command lists tasks, submits new ones (codex cloud exec), shows their diff and applies it locally; codex apply <TASK_ID> applies a cloud task's latest diff with git apply. What is Codex covers how cloud tasks work.

Common mistakes

  • Full access on your real machine. OpenAI lists "giving Codex full permission to your computer before you understand the workflow" as a common mistake. Start with Ask for approval or Approve for me.
  • No Git checkpoint. Commit before a big task. It's your undo button.
  • Durable rules in every prompt. If you repeat it, it belongs in AGENTS.md or a skill.
  • One chat for a whole project. OpenAI's advice is one chat per coherent outcome. /new is free; /compact when a long chat still matters.
  • No way to check the work. Put the build and test commands in AGENTS.md and say what "done" means ("the auth tests pass"). Codex can then run the check and fix its own failures.
  • Two tasks on the same files. Use /worktree or codex --worktree so parallel chats don't overwrite each other.
  • Debugging an outage. If Codex hangs or errors for no reason you can find, check whether Codex is down before you touch your setup. Run codex doctor for local problems.

How to update Codex

codex update

It checks for a new version and installs it, when your install supports self-update. Otherwise, update the way you installed: run the same install script again (macOS, Linux or Windows), npm install -g @openai/codex, or brew upgrade --cask codex. Then confirm with codex --version.

How to uninstall Codex

OpenAI's docs don't have an uninstall page, so remove it the way you installed it. The npm and Homebrew commands below are the ones Codex's own install script suggests when it finds an existing copy:

npm uninstall -g @openai/codex
brew uninstall --cask codex

For the standalone script on macOS or Linux, delete the binary and its package folder (on macOS the script also links a helper, codex-code-mode-host, into the same folder):

rm -f ~/.local/bin/codex ~/.local/bin/codex-code-mode-host
rm -rf ~/.codex/packages/standalone

The script may also have added a PATH block, marked # >>> Codex installer >>>, to a shell profile (~/.zprofile for zsh on macOS, ~/.zshrc or ~/.bashrc on Linux); delete it if you want the folder off your PATH.

On Windows, the standalone install is in %LOCALAPPDATA%\Programs\OpenAI\Codex. If you used the legacy elevated Windows sandbox, codex sandbox uninstall removes its machine-wide accounts and network rules; run it before you delete the binary.

That keeps your settings. To remove them too, delete ~/.codex (or %USERPROFILE%\.codex), which holds config.toml, your AGENTS.md, MCP settings and auth.json. Run codex logout first if you'd like the credentials cleared properly.

Frequently asked questions

How do I run the Codex CLI?

Install it, open a terminal in your project folder and type codex. The first time, choose Sign in with ChatGPT (or use an API key), then type your task in plain words. Use codex "your task" to start with a prompt, or codex exec "your task" to run it without the interactive screen.

How do I get Codex in the terminal?

Run curl -fsSL https://chatgpt.com/codex/install.sh | sh on macOS or Linux, the PowerShell installer on Windows, or npm install -g @openai/codex anywhere Node.js runs. Open a new terminal and check it with codex --version. As of November 2026 the CLI needs ChatGPT Plus or higher, or an API key.

How do I use Codex in VS Code?

Install the Codex extension (ID openai.chatgpt) from the VS Code Marketplace, then open it from the Codex icon in the sidebar or with "Codex: Open Codex Sidebar" in the Command Palette. Sign in with the same ChatGPT account you use for the CLI. The extension shares your login and config.toml with the CLI.

How do I use Codex in Cursor?

The same Codex extension works in VS Code-compatible editors, and OpenAI names Cursor and Windsurf. Install it, open it from the sidebar or the Command Palette, and sign in. You can also run the CLI in Cursor's built-in terminal.

How do I run Codex with full permissions?

Pick the full-access option in /permissions, or start with --sandbox danger-full-access, which removes the sandbox (OpenAI describes full access as that plus approval_policy = "never"). --dangerously-bypass-approvals-and-sandbox removes both sandbox and approvals and is meant only for environments that are already isolated, like a container. For fewer prompts on your own machine, Approve for me (--approve-for-me) is the safer choice.

How do I change the model in Codex?

Type /model in a session to pick a model and reasoning effort, or start with codex -m gpt-6.2-sol (or any other model name). To make it the default, add model = "gpt-6.2-sol" to ~/.codex/config.toml. As of November 2026 OpenAI suggests GPT-6.2 Sol for everyday work and GPT-6.2 Astra for demanding projects.

How do I resume a Codex session?

Run codex resume to pick a recent chat from the current folder, codex resume --last to jump into the most recent one, or codex resume --all to see every folder. Inside a session, /resume does the same. For scripted runs, codex exec resume --last continues the previous one.

How do I add MCP to Codex?

Run codex mcp add <name> -- <command> for a local server or codex mcp add <name> --url <url> for a remote one. codex mcp list shows what's set up, and codex mcp login <name> signs in to servers that use OAuth. The settings are saved in ~/.codex/config.toml.

Does Codex have skills?

Yes. A skill is a folder with a SKILL.md file in .agents/skills (project) or ~/.agents/skills (personal), and you call it with $name or let Codex pick it when the task matches. The built-in $skill-creator writes one for you from a description.

Does Codex have subagents?

Yes. OpenAI's CLI docs describe subagents for delegating focused work and bringing the findings back, and /subagents switches between subagent threads in a session. The Ultra reasoning level also uses subagents on models that support it.

How do I turn off fast mode in Codex?

Type /fast in a session. It turns the Fast service tier on or off for models that offer one.

How do I paste an image into the Codex CLI?

Paste the image straight into the composer, or attach it when you start with codex -i screenshot.png "your prompt". A screenshot of a broken layout or an error is often faster than describing it.

How long should Codex prompts be?

OpenAI doesn't give a length. Its best practices say a good prompt covers four things: the goal, the context (files, docs, errors), the constraints, and what "done" looks like, such as tests passing. Rules you repeat in every prompt belong in AGENTS.md instead.

How do I update Codex?

Run codex update. If your install doesn't support self-update, run the install script again, npm install -g @openai/codex, or brew upgrade --cask codex, depending on how you installed it.

Where is Codex installed on Windows?

The PowerShell installer puts the binary in %LOCALAPPDATA%\Programs\OpenAI\Codex\bin and keeps settings in %USERPROFILE%\.codex. If you installed with npm, it's wherever npm puts global packages.

Does Codex run on Linux?

Yes. The standalone installer supports macOS and Linux, and GitHub releases include Linux binaries for x86_64 and arm64. On Linux and WSL2, install bubblewrap so the sandbox works; the openai/codex install doc lists Ubuntu 20.04+ and Debian 10+.

Sources

All checked on November 30, 2026.

Checklist

Grab your free launch checklist 🚀

The definitive checklist to launch any project. It includes pre-launch steps & a detailed launch checklist you can go through to get your new project off the ground.

Secure connection. No spam.