devsandbox 0.7.1

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
---
title: Shared package caches
description: New sandboxes stop downloading the same packages again.
order: 4
icon: hard-drive
tags: [caches, performance]
related: [features, parallel-agents, shared-postgres]
---

import AgentPrompt from '../../components/AgentPrompt.astro';
import AgentPromptLink from '../../components/AgentPromptLink.astro';

## The problem

Throwaway instances are only cheap if they start fast. Each new container begins with an empty package cache, so every `npm install` or `cargo build` downloads the same dependencies again, once per instance, per rebuild.

`caches` fixes that with one line: each named cache is one directory in the config dir's `shared-volumes/`, mounted into every sandbox that lists it.

<AgentPromptLink />

## Config

```toml title="devsandboxes.toml"
[template.base]
image = "node:22"
caches = ["npm"]            # one npm cache for every sandbox and instance

[sandbox.web]
extends = "base"
folder = "../web"
postCreateCommand = "npm install"

[sandbox.api]
extends = "base"
folder = "../api"
postCreateCommand = "npm install"
```

## Try it

From the config dir:

```bash
devsandbox run web                     # npm install fills shared-volumes/npm
devsandbox run web --name fix-login    # same install, served from the cache
devsandbox run api                     # the api shares it too
devsandbox exec web sh -c 'echo $npm_config_cache'   # /root/.npm
```

- `--name`: the instance name.

## How it works

- Valid names are `pnpm`, `cargo`, `npm`, `yarn`, `go` and `pip`; each sets the tool's env vars to its mount ([`caches`](/docs/config#devsandbox-extras)).
- Arrays concatenate under `extends`: a sandbox can add its own, e.g. `caches = ["cargo"]` ([templates](/docs/config#templates-and-extends)).
- `rm` and `gc` leave caches alone; delete a directory in `shared-volumes/` to start that cache over.
- Mounts target `/root/…`, so the sandbox must run as root (`node:22` does).

## Agent instructions

<AgentPrompt
  goal="Share package caches across my devsandbox sandboxes so new instances stop re-downloading dependencies."
  steps={[
    'Find which package managers my sandboxes use and add them to `caches` (valid: pnpm, cargo, npm, yarn, go, pip), in a template every sandbox `extends` if there is one.',
    'The caches mount under /root, so check each sandbox runs as root. Tell me about any that does not rather than changing its user.',
  ]}
  verify={[
    'Existing instances now drift: list them with `devsandbox -C <config dir> ps -a` and ask me before running `devsandbox -C <config dir> rebuild <instance>` on each.',
    'Check `devsandbox -C <config dir> exec <instance> env` shows the cache variables (e.g. npm_config_cache), and that <config dir>/shared-volumes/ fills up after an install.',
  ]}
/>