Closing the lid ends the session. That is the whole problem, and a server is the cheapest fix: a Linux box that is always on, reachable over SSH, running the same CLI you already use. The setup is four commands and two failure modes, and this is the version of the walkthrough that names them.
What the machine needs
The install notes put the CLI itself at 4 GB of RAM minimum, 8 GB recommended. That is a floor, not a plan. A coding agent spends most of its memory on the toolchain it drives: tsc, jest, a bundler, a language server, a database container.
| Size | What it holds | Honest use |
|---|---|---|
| 2 vCPU / 4 GB | One agent, one small repo | Trying it. Expect OOM on a real build. |
| 2 vCPU / 8 GB | One agent plus a build | The sensible default for a single task at a time |
| 4 vCPU / 16 GB | Two or three agents, one build each | Parallel work, or a monorepo |
| 8 vCPU / 32 GB | Four or more agents | Diminishing returns; the model’s rate limits bind first |
Pick a current Ubuntu LTS. Everything below assumes Debian packaging, sudo, and a user account that is not root.
Install it
Use npm as the spec says, but check which npm you have first. A distribution’s packaged Node is usually years behind, and a global install against it produces a CLI that fails at startup with a syntax error rather than a useful message.
# Base tools: git for the repo, tmux for the session, ripgrep because agents use it constantly
sudo apt update && sudo apt install -y git tmux ripgrep
# A current Node, via NodeSource; nvm works too if you prefer per-user installs
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
# The agent itself
sudo npm install -g @openai/codex
codex --version
OpenAI also ships a standalone installer, curl -fsSL https://chatgpt.com/codex/install.sh | sh, which avoids npm entirely and puts the binary under your home directory. Either is fine. What matters is that codex --version prints something on the server, not on your laptop.
Sign in without a browser
This is where most people stall. The CLI’s normal sign-in opens a browser, and a headless box has none. Three options, in order of how much I would trust them:
1. An API key in the environment. Set OPENAI_API_KEY in the server’s shell profile or a systemd unit, and the CLI uses it without an interactive login. Simplest, and the key is a secret you now have to rotate and keep off the disk image.
2. Copy the credentials file. Sign in once on your laptop, then move the file the CLI writes. It contains live tokens, so treat it like a private key.
# On the laptop, after one successful `codex` sign-in
scp ~/.codex/auth.json server:~/.codex/
# On the server
chmod 600 ~/.codex/auth.json # the file is a bearer token; do not leave it world-readable
3. Forward the callback port. Run ssh -L for the loopback port the login flow prints, sign in from your laptop’s browser, and the callback lands on the server. It is the cleanest option and the one most likely to break after a vendor changes ports.
Clone with a key, not a password
A deploy key is per-repository and read-only by default, which is the right default for an agent that mostly reads. If the agent has to push branches, give it a second key with write access on that one repository, or let it commit locally and push from your laptop.
ssh-keygen -t ed25519 -C "codex@server" -f ~/.ssh/id_ed25519_agents -N ""
cat ~/.ssh/id_ed25519_agents.pub # paste into the repo's Settings -> Deploy keys
git clone git@github.com:you/api.git ~/repos/api
Keep the agent’s repos in one directory, ~/repos, and nothing else there. When you later need to answer “what was it working on”, the answer is a ls.
Run it under tmux
tmux new -s codex
cd ~/repos/api
codex --sandbox workspace-write
# detach: Ctrl-b then d
# reattach tomorrow: tmux attach -t codex
For work you do not want to babysit, use the non-interactive form, which runs the task and exits:
codex exec --sandbox workspace-write "Fix the flaky test in auth.spec.ts, run the suite, commit if green"
--sandbox workspace-write lets the agent edit files inside the workspace and run commands without asking you for each one, while leaving the rest of the filesystem alone. Without it, every command waits for approval that nobody is there to give, which is a subtle way to lose a night: the agent is fine, it is just blocked on a prompt.
tmux keeps the process alive across a dropped connection. It does not survive a reboot, and it does not stop the kernel from killing the process for memory. Which brings us to the failures.
The three that actually bite
Memory
The kernel kills the largest process when RAM runs out, and the agent is often the largest process.
free -h # look at available, not free
dmesg -T | grep -i -E 'killed process|out of memory' | tail
df -h / # a full disk fails differently but just as dead
If dmesg shows an OOM kill, add swap before adding agents. A 4 GB swap file on NVMe turns a hard kill into a slow hour.
sudo fallocate -l 4G /swapfile && sudo chmod 600 /swapfile
sudo mkswap /swapfile && sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
The supervisor you forgot to write
If the work must finish, do not let a terminal own it. A transient systemd unit restarts the task on failure and outlives your SSH session.
sudo loginctl enable-linger "$USER" # lets user units run without a login session
systemd-run --user --unit=codex-task --collect \
--property=Restart=on-failure \
codex exec --sandbox workspace-write "Run the migration, then the full test suite"
journalctl --user -u codex-task -f
Secrets
An agent reads everything your user can read, and it writes command output into a transcript. Keep .env out of the repository, chmod 600 anything with a token, and never paste a key into the prompt. If the machine is a shared VPS, check ~/.ssh/authorized_keys before you go to bed; agents with shell access can add keys, and so can anyone else who gets a shell.
The bill, and the part nobody prices
A 2 vCPU / 8 GB VPS is a small monthly charge. What is not small is the upkeep: kernel updates that want a reboot, a disk that fills with node_modules and Docker layers, keys to rotate, and the morning you find the agent stopped at 02:40 because a package upgrade restarted the host. Model usage is separate and billed by the vendor, on top of the machine.
Where Cubs fits
This whole walkthrough is the managed version of that chore list. You pick a size once, the machine keeps running when your laptop closes, and pausing stops the processes while keeping the disk, so tomorrow continues where last night stopped instead of re-installing a toolchain. SSH with a key is a toggle rather than a authorized_keys edit, and Codex can be signed in by copying the credentials from your own machine instead of a scp and a chmod.