---
title: "Gemini CLI integration"
description: "Run Google's Gemini CLI headless in an E2B sandbox with a filesystem, terminal and git. Starts from the prebuilt gemini template."
url: "https://e2b.dev/integrations/gemini-cli"
---

# Gemini CLI

> Run Google's Gemini CLI in a sandbox from the prebuilt gemini template. Give it a Gemini API key and a task, and it works on the sandbox's files, terminal and git.

Coding agents · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** E2B
- **How it works:** Prebuilt gemini sandbox template
- **Package:** e2b
- **Language:** TypeScript, Python
- **Upstream:** [google-gemini/gemini-cli · ★ 109k](https://github.com/google-gemini/gemini-cli)
- **Example:** [e2b-cookbook/gemini-cli-in-sandbox-js](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/gemini-cli-in-sandbox-js)

## Get started

Paste the prompt into your coding agent, or follow the steps yourself.

### Paste this into your coding agent

```text
Run Gemini CLI in an E2B sandbox from this project.

1. Install the e2b SDK using the project's package manager.
2. The SDK needs the E2B_API_KEY environment variable, and Gemini CLI needs GEMINI_API_KEY. If E2B_API_KEY isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode either key.
3. Follow the Gemini CLI guide at https://docs.e2b.dev/agents/gemini-cli: create a sandbox from the gemini template, pass GEMINI_API_KEY in envs, and run gemini --skip-trust --yolo -p with a command timeout of 0. Kill the sandbox when it's done.
4. Run one task and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Install the E2B SDK

```bash
npm init -y && npm pkg set type=module
npm install e2b
npm install -D tsx
```

### 2. Set your API keys

```bash
export E2B_API_KEY="e2b_..."
export GEMINI_API_KEY="..."
```

### 3. Run Gemini CLI headless

```typescript
import { Sandbox } from 'e2b'

const sandbox = await Sandbox.create('gemini', {
  envs: { GEMINI_API_KEY: process.env.GEMINI_API_KEY! },
  timeoutMs: 600_000,
})

const result = await sandbox.commands.run(
  `gemini --skip-trust --yolo -p "Create a hello world HTTP server in Python"`,
  { timeoutMs: 0 }
)

console.log(result.stdout)
await sandbox.kill()
```

### 4. Work on a repository

```typescript
await sandbox.commands.run(
  'git clone --depth 1 https://x-access-token:$GITHUB_TOKEN@github.com/<owner>/<repo>.git /home/user/repo',
  { envs: { GITHUB_TOKEN: process.env.GITHUB_TOKEN! } }
)

await sandbox.commands.run(
  `cd /home/user/repo && gemini --skip-trust --yolo -p "Add error handling to all API endpoints"`,
  { timeoutMs: 0 }
)

const diff = await sandbox.commands.run('cd /home/user/repo && git diff')
console.log(diff.stdout)
```

## What runs in the sandbox

Gemini CLI and every command it runs live in the sandbox, away from your machine.

- **Agent:** gemini with --yolo auto-approving tool calls and --skip-trust trusting the working folder for the run.
- **Files:** The repo you clone and every change Gemini CLI makes. Read the result back with git diff.
- **Sessions:** Sessions are saved per folder under ~/.gemini. --resume latest continues the most recent one.
- **Network:** Internet access is on by default. Restrict outbound traffic with network rules for untrusted input or secrets.

## More coding agents

Other coding agents that run in E2B sandboxes.

- [Claude Code](https://e2b.dev/integrations/claude-code.md): Run Claude Code in its own sandbox with a real filesystem, terminal, and git, so it can work unattended.
- [OpenAI Codex](https://e2b.dev/integrations/codex.md): Run OpenAI's Codex CLI headless in a sandbox and collect the diff when it's done.
- [Kimi Code CLI](https://e2b.dev/integrations/kimi-code-cli.md): Run Moonshot AI's Kimi Code CLI in a sandbox with full filesystem, terminal, and git access.

## Links

- [Get API key](https://console.e2b.dev/?tab=keys)
- [Read the docs](https://docs.e2b.dev/agents/gemini-cli)
- [E2B documentation](https://docs.e2b.dev)
