How to Run Claude Code on a VPS (DigitalOcean, Hetzner, EC2)

How to provision a VPS, install Claude Code, add basic security hardening, and keep sessions running persistently with tmux. Covers DigitalOcean, Hetzner, and AWS EC2.

Running Claude Code on a VPS gives you a persistent remote coding session that runs independently of your laptop. Kick off a task, detach from the terminal, and come back hours later. This guide covers provisioning, Node.js setup, Claude Code installation, basic security hardening, and session persistence via tmux — on DigitalOcean, Hetzner, or AWS EC2.

TL;DR

  • Minimum VPS: 1 vCPU, 1GB RAM (~$6/month on DigitalOcean or Hetzner)
  • Install Node.js 18+ via nvm, then npm install -g @anthropic-ai/claude-code
  • Add a non-root user, enable ufw firewall (SSH only)
  • Wrap every session in tmux — Claude Code exits on SSH disconnect without it
  • Optional: install Grass for phone-based monitoring without SSH

What size VPS do you need?

Claude Code is not CPU-intensive. It spends the majority of its execution time waiting on API responses from Anthropic — compute is not the bottleneck.

Provider Instance RAM Cost Notes
DigitalOcean Basic Droplet 1GB $6/month Minimum viable
Hetzner CX11 2GB ~€4/month Better value than DO for EU regions
AWS EC2 t3.micro 1GB ~$8/month 1GB is marginal for multiple sessions
AWS EC2 t3.small 2GB ~$16/month Safer if running parallel sessions

For single Claude Code sessions: 1GB RAM is sufficient. For multiple parallel sessions: 2GB+ recommended. Storage: transcript files (.jsonl) are small; 20GB is plenty.

Step 1: Provision the VPS

DigitalOcean: Create a Droplet — Ubuntu 22.04 LTS, Basic, 1GB RAM. Add your SSH key during creation.

Hetzner: Create a Server — Ubuntu 22.04, CX11. Hetzner's cheapest tier includes 2GB RAM, which makes it better value than DigitalOcean's $6 tier for this use case.

AWS EC2: Launch an instance — Ubuntu 22.04 LTS AMI, t3.small (2GB RAM recommended). Configure the security group to allow SSH (port 22) from your IP only during initial setup.

Step 2: SSH in

ssh root@your-server-ip

On DigitalOcean and Hetzner you typically get root access initially. On EC2, the default user is ubuntu.

Step 3: Create a non-root user

Running everything as root is a bad idea. Create a user and grant sudo:

adduser dev
usermod -aG sudo dev
# Copy SSH key to new user:
mkdir -p /home/dev/.ssh
cp ~/.ssh/authorized_keys /home/dev/.ssh/
chown -R dev:dev /home/dev/.ssh
chmod 700 /home/dev/.ssh
chmod 600 /home/dev/.ssh/authorized_keys

Now SSH in as the new user:

ssh dev@your-server-ip

Step 4: Basic firewall setup

Allow SSH only; block everything else:

sudo ufw allow OpenSSH
sudo ufw enable

Verify: sudo ufw status should show OpenSSH allowed.

If you plan to use grass start --network remote-ip later (for phone access), you'll add a rule for the Grass port range. For now, keep the firewall tight.

Step 5: Install Node.js 18+ via nvm

Claude Code requires Node.js 18 or higher. Install via nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install --lts
node --version  # should be v18.x+

Step 6: Install Claude Code

npm install -g @anthropic-ai/claude-code
claude --version

Step 7: Set the Anthropic API key

echo 'export ANTHROPIC_API_KEY="sk-ant-your-key-here"' >> ~/.bashrc
source ~/.bashrc

Verify: echo $ANTHROPIC_API_KEY

Step 8: Clone your project and start tmux

git clone https://github.com/your-org/your-repo
cd your-repo
tmux new -s project-name

Naming sessions by project keeps things organized when you're running multiple agents simultaneously.

Step 9: Start Claude Code inside tmux

claude

Interact with Claude Code normally. When you want to step away:

# Ctrl+B then D — detach without stopping

Close your SSH connection. Claude Code keeps running on the VPS inside tmux.

Step 10: Reconnect

When you want to check in:

ssh dev@your-server-ip
tmux attach -t project-name

If Claude Code exited while you were away:

claude --continue

claude --continue restores the session from the transcript at ~/.claude/projects/<cwd>/. The Claude Code CLI reference documents all session flags.

Can I run multiple Claude Code sessions on one VPS?

Yes. Each session gets its own tmux session:

tmux new -s project-a   # in one terminal
tmux new -s project-b   # in another
tmux ls                 # see all running sessions

Memory is the constraint, not CPU. With 1GB RAM, you can typically run 1–2 sessions before you start seeing issues. With 2GB: 3–4 sessions. Claude Code itself is lightweight; the limiting factor is OS overhead and any tools the agent runs (npm installs, builds, etc.).

Going further: monitoring from your phone

Your agent is running on the VPS with no terminal required. To monitor output, handle agent approval gates, and send instructions from your phone:

npm install -g @grass-ai/ide

In a second tmux window alongside your Claude Code session:

grass start --network tailscale
# or: grass start --network remote-ip

Scan the QR code from the Grass iOS or Android app.

Security note for --network remote-ip: This exposes the Grass port (range 32100–32199) on the server's public IP. Add a ufw rule to restrict access:

sudo ufw allow from your.phone.ip.address to any port 32100:32199 proto tcp

Or use --network tailscale to keep the connection entirely private — install Tailscale on both the VPS and your phone, connect them to the same tailnet, and Grass connects over the private network with no public ports exposed.

For mobile coding agent access without a terminal: install Grass, run grass start, scan once. From that point the Grass app is the interface.

Frequently asked questions

What size VPS do I need to run Claude Code?

Minimum: 1 vCPU, 1GB RAM (DigitalOcean Basic Droplet at $6/month or Hetzner CX11 at ~€4/month). Claude Code waits on API responses most of the time, so CPU is rarely the bottleneck. 2GB RAM is recommended if you're running multiple concurrent sessions.

How do I run Claude Code on DigitalOcean?

Create a Droplet with Ubuntu 22.04 LTS, add your SSH key, SSH in, create a non-root user, set up ufw, install Node.js 18+ via nvm, install Claude Code with npm install -g @anthropic-ai/claude-code, and wrap every session in tmux.

How do I run Claude Code on Hetzner?

Same process as DigitalOcean. Create a CX11 server (Ubuntu 22.04), SSH in as root, create a non-root user, configure ufw, and follow the same Node.js + Claude Code install steps. Hetzner's CX11 includes 2GB RAM at a lower price point than DigitalOcean's 1GB tier.

How do I run Claude Code on AWS EC2?

Launch a t3.small (Ubuntu 22.04, 2GB RAM — t3.micro at 1GB is tight for multiple sessions), configure the security group to allow SSH from your IP, SSH in as ubuntu, and follow the same setup steps. EC2 costs slightly more than DigitalOcean or Hetzner for equivalent specs.

How do I secure a VPS running Claude Code?

Create a non-root user, disable root SSH login, enable ufw (allow SSH only), and use SSH key authentication. If using Grass for mobile access, restrict the Grass port range (32100–32199) to your phone's IP via ufw, or use Tailscale to keep it off the public internet entirely.

Can I run multiple Claude Code sessions on one VPS?

Yes. Each session gets its own tmux session. Memory is the constraint — 1GB RAM supports 1–2 sessions; 2GB supports 3–4. Use tmux ls to see all running sessions.

How do I keep Claude Code running on a VPS after I disconnect?

Wrap every session in tmux before starting Claude Code. Run tmux new -s name, start claude inside, then detach with Ctrl+B D. The process keeps running on the VPS after SSH disconnect. Reconnect with tmux attach -t name.