Install custom packages
There are two ways to install custom packages in the E2B Sandbox.
Create a custom sandbox
Use this option if you know beforehand what packages you need in the sandbox.
Prerequisites:
- E2B CLI
- Docker running
Custom sandbox template is a Docker image that we automatically convert to a sandbox that you can then start with our SDK.
1. Install E2B CLI
Install the E2B CLI globally on your machine with NPM.
npm i -g @e2b/cli
2. Login to E2B CLI
Before you can create a custom sandbox, you need to login to E2B CLI.
e2b auth login
2. Initialize a sandbox template
e2b template init
3. Specify the packages you need in e2b.Dockerfile
Edit the E2B Dockerfile to install the packages you need.
You need to use the e2bdev/code-interpreter:latest
base image.
e2b.Dockerfile
FROM e2bdev/code-interpreter:latest
RUN pip install cowsay
RUN npm install cowsay
4. Build the sandbox template
Run the following command to build the sandbox template.
e2b template build -c "/root/.jupyter/start-up.sh"
This will take a while, as it convert the Docker image to a sandbox which is a small VM. At the end of the process you will see the sandbox ID like this:
Running postprocessing. It can take up to few minutes.
Postprocessing finished.
✅ Building sandbox template YOUR_TEMPLATE_ID finished.
5. Start your custom sandbox
Now you can pass the template ID to the SDK to start your custom sandbox.
import { Sandbox } from '@e2b/code-interpreter'
const sbx = Sandbox.create({
template: 'YOUR_TEMPLATE_ID',
})
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
import { Sandbox } from '@e2b/code-interpreter'
const sbx = 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
import { Sandbox } from '@e2b/code-interpreter'
const sbx = 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:
import { Sandbox } from '@e2b/code-interpreter'
const sbx = Sandbox.create()
await sbx.commands.run('apt-get update && apt-get install -y curl git')