How to run Claude Code on a VPS (24/7, from your phone)

A small Linux server, the official installer, and tmux are enough to keep Claude Code working after you close your laptop. Here is the whole setup, including phone access and security.

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.

WorkloadStarting point
Scripts, small web apps, light refactors2 vCPU, 4 GB RAM, 40 GB disk
Full-stack projects with a database or Docker4 vCPU, 8 GB RAM, 75 GB+ disk
Heavy builds, several parallel sessions6+ 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.

On the server, as root
# 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:

On your computer
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.

On the server, as your normal user
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-code works and needs Node.js 22 or later. Anthropic says not to use sudo for 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:

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

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

On the server
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.

  1. Keys only. After confirming key login works in a second terminal, disable passwords and root login in /etc/ssh/sshd_config:
    /etc/ssh/sshd_config
    PasswordAuthentication no
    KbdInteractiveAuthentication no
    PermitRootLogin no
    Check the config with sudo sshd -t, then restart the SSH service (the unit is ssh on Debian/Ubuntu, sshd on many other distributions). Some distributions ship drop-in files that override the main file, so verify the effective values with sudo sshd -T | grep -i -E "passwordauth|permitroot". Keep your current session open until a new login works.
  2. 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:
    ufw
    sudo ufw allow 22/tcp
    sudo ufw enable
    sudo ufw status
    Open 80 and 443 only if you serve a website.
  3. fail2ban. It scans logs such as /var/log/auth.log for IPs with repeated failed logins and blocks them for a period. Install with sudo apt install fail2ban and check its jails with sudo fail2ban-client status.
  4. Don't run as root. Use your normal user for Claude Code. Review permission prompts instead of blanket-approving commands.
  5. Protect secrets. Keep API keys in ~/.claude/settings.json or environment files readable only by you (chmod 600), never in git. Claude Code stores subscription credentials in ~/.claude/.credentials.json with mode 0600 on Linux. Use a deploy key or scoped token for git.
  6. Keep it updated. Native installs update Claude Code automatically; for the OS, enable unattended security updates (unattended-upgrades on 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 found after 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.
  • Killed during install (exit code 137). The server ran out of memory. Add swap (Anthropic's guide shows a 2 GB swap file with fallocate, mkswap, and swapon) 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_PROXY if 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 /login again, check the server clock, and use /status to see whether ANTHROPIC_API_KEY is overriding your subscription.
  • Session gone after reconnecting. You ran claude outside 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?
Yes. Install it on an always-on Linux server, start it inside tmux, and the session keeps running after you disconnect. Claude Code itself stays idle while waiting for you, so the cost is the server plus whatever model usage you trigger.
How much RAM does Claude Code need on a server?
Anthropic's system requirements list 4 GB or more of RAM. The installer needs about 512 MB of free memory, and small instances can kill it, so add swap or pick a bigger plan. Size beyond that for your own builds and containers.
Can I use Claude Code from my phone if it runs on a server?
Yes. Connect with an SSH app and attach to tmux, use Tailscale for private access, or use Anthropic's Remote Control, which requires a claude.ai subscription login. A browser workspace like AlwaysAgent also works from a phone without any app.
Does Claude Code on a VPS work with an API key?
Yes. Set ANTHROPIC_API_KEY, preferably in the env block of ~/.claude/settings.json. Anthropic's cloud sessions and Remote Control don't support API keys, which is a main reason to use your own server.
Do I need Node.js to run Claude Code?
Not with the native installer. The npm package needs Node.js 22 or later, but it installs the same native binary, so you only need Node for your own projects.
How do I sign in to Claude Code on a server with no browser?
Run claude, copy the login URL (press c), open it in a browser on your own computer, sign in, and paste the code back into the terminal. If paste fails, use claude auth login. For unattended jobs, claude setup-token creates a long-lived token.
Is it safe to run Claude Code on a public server?
It can be, if you harden it: key-only SSH, no root login, a firewall, fail2ban, secrets stored with strict permissions, and a non-root user. Putting SSH behind Tailscale removes public exposure entirely.
Is a VPS cheaper than a managed option?
It depends on usage. A VPS is a flat rate that bills while it exists. Hourly managed servers can be cheaper if you only need one occasionally, and monthly plans suit constant use. Compare the real monthly total, not the headline rate.

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 →