There are two ways to install custom packages in the E2B Sandbox.
Create custom sandbox with preinstalled packages .
Install packages during the sandbox runtime .
Create a custom sandbox
Use this option if you know beforehand what packages you need in the sandbox.
Sandbox templates allow you to define custom sandboxes with preinstalled packages and configurations.
1. Install the E2B SDK
JavaScript & TypeScript
Python
Create a .env file with your API key:
2. Create a template file
Define your custom template with the packages you need.
JavaScript & TypeScript
Python
// template.ts
import { Template } from "e2b" ;
export const template = Template ()
. fromTemplate ( "code-interpreter-v1" )
. pipInstall ([ 'cowsay' ]) // Install Python packages
. npmInstall ([ 'cowsay' ]); // Install Node.js packages
3. Create a build script
JavaScript & TypeScript
Python
// build.prod.ts
import "dotenv/config" ;
import { Template, defaultBuildLogger } from "e2b" ;
import { template } from "./template" ;
async function main () {
await Template. build (template, {
alias: "custom-packages" ,
cpuCount: 2 ,
memoryMB: 2048 ,
onBuildLogs: defaultBuildLogger (),
});
}
main (). catch (console.error);
4. Build the template
Run the build script to create your custom template:
JavaScript & TypeScript
Python
This will build your template and you’ll see build logs in the console.
5. Use your custom sandbox
Now you can create sandboxes from your custom template:
JavaScript & TypeScript
Python
import { Sandbox } from 'e2b'
const sbx = await Sandbox. create ( "custom-packages" )
// Your packages are already installed and ready to use
Install packages during the sandbox runtime
Use this option if don’t know beforehand what packages you need in the sandbox. You can install packages with the package manager of your choice.
The packages installed during the runtime are available only in the running sandbox instance.
When you start a new sandbox instance, the packages are not be available.
1. Install Python packages with PIP
JavaScript & TypeScript
Python
import { Sandbox } from '@e2b/code-interpreter'
const sbx = await Sandbox. create ()
sbx.commands. run ( 'pip install cowsay' ) // This will install the cowsay package
sbx. runCode ( `
import cowsay
cowsay.cow("Hello, world!")
` )
2. Install Node.js packages with NPM
JavaScript & TypeScript
Python
import { Sandbox } from '@e2b/code-interpreter'
const sbx = await Sandbox. create ()
sbx.commands. run ( 'npm install cowsay' ) // This will install the cowsay package
sbx. runCode ( `
const cowsay = require('cowsay')
console.log(cowsay.say({ text: 'Hello, world!' }))
` , { language: 'javascript' })
3. Install packages with package manager of your choice
Since E2B Sandboxes are Debian based machines, you can use any package manager supported by Debian.
You just need to make sure that the package manager is already installed in the sandbox.
For example, to install curl and git, you can use the following commands:
JavaScript & TypeScript
Python
import { Sandbox } from '@e2b/code-interpreter'
const sbx = await Sandbox. create ()
await sbx.commands. run ( 'apt-get update && apt-get install -y curl git' )