How to Install Codex CLI on Ubuntu 26.04, 24.04 and 22.04

Install Codex CLI on Ubuntu 26.04, 24.04, and 22.04 with OpenAI's standalone installer, then compare npm and Homebrew options, authenticate with ChatGPT or an API key, and manage updates, PATH fixes, and removal.

Last updatedAuthorJoshua JamesRead time7 minGuide typeUbuntu

Installing Codex CLI on Ubuntu gives you OpenAI’s terminal coding agent inside the same shell where you already run git, package managers, tests, and project scripts. Codex can inspect a local repository, explain unfamiliar code, draft patches, review changes, and run commands with the approval and sandbox settings you choose.

OpenAI documents a standalone macOS and Linux installer as the primary setup path for Codex CLI. On Ubuntu, that installer avoids Node.js and npm version differences by downloading the release package, placing the visible codex command under ~/.local/bin, and storing the standalone package under ~/.codex/packages/standalone. npm and Homebrew remain useful alternatives when those package managers already fit your workstation.

Install Codex CLI on Ubuntu

Start by refreshing APT and installing the small HTTPS download prerequisites used by the install methods below. The curl command retrieves OpenAI’s installer, and ca-certificates keeps TLS certificate validation available on minimal systems:

sudo apt update
sudo apt install curl ca-certificates

If your account cannot run sudo, configure administrative access first with the Ubuntu sudoers setup guide, then return to the installation steps.

Ubuntu does not currently provide an APT package named codex for OpenAI Codex CLI, so sudo apt install codex is not the correct install path. Use one of the upstream methods below instead.

MethodSourceBest Fit
Standalone installerOpenAI Codex CLI docsRecommended Ubuntu path; no Node.js, npm, or Homebrew required
npmnpm package @openai/codexSystems where you already maintain a current Node.js and npm setup
HomebrewHomebrew caskLinux Homebrew users who want cask-managed updates

For Ubuntu 26.04, 24.04, and 22.04, start with the standalone installer unless you specifically want npm or Homebrew to own the package. It supports x86_64 and aarch64 Linux systems and handles the matching release asset automatically.

Method 1: Install Codex CLI with OpenAI Installer

The standalone installer is the official no-npm path for Linux systems. It detects the CPU architecture, downloads the matching OpenAI release package from GitHub, verifies checksums, creates the visible codex command, and adds a PATH block to your shell profile when needed.

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

If your policy does not allow piping installers directly into sh, save the script first, review it, then run the local copy:

curl -fsSL https://chatgpt.com/codex/install.sh -o codex-install.sh
${PAGER:-less} codex-install.sh
sh codex-install.sh
rm codex-install.sh

If the installer adds ~/.local/bin to ~/.bashrc, open a new terminal or refresh the current shell before running codex:

export PATH="$HOME/.local/bin:$PATH"
codex --version
codex-cli 0.x.x

For scripted installs or remote bootstrap sessions where you do not want installer prompts, set CODEX_NON_INTERACTIVE=1 on the shell that runs the downloaded script:

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

The installer uses ~/.local/bin by default. If you need a different command location, set CODEX_INSTALL_DIR and reuse the same value when you update or remove the standalone install later.

Ubuntu Node.js Package Notes

The current npm package declares a Node.js engine of >=16. Ubuntu’s default Node.js packages differ by release, which affects whether the npm method is a clean choice on a fresh system:

Ubuntu ReleaseDefault nodejs CandidateCodex npm Method
Ubuntu 26.0422.xMeets the current Codex npm engine
Ubuntu 24.0418.xMeets the current engine, but use your normal current Node.js source if you need newer JavaScript tooling
Ubuntu 22.0412.xToo old for the current Codex npm package; use the standalone installer or install a newer Node.js line first

If you need a newer Node.js stack before using npm, the Ubuntu Node.js installation guide covers repository, NodeSource, and NVM options. Return here after node --version reports Node.js 16 or newer.

Method 2: Install Codex CLI with npm

The npm method is OpenAI’s package-manager path for users who already maintain Node.js and npm. Check your current versions before installing Codex CLI this way:

node --version
npm --version
v22.x.x
10.x.x

If node --version reports a version older than 16, skip this method until you install a newer Node.js line, or use the standalone installer method.

Install Codex into a user-owned prefix so npm does not need root permissions for the global package:

mkdir -p ~/.local/bin
grep -qxF 'export PATH="$HOME/.local/bin:$PATH"' ~/.bashrc || printf '%s\n' 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
export PATH="$HOME/.local/bin:$PATH"
npm install -g @openai/codex --prefix "$HOME/.local"

Avoid sudo npm install -g @openai/codex. Using a home-directory prefix keeps the package, generated command, and future updates owned by your account instead of root.

Verify the npm-managed command:

codex --version
codex-cli 0.x.x

Method 3: Install Codex CLI with Homebrew

Use the Homebrew method only if Linuxbrew is already part of your Ubuntu toolchain. If Homebrew is not installed yet, follow the Ubuntu Homebrew installation guide first, then return here.

Install the official Homebrew cask:

brew install --cask codex

Homebrew installs the Codex release binary and manages related cask updates through the same brew workflow you use for other developer tools.

Verify the Homebrew install:

codex --version
codex-cli 0.x.x

Authenticate Codex CLI

Codex CLI needs OpenAI authentication before it can send prompts and code context to the model. OpenAI’s Codex authentication documentation supports ChatGPT sign-in for subscription access and API-key sign-in for usage-based Platform billing. The CLI defaults to ChatGPT sign-in when no valid local session is available.

Sign In with ChatGPT

Start Codex inside a project directory:

cd ~/your-project
codex

Choose the ChatGPT sign-in option when prompted. Codex opens a browser-based login flow, then stores local configuration, credentials, and session data under ~/.codex/.

On an SSH session or another headless system, try the device-code login flow if your account or workspace allows it:

codex login --device-auth

Check or reset your login state with these commands:

codex login status
codex logout
codex login

Sign In with an API Key

For Platform-billed workflows, pipe an API key into the login command. This avoids saving the key directly in your shell history:

read -rsp "OpenAI API key: " OPENAI_API_KEY
printf '\n'
printf '%s' "$OPENAI_API_KEY" | codex login --with-api-key
unset OPENAI_API_KEY

You can also use an existing environment variable:

printenv OPENAI_API_KEY | codex login --with-api-key

API-key authentication is useful for local CLI workflows and automation, but it follows your OpenAI Platform account’s billing and data controls instead of ChatGPT workspace entitlements. For one-off non-interactive runs, CODEX_API_KEY is supported by codex exec; set it only for the specific command that needs it.

Use Codex CLI on Ubuntu

These Ubuntu commands install the terminal CLI. OpenAI also references Codex Web, IDE integrations, and a desktop app flow, but those are separate from the Ubuntu shell command installed here. Run codex --help for the current command list and codex doctor when you need a local install, auth, or runtime diagnostic.

Interactive and Non-Interactive Prompts

Launch an interactive Codex session from a project directory:

codex

You can also start with an initial prompt:

codex "explain the authentication flow in this project"

Use exec for a non-interactive task:

codex exec "summarize this repository and list risky files"

Review Local Changes

Codex can review uncommitted work, a branch comparison, or a specific commit:

codex review --uncommitted
codex review --base main
codex review --commit HEAD~1

Add a prompt when you want to narrow the review focus:

codex review --uncommitted "focus on authentication and file-permission risks"

Resume Sessions and Apply Diffs

Resume a previous session, fork the latest session, or apply a Codex task diff to your working tree:

codex resume
codex resume --last
codex fork --last
CODEX_TASK_ID="task-id-from-codex-cloud"
codex apply "$CODEX_TASK_ID"

The codex apply command expects a Codex task ID and applies that task’s latest diff with git apply.

Use Images and Model Options

Attach screenshots or diagrams when visual context matters:

codex -i screenshot.png "describe the UI changes needed"

Use a model flag only when you intentionally need a specific model available to your account:

CODEX_MODEL="model-name"
codex -m "$CODEX_MODEL" "refactor this function"

Choose a Sandbox Mode

Sandbox modes control what Codex can do while it works:

  • read-only: lets Codex inspect files without writing changes.
  • workspace-write: allows edits in the project workspace while keeping broader filesystem access limited.
  • danger-full-access: removes filesystem restrictions and should be reserved for trusted, intentional work.
codex --sandbox read-only
codex --sandbox workspace-write
codex --sandbox danger-full-access

For low-friction local work, combine the workspace sandbox with an explicit approval policy instead of relying on deprecated compatibility shortcuts:

codex --sandbox workspace-write --ask-for-approval on-request

Do not use --dangerously-bypass-approvals-and-sandbox unless you fully trust the repository, prompt, and command side effects. The flag name is intentionally explicit because it removes core safety controls.

Update Codex CLI

Update Codex CLI with the same method you used to install it.

Update Standalone Installer

For the standalone installer method, rerun the official installer. If you used a custom CODEX_INSTALL_DIR, set the same value before sh:

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

Update npm Install

export PATH="$HOME/.local/bin:$PATH"
npm install -g @openai/codex@latest --prefix "$HOME/.local"
codex --version

Update Homebrew Install

brew upgrade --cask codex
codex --version

Troubleshoot Codex CLI

APT Cannot Locate Codex

If APT reports that it cannot locate a codex package, that is expected. OpenAI Codex CLI is not installed from Ubuntu’s APT repositories, so use the standalone installer, npm package, or Homebrew cask instead.

Codex Command Not Found

For standalone installer and user-prefix npm installs, make sure ~/.local/bin is in your current PATH:

export PATH="$HOME/.local/bin:$PATH"
command -v codex
/home/username/.local/bin/codex

If the standalone installer added a PATH block to ~/.bashrc, either open a new terminal or export the path in the current session before retrying.

Node.js Is Too Old for npm

Ubuntu 22.04’s default Node.js 12 package is too old for the current Codex npm package. Check your version before troubleshooting npm errors:

node --version
v12.22.x

If you see an older major version, use the standalone installer or install a newer Node.js line before retrying npm.

npm Permission Errors

If npm fails with an EACCES permission error, reinstall Codex with the user-owned prefix shown earlier:

npm install -g @openai/codex --prefix "$HOME/.local"

Then refresh your current shell PATH:

export PATH="$HOME/.local/bin:$PATH"
codex --version

Authentication Fails or Uses the Wrong Account

Check the active login, sign out, then start a fresh login flow:

codex login status
codex logout
codex login

If you want a full local reset, remove the Codex configuration directory after signing out. This deletes saved credentials and session history:

rm -rf ~/.codex
codex login

Installer Reports Unsupported Architecture

The standalone installer supports common Linux x86_64 and aarch64 systems. If it reports an unsupported architecture, confirm what the kernel reports:

uname -m
aarch64

On unsupported architectures, wait for an upstream Codex CLI package that matches your hardware or use another supported machine. Do not force an x86_64 binary onto an ARM64 system.

Remove Codex CLI

Remove Codex CLI with the same method you used to install it.

Remove Standalone Installer

For the default standalone installer path, remove the visible command and the standalone package cache:

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

If the installer added a PATH block to ~/.bashrc, remove the marked block as well:

sed -i '/# >>> Codex installer >>>/,/# <<< Codex installer <<</d' ~/.bashrc

Remove npm Install

npm uninstall -g @openai/codex --prefix "$HOME/.local"
rm -f ~/.local/bin/codex

Remove Homebrew Install

brew uninstall --cask codex

Remove Legacy Manual Binary Install

If you previously installed an older manual binary or helper under /usr/local/bin, remove those files separately:

sudo rm -f /usr/local/bin/codex /usr/local/bin/update-codex-cli

Remove Local Codex Data

Codex stores local configuration, authentication state, and session history in ~/.codex/. Remove this directory only when you want a full reset or permanent uninstall.

This command permanently deletes Codex CLI configuration, credentials, and local session history for your user account.

rm -rf ~/.codex

Verify that the command has been removed from your PATH:

command -v codex || echo "codex removed"
codex removed

Conclusion

Codex CLI is ready on Ubuntu when codex --version works and codex login status shows the account you intend to use. Keep the standalone OpenAI installer as the default no-npm path across Ubuntu 26.04, 24.04, and 22.04; use npm or Homebrew only when those package managers already fit your workstation. If you compare terminal coding assistants, related Ubuntu guides cover Gemini CLI, Claude Code, and GitHub Copilot CLI.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show our tutorials more often in Top Stories and mark them as preferred in AI Mode and AI Overviews when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<a href="https://example.com">link</a> link
<blockquote>quote</blockquote> quote block

Add to the discussion

Questions, fixes, command output, and version notes help keep this guide current.

Verify before posting: