How to install packages in E2B sandbox
Quick answer
To install packages in the
E2B sandbox, run pip install commands inside the sandbox using Sandbox.run_code(). This executes installation commands securely within the sandbox environment without affecting your local system.PREREQUISITES
Python 3.8+E2B API keypip install e2b-code-interpreter
Setup
Install the e2b-code-interpreter Python package and set your E2B_API_KEY environment variable before running code in the sandbox.
pip install e2b-code-interpreter output
Collecting e2b-code-interpreter Downloading e2b_code_interpreter-1.0.0-py3-none-any.whl (15 kB) Installing collected packages: e2b-code-interpreter Successfully installed e2b-code-interpreter-1.0.0
Step by step
Use the Sandbox.run_code() method to execute pip install commands inside the sandbox. This example installs requests and verifies the installation by importing it.
import os
from e2b_code_interpreter import Sandbox
# Initialize sandbox client with API key from environment
sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"])
# Install 'requests' package inside sandbox
install_code = """
import subprocess
subprocess.run(['pip', 'install', 'requests'], check=True)
"""
install_result = sandbox.run_code(install_code)
print("Install output:", install_result.text)
# Verify installation by importing requests
verify_code = """
import requests
print(requests.__version__)
"""
verify_result = sandbox.run_code(verify_code)
print("Requests version:", verify_result.text)
# Close sandbox session
sandbox.close() output
Install output: Collecting requests\n Downloading requests-2.31.0-py3-none-any.whl (62 kB)\nInstalling collected packages: requests\nSuccessfully installed requests-2.31.0 Requests version: 2.31.0
Common variations
- Run asynchronous code with
await sandbox.run_code()in async functions. - Install multiple packages by chaining
pip installcommands or listing packages separated by spaces. - Use
sandbox.files.write()to upload requirements files and install viapip install -r requirements.txt.
import asyncio
import os
from e2b_code_interpreter import Sandbox
async def async_install():
sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"])
code = """
import subprocess
subprocess.run(['pip', 'install', 'numpy', 'pandas'], check=True)
"""
result = await sandbox.run_code(code)
print("Async install output:", result.text)
await sandbox.close()
asyncio.run(async_install()) output
Async install output: Collecting numpy\n Downloading numpy-1.26.4-cp38-cp38-manylinux_2_31_x86_64.whl (17.3 MB)\nCollecting pandas\n Downloading pandas-2.1.1-cp38-cp38-manylinux_2_31_x86_64.whl (12.5 MB)\nInstalling collected packages: numpy, pandas\nSuccessfully installed numpy-1.26.4 pandas-2.1.1
Troubleshooting
- If package installation fails, check the
install_result.textfor error messages such as missing system dependencies. - Ensure your sandbox session is active; call
sandbox.close()after finishing to free resources. - Use
subprocess.run(..., check=True)to raise exceptions on install errors for easier debugging.
Key Takeaways
- Use
Sandbox.run_code()to runpip installcommands inside the E2B sandbox securely. - You can install multiple packages or use requirements files by uploading them with
sandbox.files.write(). - Always close the sandbox session with
sandbox.close()to release resources. - Check the output text for errors if installation fails and use
check=Truein subprocess calls for error detection.