Why run Claude Code on a server
A server removes the laptop from the loop: the session keeps running when your computer sleeps, closes, or loses Wi-Fi, and you can check on it from anywhere.
Claude Code is a terminal program. It runs on the machine where you start it, so when that machine goes to sleep or shuts down, the task stops with it. Putting it on an always-on Linux box fixes three things at once:
- Tasks don't get interrupted. Long refactors, test loops, and migrations finish while you are away.
- You can reach it from a phone or any other computer. The session lives on the server, not on one device.
- Your computer can be off. Nothing depends on a laptop lid staying open.
Anthropic's hosted options have limits. Per its documentation, Remote Control sessions run on your machine, so "your computer has to stay on." Cloud sessions (Claude Code on the web) require a claude.ai account and don't accept API keys or third-party providers. A server you control has neither restriction.
Pick the right server
Choose a regular Linux VPS with at least 4 GB of RAM, 2 vCPUs, and enough disk for your repositories; anything smaller will make you fight the installer.
Anthropic's system requirements list Ubuntu 20.04+, Debian 10+, or Alpine 3.19+, a 4 GB+ RAM machine with an x64 or ARM64 processor, and an internet connection. Beyond that, size the machine for your builds, test suites, and containers rather than for Claude Code itself.
| Workload | Starting point |
|---|---|
| Scripts, small web apps, light refactors | 2 vCPU, 4 GB RAM, 40 GB disk |
| Full-stack projects with a database or Docker | 4 vCPU, 8 GB RAM, 75 GB+ disk |
| Heavy builds, several parallel sessions | 6+ vCPU, 12 GB+ RAM, 100 GB+ disk |
Practical advice for choosing a provider:
- Use a mainstream distribution. Ubuntu LTS or Debian match the commands in this guide.
- Check the region. Claude Code is only available in Anthropic's supported countries, and a server in an unsupported location can fail at sign-in or return 403 errors.
- Mind hourly versus monthly billing. Some providers keep charging for a powered-off server. Check before you "stop" an instance to save money.
Create a non-root user first
Do all your work as a normal user with sudo rights, not as root, so a mistake or a bad command has a smaller blast radius.
Log in once as root (or the default user your provider created), then run the following. Replace dev with any username.
# Create the user (Debian/Ubuntu)
adduser dev
usermod -aG sudo dev
Then, from your own computer, copy your SSH public key to the new user so you can log in without a password:
ssh-copy-id dev@YOUR_SERVER_IP
ssh dev@YOUR_SERVER_IP
If you have no key pair yet, run ssh-keygen -t ed25519 first and keep the private key on your machine only.
Install Claude Code
Use Anthropic's native installer; it needs no Node.js, installs to your home directory, and updates itself in the background.
curl -fsSL https://claude.ai/install.sh | bash
claude --version
The installer shows no progress while it downloads. Afterwards, claude --version should print a version number, and claude doctor runs a read-only health check.
Two alternatives from the official setup guide:
- apt, dnf, or apk. Anthropic publishes signed repositories; updates come through normal system upgrades.
- npm.
npm install -g @anthropic-ai/claude-codeworks and needs Node.js 22 or later. Anthropic says not to usesudofor it. It installs the same native binary, so you only need Node for your own projects.
Sign in: subscription or API key
You can authenticate with a claude.ai subscription or with an Anthropic Console API key; pick based on how you want to pay.
Option A: subscription login
Run claude and follow the prompts. On a server there is no browser, so copy the login URL (press c in the prompt to copy it, or select it), open it in the browser on your own computer, sign in, and paste the code it shows back into the terminal at the Paste code here prompt. If pasting into the interactive prompt does nothing, Anthropic suggests running claude auth login instead, which reads the code from standard input.
Claude Code requires a Pro, Max, Ultra, Team, Enterprise, or Console account; the free claude.ai plan does not include it. For unattended scripts, claude setup-token generates a one-year OAuth token for CLAUDE_CODE_OAUTH_TOKEN; Anthropic notes it can't establish Remote Control sessions.
Option B: API key
Set ANTHROPIC_API_KEY to a key from the Claude Console and Claude Code bills you per token instead of using your subscription. In interactive mode you approve the key once. Anthropic's docs state the key is used instead of your subscription "even if you are logged in," so run unset ANTHROPIC_API_KEY to go back. The cleanest place to store it is the env block of ~/.claude/settings.json:
{
"env": {
"ANTHROPIC_API_KEY": "YOUR_API_KEY"
}
}
Never put credentials in a project's committed .claude/settings.json; Anthropic warns that file is shared with everyone who clones the repository. Run /status inside Claude Code to confirm which credential is active. For gateways, relays, and OpenRouter, see our guide on using Claude Code with your own API key.
Keep sessions alive with tmux
Run Claude Code inside tmux, because a plain SSH session kills your process the moment the connection drops.
tmux keeps terminals running on the server and lets you detach and reattach from any device. Anthropic's own Remote Control docs give the same advice: if you run claude from a terminal on a remote machine, start it inside tmux or screen so it survives an SSH disconnect.
sudo apt install tmux # Debian/Ubuntu
tmux new -s work # create a named session and attach
claude # start Claude Code inside it
# press Ctrl+b, then d, to detach
tmux ls # list sessions
tmux attach -t work # reattach later
The default prefix key is Ctrl+b: press it, release, then press the next key. tmux new -A -s work attaches if the session exists and creates it if not, handy as one command from a phone.
tmux survives disconnects but not a server reboot; after a restart, just run tmux new -A -s work again.
Use it from your phone
There are three realistic ways to reach a server-hosted session from a phone: SSH through a terminal app, Anthropic's Remote Control, or a browser workspace. Each trades convenience against setup effort.
SSH app plus tmux
Install an SSH client on your phone, add your private key, connect to the server, and run tmux new -A -s work. Termux (Android) and Blink Shell or Prompt (iOS) are commonly used. You get the full CLI, but typing is slow, so keep prompts short and lean on the approve-or-reject flow.
Tailscale for private access
Tailscale creates a private network between your devices, so you don't have to expose SSH to the public internet at all. Install it on the server and on your phone, sign in to the same account, and connect using the server's Tailscale name.
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up
Tailscale also offers Tailscale SSH, enabled with tailscale set --ssh. Connections over your tailnet are authenticated by Tailscale instead of SSH keys, and it leaves your regular sshd configuration untouched. It requires an access-control policy that allows SSH to the machine; the default policy covers your own devices. Once it works, you can close port 22 to the public internet.
Remote Control
Anthropic's Remote Control lets the Claude mobile app or claude.ai/code drive a session running on your server. Start it with claude remote-control (server mode) inside tmux, then connect from the app using the URL or QR code it prints. It only makes outbound HTTPS requests and never opens inbound ports.
The limits matter: it requires a claude.ai subscription login (API keys are not supported), it is disabled if ANTHROPIC_BASE_URL points anywhere other than api.anthropic.com, and the claude process must stay running. If you pay per token or use a gateway, use SSH or a browser workspace instead.
Lock the server down
A fresh VPS is scanned by bots within minutes, so the minimum is key-only SSH, a firewall, fail2ban, and a non-root user.
- Keys only. After confirming key login works in a second terminal, disable passwords and root login in
/etc/ssh/sshd_config:Check the config with/etc/ssh/sshd_configPasswordAuthentication no KbdInteractiveAuthentication no PermitRootLogin nosudo sshd -t, then restart the SSH service (the unit issshon Debian/Ubuntu,sshdon many other distributions). Some distributions ship drop-in files that override the main file, so verify the effective values withsudo sshd -T | grep -i -E "passwordauth|permitroot". Keep your current session open until a new login works. - Firewall. On Ubuntu, ufw is disabled by default but defaults to denying incoming traffic once enabled. Allow SSH first, or you will lock yourself out:
Open 80 and 443 only if you serve a website.ufw
sudo ufw allow 22/tcp sudo ufw enable sudo ufw status - fail2ban. It scans logs such as
/var/log/auth.logfor IPs with repeated failed logins and blocks them for a period. Install withsudo apt install fail2banand check its jails withsudo fail2ban-client status. - Don't run as root. Use your normal user for Claude Code. Review permission prompts instead of blanket-approving commands.
- Protect secrets. Keep API keys in
~/.claude/settings.jsonor environment files readable only by you (chmod 600), never in git. Claude Code stores subscription credentials in~/.claude/.credentials.jsonwith mode 0600 on Linux. Use a deploy key or scoped token for git. - Keep it updated. Native installs update Claude Code automatically; for the OS, enable unattended security updates (
unattended-upgradeson Debian/Ubuntu).
Estimate the cost
Your total is the server rental plus model usage, and they scale differently: the server is a flat rate, while model usage depends on how much you run.
- Server. Multiply the hourly or monthly price by how long it exists, not by how long you use it. Some providers bill powered-off servers, so check.
- Model usage. With a subscription, usage counts against your plan's limits. With an API key, you pay per token, so heavy autonomous runs can cost more than the server itself. Snapshots, extra disk, and bandwidth can add to the bill.
To estimate, run your real workflow for a week, record the server and model bills separately, and extrapolate.
Troubleshooting
Most problems on a new server are PATH, memory, or login issues, and each has a documented fix.
claude: command not foundafter install. The installer places the binary at~/.local/bin/claude. Add it to your PATH and reload your shell:echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc.Killedduring install (exit code 137). The server ran out of memory. Add swap (Anthropic's guide shows a 2 GB swap file withfallocate,mkswap, andswapon) or move to a larger plan, then rerun the installer.- Install script returns HTML or a 403. Usually a proxy, a network filter, or an unsupported region. Set
HTTPS_PROXYif you are behind a proxy. - OAuth error: Invalid code. The code expired or was truncated. Retry quickly and copy the whole URL with
c. - Login expired or auth errors. Run
/loginagain, check the server clock, and use/statusto see whetherANTHROPIC_API_KEYis overriding your subscription. - Session gone after reconnecting. You ran
claudeoutside tmux; start it inside a tmux session next time. - Remote Control won't start. Check the login and base URL limits above, then run
claude remote-control --verbose.
Skip the setup: use AlwaysAgent
If you want the outcome without the server administration, AlwaysAgent gives you the same thing, a dedicated Linux server with Claude Code already installed, behind a browser tab.
You get one dedicated US Linux server with full root access, Claude Code and OpenAI Codex preinstalled, and a browser workspace with chat, files, terminal, and preview. No SSH client or app is needed, so a phone browser works, and tasks keep running when you close the tab.
- Bring your own access. Use your subscription login, an Anthropic API key, OpenRouter, or a relay. Credentials stay on your server, and AlwaysAgent doesn't proxy model calls or add token markup.
- Simple pricing. Hourly use is about $0.05 per hour after a $10 top-up, with no auto-renewal. Monthly plans start at $9.9 for 2 vCPU, 4 GB RAM, and 40 GB NVMe, and you can upgrade from hourly to monthly while keeping your files.
- You still own the machine. It has root, Docker, its own public IP, and can serve websites on ports 80 and 443.
Hourly billing continues while the server exists, even when powered off, until you delete it. AlwaysAgent is an independent product and is not affiliated with Anthropic or OpenAI. If you would rather self-host, everything above works on any VPS. If you would rather start in a minute, compare the options and try it.
FAQ
Can I run Claude Code on a VPS 24/7?
How much RAM does Claude Code need on a server?
Can I use Claude Code from my phone if it runs on a server?
Does Claude Code on a VPS work with an API key?
Do I need Node.js to run Claude Code?
How do I sign in to Claude Code on a server with no browser?
Is it safe to run Claude Code on a public server?
Is a VPS cheaper than a managed option?
Skip the server admin
AlwaysAgent gives you a dedicated Linux server with Claude Code and Codex preinstalled, ready in a browser tab. No SSH, no hardening checklist.
Try it for $0.05/h →