# Deployment

Bub works interactively in your local terminal (see [Terminal interaction](/docs/app/terminal/)), and you can also deploy it to a server as a long-running service:

- **Persistent message gateway**: listen on Telegram, Feishu, or Discord 24/7 and reply to messages.
- **Containerized sandbox**: run development toolchains inside an isolated container and attach via `bub chat` without exposing the host filesystem.

Comparison of the two deployment modes:

| Mode | Best for | Key benefits |
| --- | --- | --- |
| **Docker Compose** (recommended) | Cloud VMs, VPS, NAS, local containers | Bundles Python, `uv`, Git, Node.js, and pnpm; isolates the host; easy to back up and upgrade |
| **System daemon** | Linux / Windows hosts with an existing Python environment | Low overhead; registers as a user service in one command via `bub gateway --install` |

## Before you start {#before-you-start}

1. **Model credentials**: set `BUB_MODEL` and `BUB_API_KEY` in `.env` (see [Configuration](/docs/app/configure/)).
2. **Channel allowlists**: **always configure an allowlist** (such as `BUB_TELEGRAM_ALLOW_USERS`) so unauthorized users cannot invoke your agent.
3. **Persistent data volume**: mount the data directory (host `~/.bub`, container `/data`) to preserve session Tapes and managed plugins across restarts.

:::caution[Security warning]
Never deploy to production with an empty allowlist. Without an allowlist, anyone who finds your bot can send prompts and run Agent tools.
:::

## Docker Compose deployment {#docker-compose}

The official image (`ghcr.io/bubbuild/bub`) comes preinstalled with Python 3.12, `uv`, Git, Node.js, and pnpm.

### 1. Prepare configuration {#configure}

Download [`docker-compose.yml`](https://github.com/bubbuild/bub/blob/main/docker-compose.yml) and [`env.example`](https://github.com/bubbuild/bub/blob/main/env.example):

```bash
mkdir -p bub-deploy && cd bub-deploy
curl -O https://raw.githubusercontent.com/bubbuild/bub/main/docker-compose.yml
curl -O https://raw.githubusercontent.com/bubbuild/bub/main/env.example
cp env.example .env
```

Edit `.env`:

```bash title=".env"
# Model
BUB_MODEL=openrouter:openrouter/free
BUB_API_KEY=sk-or-...

# Channel (Telegram example)
BUB_TELEGRAM_TOKEN=123456789:your-telegram-bot-token
BUB_TELEGRAM_ALLOW_USERS=123456789,your_telegram_username

# Optional host paths
# BUB_HOME_HOST=~/.bub
# BUB_WORKSPACE_PATH=.
```

### 2. Review volume mounts {#volumes}

`docker-compose.yml` mounts three directories by default:

```yaml title="docker-compose.yml"
volumes:
  - ${BUB_WORKSPACE_PATH:-.}:/workspace
  - ${BUB_HOME_HOST:-${HOME}/.bub}:/data
  - ${BUB_AGENT_HOME:-${HOME}/.agents}:/root/.agents
```

| Container path | Host path | Purpose |
| --- | --- | --- |
| `/workspace` | `BUB_WORKSPACE_PATH` (default `.`) | **Workspace**: directory where the Agent reads/writes files and runs commands |
| `/data` | `BUB_HOME_HOST` (default `~/.bub`) | **Data directory** (`BUB_HOME=/data`): stores session Tapes and managed plugins |
| `/root/.agents` | `BUB_AGENT_HOME` (default `~/.agents`) | **Global skills**: shared skills across workspaces |

:::danger[Do not mount sensitive host directories]
The workspace defines which files the Agent can access. **Never** mount the host root `/` or directories containing secrets into `/workspace`.
:::

### 3. Start the container {#start}

```bash
    docker compose pull
    docker compose up -d
    docker compose logs -f app
    ```

    In production, pin a specific version tag in `.env` (e.g. `BUB_IMAGE=ghcr.io/bubbuild/bub:0.6.0`).
  ```bash
    docker compose up -d --build
    ```
  ```bash
    docker run -d --name bub --restart unless-stopped \
      --env-file .env \
      -e BUB_HOME=/data \
      -v "$PWD":/workspace \
      -v "$HOME/.bub":/data \
      -v "$HOME/.agents":/root/.agents \
      ghcr.io/bubbuild/bub:latest

    docker logs -f bub
    ```
  ### 4. Verify startup {#verify}

```bash
docker compose ps
docker compose logs -f app
```

When the Telegram gateway is ready, it logs:

```text
telegram.start allow_users_count=1 allow_chats_count=0 proxy_enabled=False
telegram.start polling
```

### 5. Attach an interactive chat {#chat}

Compose enables `stdin_open` and `tty` by default. Even while the container runs the gateway in the background, you can attach at any time for an interactive `bub chat` session:

```bash
docker compose exec -it app bub chat
```

This lets you pair-program directly with the Agent inside an isolated cloud sandbox with the preinstalled toolchain.

### 6. Customize startup and dependencies {#entrypoint}

At container startup, `entrypoint.sh` runs three steps:

1. If `/workspace/bub-reqs.txt` exists, installs extra Python packages via `uv pip install`.
2. Activates the virtual environment and runs `bub install` to sync managed plugins in `/data/bub-project`.
3. Executes `/workspace/startup.sh` if present; otherwise runs `bub gateway`.

To customize the startup command, create an executable `/workspace/startup.sh` (use `exec` so shutdown signals reach the gateway):

```bash
#!/bin/bash
# /workspace/startup.sh
set -euo pipefail
exec /app/.venv/bin/bub gateway --enable-channel telegram
```

### 7. Manage plugins inside the container {#plugins}

Managed plugins live on the `/data/bub-project` volume and can be installed or updated without rebuilding the image:

```bash
docker compose exec app bub install https://github.com/owner/repo.git@v1.2.0
docker compose exec app bub update
docker compose restart app
```

### 8. Back up and upgrade {#upgrade}

Stop the container and back up the data directory before upgrading:

```bash
docker compose stop app
tar -czf "bub-data-$(date +%Y%m%d-%H%M%S).tar.gz" "$HOME/.bub"
cp .env ".env.bak-$(date +%Y%m%d-%H%M%S)"

docker compose pull
docker compose up -d
```

To roll back, switch `BUB_IMAGE` back to the previous tag in `.env` (for example `BUB_IMAGE=ghcr.io/bubbuild/bub:0.6.0`) and restart.

---

## System daemon mode {#daemon}

On a Linux or Windows host with Python installed, you can register the gateway as a background user service with `bub gateway --install`:

- **Linux**: registers as a systemd user service at `~/.config/systemd/user/bub-gateway.service`.
- **Windows**: registers as a scheduled task for the current user (starts at login and restarts on failure).
- **macOS**: `--install` is not supported yet; use `launchd`, `tmux`, or the Docker approach above.

### 1. Install the service {#daemon-install}

Run in the workspace directory containing `.env`:

```bash
uv venv && source .venv/bin/activate
uv pip install 'bub[app]'
bub gateway --install
```

The installer automatically records the current Python interpreter path, workspace directory, and environment variables.

### 2. Manage the service (Linux) {#daemon-manage}

After you log out of SSH on Linux, user-level services stop by default. Enable linger to keep the gateway running 24/7:

```bash
loginctl enable-linger $USER
```

Common `systemctl` commands:

```bash
systemctl --user status bub-gateway
journalctl --user -u bub-gateway -f
systemctl --user restart bub-gateway
systemctl --user stop bub-gateway
```

### 3. Uninstall the service {#daemon-uninstall}

To remove the background service, run:

```bash
bub gateway --uninstall
```

---

## Security checklist {#security-checklist}

- **Configure allowlists**: set `BUB_TELEGRAM_ALLOW_USERS` or `BUB_TELEGRAM_ALLOW_CHATS`.
- **Scope the workspace**: mount only the target project directory, never host credentials or system paths.
- **Set a step limit**: configure `BUB_MAX_STEPS` (unlimited by default; set to e.g. `50` in production) to prevent runaway loops.
- **Protect secrets**: pass API keys via `.env` or environment variables and keep them out of version control.
- **Back up Tapes regularly**: archive `~/.bub` (or container `/data`) periodically.