---
title: "Self-hosting"
description: "Run the full Pug stack (Go services, Postgres, ClickHouse, NATS and Dragonfly) on your own infrastructure."
---

> Documentation Index
> Fetch the complete documentation index at: https://docs.pug.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Self-hosting

Pug is open-source product analytics. Run the full stack on your own infrastructure. Self-hosting gives you complete data ownership and your own deployment topology, on infrastructure as private as you need.

## What you'll run

A working Pug instance has **two deployables**, the backend and the dashboard, plus **four backing stores**. They live in two repositories:

| Part | Repo | What it is | You deploy it as |
|------|------|-----------|------------------|
| **Backend** | [`pug-sh/pug`](https://github.com/pug-sh/pug) | The Go API server, the background workers, and the `pug` CLI - all one binary | Long-running server + worker processes |
| **Dashboard** | [`pug-sh/app`](https://github.com/pug-sh/app) | The React UI ("Pug UI") that reads and manages your data | Static files behind a web server / CDN |

Both are AGPL-3.0 licensed.

Everything else (Postgres, ClickHouse, NATS, and Dragonfly) you either run yourself or point at a managed equivalent.

Once both are up, you do one more thing: **[point your SDKs](/self-hosting/connect-sdks) at your own server** instead of Pug Cloud. That's the whole job.

1. **Deploy the backend** (pug-sh/pug) - Run the API server, the workers, and migrations against your four stores.
2. **Deploy the dashboard** (pug-sh/app) - Build the React SPA and serve it, pointed at your backend's URL.
3. **Connect your SDKs** (browser / android / ios / flutter / node / go / ruby) - Set the SDK endpoint to your server so events flow to your instance.

## Get the code

The backend (the server, every worker, and the `pug` CLI) builds from one repo:

```bash
git clone https://github.com/pug-sh/pug.git
cd pug
```

The dashboard is a separate repo; see [Dashboard](/self-hosting/dashboard) for its build. To build from source you need a **Go >= 1.27.2** toolchain (backend) and **Bun** (dashboard). The backend also ships as [prebuilt container images](/self-hosting/deployment#published-images), which need no Go; the dashboard has no image, so it always needs a Bun build. Full prerequisites are in [Requirements](/self-hosting/requirements).

**Recommended reading order:** [Requirements](/self-hosting/requirements) -> [Configuration](/self-hosting/configuration) -> [Deployment](/self-hosting/deployment) -> [Dashboard](/self-hosting/dashboard) -> [Connect your SDKs](/self-hosting/connect-sdks). To run the whole stack on one machine for local work, follow [Development](/self-hosting/development) instead.

## When to self-host

| Self-host when... | Use Pug Cloud when... |
|-----------------|----------------------|
| Data must stay in your VPC | You want the fastest path to production |
| Compliance or regulatory requirements | You prefer managed infrastructure and upgrades |
| You have an existing Postgres / ClickHouse / NATS cluster | You don't want to operate stateful services |
| On-premise or air-gapped environments | |

Air-gapped takes some extra setup. Operator email goes out through Resend or Amazon SES, so without internet access nobody can get a magic link: give the instance an internal [OIDC provider](/self-hosting/authentication) to sign people in. Mirror the dashboard's [map assets](/self-hosting/dashboard#map-assets) too.

## Architecture

The backend is the **API server**, a set of **background workers** and four **scheduled jobs**, backed by four stores. The **dashboard** is a static single-page app that talks to the same API server.

- **Clients**: Browser / Mobile SDK, Dashboard - React SPA
- **API**: pug server - Connect RPC :3000, Dragonfly - caches + cookieless sessions
- **Queue**: NATS - JetStream
- **Workers**: events, profiles x3, compliance, email
- **Scheduled jobs**: cron purge, cron usage, cron retention, cron expired-tokens
- **Storage**: ClickHouse - analytics events, PostgreSQL - config + profiles

Both the SDKs (ingest) and the dashboard (read/manage) hit the **same** `pug server` on port `3000` - they differ only in which RPC services they call and how they authenticate ([API key](/get-started/authentication) for SDKs, JWT for the dashboard). Dragonfly (a Redis-compatible store) holds the API-key and role caches, the daily cookieless salt and cookieless session stitching. Nothing in it needs to survive a restart, but it is a hard dependency: the server won't start without it.

Tracing one event end-to-end:

1. **SDK -> pug server** (x-api-key) - An SDK POSTs the event to the API server, authenticated with your API key.
2. **pug server -> NATS** (enrich + publish) - The server enriches the event (geo, user-agent, bot detection), validates it, and publishes it onto a JetStream stream.
3. **events worker -> ClickHouse** (persist) - The events worker consumes the stream and writes the events to ClickHouse.
4. **Dashboard <- pug server** (reads) - The dashboard queries the same server for insights, which read from ClickHouse and PostgreSQL.

## Processes at a glance

Every long-running process is a sub-command of the single `pug` binary: the API server (`pug server`) plus **7 worker processes** - `events`, `email`, `compliance`, `demo`, and three profile workers (`identify`, `alias`, `upsert`).

Four **scheduled jobs** are sub-commands too: `pug cron purge` erases deleted projects, `pug cron usage` meters event usage, `pug cron retention` deletes event history past its retention, and `pug cron expired-tokens` deletes expired tokens. Each runs one pass and exits, so a scheduler (a Kubernetes CronJob, a systemd timer, cron) runs them - see [Scheduled jobs](/self-hosting/deployment#scheduled-jobs).

You can run these three ways, mixed and matched:

| Way | What it is | Best for |
|-----|-----------|----------|
| **`pug dev`** (alias `pug start`) | One process that runs the server **and every worker** together (not the scheduled jobs) | Local dev, or a small low-volume single-node instance |
| **Sub-commands** | `pug server`, `pug worker events`, ...: one process each | Production, when you want to scale and restart processes independently |
| **Standalone binaries / images** | A single-purpose binary (`pug-worker-events`) or container image per role | Kubernetes, systemd - see [Deployment](/self-hosting/deployment) |

For the **complete command tree**, every flag, and the build output, see the [CLI reference](/self-hosting/cli). [Deployment](/self-hosting/deployment) is the canonical runbook for running each process in production.

> The `demo` worker only generates the public demo dataset and stays idle unless `PUG_DEMO_ENABLED=true` - a normal self-host doesn't run it. The other six workers are the real pipeline.

## Sub-pages

- **Requirements** - Backing services, toolchains, ports, and sizing before you deploy.
[Read more](/self-hosting/requirements)
- **Configuration** - Every environment variable for the server and workers.
[Read more](/self-hosting/configuration)
- **Deployment** - Build the binaries or images, migrate, run each process, and harden.
[Read more](/self-hosting/deployment)
- **Dashboard** - Build, configure, and deploy the React UI, pointed at your backend.
[Read more](/self-hosting/dashboard)
- **Authentication** - Add Google or any OIDC identity provider, turn on org SSO connections, and verify domains.
[Read more](/self-hosting/authentication)
- **Connect your SDKs** - Repoint the browser, Android, iOS, Flutter, Node, Go and Ruby SDKs at your own instance.
[Read more](/self-hosting/connect-sdks)
- **CLI reference** - Every `pug` sub-command, flag, and the make targets that wrap them.
[Read more](/self-hosting/cli)
- **Development** - Run the whole stack locally with `pug dev` and Docker infra.
[Read more](/self-hosting/development)

Source: https://docs.pug.sh/self-hosting/index.mdx
