# Why does pip install fail when building a template? (/docs/faq/pip-install-error)

<!-- agent-signals: reading_time_min: 2 · est_tokens: 938 · updated: 2026-07-30 -->
Related: [How do I increase my concurrency limit?](/docs/faq/increase-concurrency.md), [Do paused sandboxes count toward the concurrency limit?](/docs/faq/paused-sandboxes-concurrency.md), [Is there a limit on how many templates I can create?](/docs/faq/template-limit.md), [Can I make a template public so other projects can use it?](/docs/faq/public-templates.md), [Can I run sandboxes in the EU?](/docs/faq/eu-region.md), [Why does iptables fail with 'xt_owner module missing' in a sandbox?](/docs/faq/iptables-xt-owner.md)

When building a template that installs large Python packages like PyTorch with CUDA dependencies, `pip install` can fail with one of two errors:

* **`MemoryError`**: pip tries to serialize large wheel files into memory for caching, exceeding available RAM.
* **`OSError: [Errno 28] No space left on device`**: downloaded wheels fill up the `/tmp` directory.

**Example errors**

```txt
File "pip/_vendor/cachecontrol/serialize.py", line 70, in dumps
    return b",".join([b"cc=4", msgpack.dumps(data, use_bin_type=True)])
MemoryError
```

```txt
ERROR: Could not install packages due to an OSError: [Errno 28] No space left on device
```

## Cause [#cause]

The build environment mounts `/tmp` as a [tmpfs](https://www.kernel.org/doc/html/latest/filesystems/tmpfs.html), a RAM-backed filesystem capped at \~3.9 GB. pip downloads all wheels to `/tmp/pip-*` before installing them. PyTorch with CUDA dependencies totals \~4.1 GB of downloads, which exceeds this limit.

## Solution 1: Redirect pip's temp directory to disk (recommended) [#solution-1-redirect-pips-temp-directory-to-disk-recommended]

Set the `TMPDIR` environment variable to a disk-backed path so pip downloads don't go through the RAM-backed `/tmp`. Combined with `--no-cache-dir`, this avoids both the disk space and memory issues.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```typescript  
      const template = Template()
        .runCmd('TMPDIR=/var/tmp pip install --no-cache-dir torch sentence-transformers')
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      template = (
          Template()
          .run_cmd("TMPDIR=/var/tmp pip install --no-cache-dir torch sentence-transformers")
      )
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

## Solution 2: Install CPU-only PyTorch [#solution-2-install-cpu-only-pytorch]

E2B sandboxes don't have GPUs, so there's no reason to download CUDA dependencies. Installing the CPU-only variant of PyTorch reduces the download from \~4.1 GB to \~189 MB, avoiding the `/tmp` size limit entirely.

<CodeGroup>
  <CodeBlockTabs defaultValue="JavaScript & TypeScript" groupId="javascript-typescript+python">
    <CodeBlockTabsList>
      <CodeBlockTabsTrigger value="JavaScript & TypeScript">
        JavaScript & TypeScript
      </CodeBlockTabsTrigger>

      <CodeBlockTabsTrigger value="Python">
        Python
      </CodeBlockTabsTrigger>
    </CodeBlockTabsList>

    <CodeBlockTab value="JavaScript & TypeScript">
      ```typescript  
      const template = Template()
        .runCmd('pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu')
        .runCmd('echo "torch" > /tmp/constraints.txt && pip install --no-cache-dir -c /tmp/constraints.txt sentence-transformers')
      ```
    </CodeBlockTab>

    <CodeBlockTab value="Python">
      ```python  
      template = (
          Template()
          .run_cmd("pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cpu")
          .run_cmd('echo "torch" > /tmp/constraints.txt && pip install --no-cache-dir -c /tmp/constraints.txt sentence-transformers')
      )
      ```
    </CodeBlockTab>
  </CodeBlockTabs>
</CodeGroup>

The constraints file in the second step prevents pip from replacing the CPU-only torch with the CUDA version when installing packages that depend on PyTorch.
