{"slug": "using-claude-code-on-a-server", "title": "Using Claude Code on a server", "summary": "A hands-on walkthrough by Flavio demonstrates running Anthropic's Claude Code directly on a remote Linux server — a disposable Sprites VM from Fly.io — to build a digital pet web app with a public URL, rather than running the coding agent on a local Mac and deploying afterward. The walkthrough covers creating the project folder, authenticating Claude Code via a subscription login that opened in the Mac's browser, and prompting the agent to build the app in Bun and TypeScript, with the pet gaining 10 hunger points per minute and moods shown by emoji. It also compares local, disposable Sprite VM and always-on VPS setups, noting that Sprites auto-pause when idle and need `sprite-env services` plus disk storage to keep state across restarts.", "body_md": "# Using Claude Code on a server\n\nBy [Flavio](https://flaviocopes.com/about/)\n\nTagged: [AI](https://flaviocopes.com/tags/ai/)\n\nRun Claude Code directly on a remote Linux server instead of your laptop. A hands-on walkthrough building a digital pet web app on a Sprites VM, with a public URL anyone can visit.\n\nI usually run coding agents on my Mac, then deploy the finished app to a server. In this walkthrough, we’ll flip that around and run Claude Code directly on a remote Linux machine to build a small digital pet.\n\nThe pet gets hungrier over time, and anyone with its public URL can visit the page and feed it. We’ll check that two browsers see the same pet, make sure it survives a restart, and delete the test machine once we’re done.\n\nBecause the app lives on a remote machine, visitors can keep feeding the pet while your laptop is asleep, as long as the remote app is running. Sprites can pause when idle, so we’ll also look at what happens when the process stops and how to keep the state intact.\n\n## Why a server\n\nA server is just a computer waiting to answer requests. Putting the app on a remote host lets people reach it without relying on your laptop.\n\nYour local machine can run a web server too, though it comes with a few catches:\n\n- by default, it may only be reachable on `localhost`\n- a process tied to a terminal window can stop when you close that window\n- everything disappears the moment your laptop sleeps\n\nA remote host gives our pet its own URL. Once you make that URL public, anyone can visit whenever the app is available.\n\nSprites manages idle time differently from an always-running VPS, so here is how the three setups compare:\n\n| Setup | Reachable from another device | When your laptop sleeps | Idle behavior | Process recovery | \n|---|---|---|---|---|\n| Local Mac | Only on `localhost` by default | Stops | Stays awake while Mac is awake | Manual restart if terminal closes | \n| Disposable Sprite VM | Built-in `*.sprites.app` URL (private by default, toggleable to public) | Keeps running until idle | Auto-pauses when idle; HTTP requests wake it | Needs `sprite-env services` and disk storage | \n| Always-on VPS | Public IP or your own domain | Keeps running | Runs continuously | Needs a service manager like systemd | \n\nFor this experiment, we’ll use [Sprites](https://fly.io/sprites/) as our server. You could follow along on any VPS, but a Sprite is disposable, boots in seconds, and ships with AI coding agents preinstalled.\n\nIf you haven’t set up Sprites yet, install the CLI (`curl -fsSL https://sprites.dev/install.sh | sh`), sign in with `sprite login`, and spin up a machine with `sprite create test`. The [Sprites docs](https://docs.sprites.dev/) have the full setup details.\n\nClaude Code comes preinstalled on a fresh Sprite. I’d still check `claude --version` and update it if needed, since the base image can change. Codex is installed too, so you can use that instead if you have an OpenAI subscription.\n\n## Create the project folder\n\nConnect to your Sprite (I named mine `test`) and create a folder for the project:\n\n```\nmkdir pet && cd pet\n```\n\n## Run Claude Code\n\nNow start Claude Code inside that folder:\n\n```\nclaude\n```\n\nPick a theme, then authenticate your account. I chose the subscription route here:\n\nDuring my session, the authorization page opened right in my Mac’s browser. Completing that sign-in authenticated Claude Code on the remote Sprite:\n\nIn the screenshot below, I turned on “bypass permissions” mode. Only do that with disposable data and narrowly scoped access.\n\nEven inside a sandbox, an agent can still affect any external services you authorize, and turning on a public URL exposes whatever the app serves:\n\n## Create the app\n\nNow you can write a prompt and build whatever you like. Here is the prompt I used:\n\nCreate a digital pet web server using Bun and TypeScript. The pet should:\n\n- Have a name (Pixel), hunger level (0-100), and track when it was last fed\n- Get hungrier over time (10 hunger points per minute)\n- Show different emoji moods based on hunger: happy (<20), okay (<50), hungry (<80), starving (>=80)\n- Have a simple dark-themed web UI showing the pet, hunger bar, and a feed button\n- GET / shows the pet, POST /feed feeds it and redirects back\nSave it as pet.ts, then run it and tell me how to access it.\n\nAfter a short wait, Claude Code finishes writing the code and starts the server:\n\nHere is the summary of what it built:\n\n```\nFeatures:\n- Pixel starts happy with 0 hunger\n- Hunger increases by 10 points per minute automatically\n- Click \"Feed Pixel\" to reduce hunger by 30 points\n- The mood emoji and bar color change based on hunger level:\n  - Green/happy at <20\n  - Yellow/okay at <50\n  - Orange/hungry at <80\n  - Red/starving at >=80\n```\n\nNotice that it tells me the app is running at `localhost:3000`. Here, `localhost` refers to the Sprite itself, not my laptop.\n\nOpening that address in my local browser would look for a server on my Mac instead, so we need the Sprite’s public-facing URL.\n\n## Open it from the internet\n\nBack on your local computer, grab the Sprite’s URL:\n\n```\nsprite info -s test\n```\n\nIf you open that URL in your browser right away, you’ll hit an error:\n\nOne quick note on the screenshots: they show the older `sprite url` command. The current CLI uses `sprite info` and `sprite config update`, as shown in the code blocks above and below.\n\nYour browser request reaches Sprites, but access is private by default. Because this is an access error from Sprites itself, editing the pet’s code won’t fix it.\n\nMake this demo URL public:\n\n```\nsprite config update -s test --url-auth public\n```\n\nReload the page, and Pixel shows up:\n\nIf the page still doesn’t load for you, check that your server listens on port **8080**. That is the default port a Sprite URL forwards traffic to.\n\nGo ahead and feed Pixel:\n\nIf you come back after a while, you’ll see Pixel getting hungry again:\n\n### Testing across devices and sleep behavior\n\nTry opening the same URL on your phone while keeping it open on your computer. When you feed Pixel on one device and reload the other, both should show the updated hunger level because they talk to the same server process.\n\nSeeing a separate pet on each device would be a bug in this setup.\n\nYou can even close your laptop lid and keep using the app on your phone. The active process on the Sprite keeps running on its own.\n\nWhat happens when nobody visits for a while? Sprites can pause when idle.\n\nA warm pause freezes running processes in place, while a cold wake boots back up without those processes.\n\nAn incoming HTTP request will wake the Sprite, but a server you started manually in the terminal may need to be started again. To handle restarts automatically, we can use the built-in `sprite-env services` manager (we’ll set that up in a moment).\n\nYou also need to save the pet’s state to disk so it survives a restart. A regular VPS has the same requirement and still needs a service manager to recover after a crash or reboot.\n\nIf you plan to keep this app running, test a restart on its own: feed Pixel, restart the process, and reload the page. If the hunger level resets back to zero, the app isn’t restoring its state from disk.\n\nA public URL alone does not give you persistence.\n\n## Keep the same pet after a restart\n\nThe original prompt leaves storage up to the agent, so it might only keep Pixel’s state in memory. Here is a complete version you can compare against what the agent wrote, saved as `pet.ts` inside `/home/sprite/pet`.\n\nInstead of relying on an in-memory timer, this version saves Pixel’s hunger and last update timestamp in SQLite and calculates elapsed time on every request. That also solves the sleeping Sprite problem, since an interval running in memory stops ticking while the machine is asleep:\n\n``` js\nimport { Database } from 'bun:sqlite'\n\nconst db = new Database('pet.sqlite', { create: true })\ndb.exec(`CREATE TABLE IF NOT EXISTS pet (\n  id INTEGER PRIMARY KEY CHECK (id = 1),\n  hunger REAL NOT NULL,\n  updated INTEGER NOT NULL\n)`)\ndb.query('INSERT OR IGNORE INTO pet VALUES (1, 0, ?)').run(Date.now())\nconst read = db.query<{ hunger: number; updated: number }, []>('SELECT hunger, updated FROM pet WHERE id = 1')\nconst hungerNow = (row: { hunger: number; updated: number }) =>\n  Math.min(100, Math.max(0, row.hunger + (Date.now() - row.updated) / 6000))\nconst feed = db.transaction(() => {\n  const hunger = Math.max(0, hungerNow(read.get()!) - 30)\n  db.query('UPDATE pet SET hunger = ?, updated = ? WHERE id = 1').run(hunger, Date.now())\n})\n\nBun.serve({\n  hostname: '0.0.0.0',\n  port: Number(process.env.PORT ?? 8080),\n  fetch(request) {\n    const url = new URL(request.url)\n    if (url.pathname === '/feed' && request.method === 'POST') {\n      feed()\n      return new Response(null, { status: 303, headers: { Location: '/' } })\n    }\n    if (url.pathname !== '/') return new Response('Not found', { status: 404 })\n    if (request.method !== 'GET') return new Response('Method not allowed', { status: 405 })\n    const hunger = Math.floor(hungerNow(read.get()!))\n    const mood = hunger < 20 ? 'happy' : hunger < 50 ? 'okay' : hunger < 80 ? 'hungry' : 'starving'\n    const emoji = hunger < 20 ? '😄' : hunger < 50 ? '🙂' : hunger < 80 ? '😟' : '😫'\n    return new Response(`<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <title>Pixel the pet</title>\n  <style>\n    body { max-width: 32rem; margin: 4rem auto; padding: 1rem; font-family: system-ui; background: #161616; color: #eee }\n    progress { width: 100% }\n    button { margin-top: 1rem; padding: 0.75rem; font: inherit }\n  </style>\n</head>\n<body>\n  <h1>Pixel ${emoji}</h1>\n  <p>Hunger: ${hunger}/100 (${mood})</p>\n  <progress value=\"${hunger}\" max=\"100\" aria-label=\"Hunger\"></progress>\n  <form action=\"/feed\" method=\"post\"><button>Feed Pixel</button></form>\n  <p>Reload to see how hungry Pixel is now.</p>\n</body>\n</html>`, { headers: { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-store' } })\n  },\n})\n```\n\nUsing a database transaction keeps feeding as a single read-and-write operation, and both browsers read from the same database. Feeding lowers hunger by 30 points (without going below zero), while passing time adds 10 points per minute up to a maximum of 100.\n\nRun this version from the project folder:\n\n```\ncd /home/sprite/pet\nbun run pet.ts\n```\n\nThis server listens on port 8080 out of the box. Feed Pixel, wait a minute, and reload the page, and you should see hunger climb by about 10 points.\n\nIf you open a second browser, feed Pixel there, and reload the first browser, both will show the same state (allowing for the few seconds between requests).\n\nNext, stop the server with Ctrl-C and start it again from the same folder. Hunger should pick up right where it left off.\n\nThe database lives in `pet.sqlite` inside that folder, so starting the script from another working directory would create a different pet.\n\n### Running it as a service\n\nBefore registering a background service, stop your manual process so both don’t fight over port 8080. A Sprite can only run one HTTP service at a time.\n\nIf you followed the earlier guide and still have its `web` test service registered, delete it first with `sprite-env services delete web`. Then run this command inside the Sprite:\n\n```\nsprite-env services create pet \\\n  --cmd bun \\\n  --args \"run,pet.ts\" \\\n  --dir /home/sprite/pet \\\n  --http-port 8080\n```\n\nNow the service manager handles restarts whenever the Sprite needs them. Setting `--dir` to a fixed working directory keeps the process attached to the same SQLite file every time.\n\nNext, restart the service and check its log to confirm it came back cleanly:\n\n```\nsprite-env services restart pet\ntail -n 30 /.sprite/logs/services/pet.log\n```\n\nReload both browsers one more time and check the hunger value too, not just whether the page loads. A service can restart cleanly while opening the wrong database.\n\nAnd remember, this is a public toy, so anyone who can reach the URL can feed the same pet.\n\n## Destroy the server\n\nSince this was only a test, destroy the Sprite once you’re done so you aren’t paying for its storage:\n\n```\nsprite destroy -s test\n```\n\nThere is another workflow I use too: keeping the coding agent on my Mac and letting it inspect a remote server. I prefer that approach for maintenance work, where existing data and services need to survive the work.\n\nWant me to talk about your product? You can [sponsor this site](https://flaviocopes.com/sponsor/).", "url": "https://wpnews.pro/news/using-claude-code-on-a-server", "canonical_source": "https://flaviocopes.com/claude-code-on-a-server/", "published_at": "2026-11-29 14:00:00+00:00", "updated_at": "2026-11-29 14:42:56.202767+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools"], "entities": ["Claude Code", "Anthropic", "Sprites", "Fly.io", "Flavio", "Bun", "TypeScript", "Codex"], "also_reported_by": [], "takeaway": "Running Claude Code on a disposable Sprites VM keeps the app reachable via a public *.sprites.app URL while your laptop sleeps, but because Sprites auto-pause when idle, persistence requires `sprite-env services` and disk storage.", "recommendation": {"advice": "Run the coding agent on a disposable remote VM with a public URL so the app stays reachable, but enable `sprite-env services` and disk storage to survive idle pauses and restarts.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/using-claude-code-on-a-server", "markdown": "https://wpnews.pro/news/using-claude-code-on-a-server.md", "text": "https://wpnews.pro/news/using-claude-code-on-a-server.txt", "jsonld": "https://wpnews.pro/news/using-claude-code-on-a-server.jsonld"}}