# Dev Mode

> Run LearnHouse locally with npx learnhouse dev. PostgreSQL and Redis run in Docker, and the API, web and collab servers run with hot reload.

The `dev` command starts a full local development environment with hot reload for all services.

```bash
npx learnhouse dev
```

## How It Works

The command splits infrastructure and application services:

- **Infrastructure** (Docker): PostgreSQL with pgvector and Redis run in containers, defined in `.learnhouse/docker-compose.dev.yml` (the CLI writes this file on each run)
- **Application** (local): the API, web, and collab servers run directly on your machine with hot reload

## Requirements

- Docker with Docker Compose v2
- [bun](https://bun.sh/) (for web and collab dependencies)
- [uv](https://docs.astral.sh/uv/) (for Python API dependencies)

> `bun` and `uv` must be installed beforehand. The CLI checks for both and stops with an install link if either is missing. It runs `bun install` and `uv sync` for the project, but does not install the tools themselves.

## Startup Flow

1. **Detect project root**: walks up from the current directory until it finds `apps/api` and `apps/web`
2. **Check env files**: looks for the variables each app needs (see below). If any are missing, it lists them and offers to write development defaults or stop.
3. **Link Enterprise code** (only with `--ee`)
4. **Check tools**: `bun`, `uv`, Docker, and a running Docker daemon
5. **Admin setup and infrastructure**: when the `learnhouse-db-dev` and `learnhouse-redis-dev` containers are not already running, prompts for an admin email (default `admin@school.dev`) and password, then starts them. When they are running, they are reused and the prompt is skipped. Pass `--admin-email` and `--admin-password` to skip the prompt.
6. **Health checks**: waits for PostgreSQL and Redis to be ready
7. **Install dependencies**: runs `bun install` in `apps/web` and `apps/collab` when `node_modules` is missing, and `uv sync` in `apps/api` when `.venv` is missing
8. **Start services**: launches the API, web, and collab servers with color-coded output

## Env files

The CLI checks these variables and, if you accept, appends development defaults for the missing ones only:

| File | Variable | Default written |
| --- | --- | --- |
| `apps/api/.env` | `LEARNHOUSE_AUTH_JWT_SECRET_KEY` | Random secret |
| `apps/api/.env` | `COLLAB_INTERNAL_KEY` | Random secret |
| `apps/web/.env.local` | `NEXT_PUBLIC_LEARNHOUSE_BACKEND_URL` | `http://localhost:1338/` |
| `apps/collab/.env` | `COLLAB_PORT` | `4000` |
| `apps/collab/.env` | `LEARNHOUSE_API_URL` | `http://localhost:1338` |
| `apps/collab/.env` | `LEARNHOUSE_AUTH_JWT_SECRET_KEY` | Same value as the API |
| `apps/collab/.env` | `COLLAB_INTERNAL_KEY` | Same value as the API |

Other API settings fall back to `apps/api/config/config.yaml`, which ships localhost defaults (database `postgresql://learnhouse:learnhouse@localhost:5432/learnhouse`, Redis `redis://localhost:6379/learnhouse`).

The CLI also sets these for the processes it starts:

| Variable | Value |
| --- | --- |
| `LEARNHOUSE_DEVELOPMENT_MODE` | `true` |
| `LEARNHOUSE_INITIAL_ADMIN_EMAIL` / `LEARNHOUSE_INITIAL_ADMIN_PASSWORD` | The admin credentials, when given |
| `LEARNHOUSE_DISABLE_EE` | `1`, unless `--ee` is passed |
| `LEARNHOUSE_FORCE_EE` | `1`, only with `--ee` |

## Services

| Service | Command | Port |
| --- | --- | --- |
| API | `uv run python app.py` | 1338 |
| Web | `next dev --turbopack` | 3000 |
| Collab | `tsx watch src/index.ts` | 4000 |
| PostgreSQL | Docker (`pgvector/pgvector:pg16`) | 5432, bound to `127.0.0.1` |
| Redis | Docker (`redis:8.6.1-alpine`) | 6379, bound to `127.0.0.1` |

The development database uses user, password and database name `learnhouse`.

## Interactive Controls

While running, you can type these keys to manage services:

| Key | Action |
| --- | --- |
| `ra` | Restart API |
| `rw` | Restart Web |
| `rc` | Restart Collab |
| `rb` | Restart all services |
| `q` | Quit (graceful shutdown) |

## Enterprise Edition

Use the `--ee` flag to enable Enterprise Edition features:

```bash
npx learnhouse dev --ee
```

With the flag, the CLI links `apps/api/ee` and `apps/web/ee` to a sibling EE checkout at `../ee/apps/<app>/ee` (on Windows it uses directory junctions). If no EE checkout is found, that app runs as the community edition. Without the flag, `LEARNHOUSE_DISABLE_EE=1` keeps the app on the community edition even if an `ee/` folder exists.

## Shutdown

When you quit (`q` or Ctrl+C):

- Local services (API, Web, Collab) are stopped
- Docker containers (PostgreSQL, Redis) keep running for the next session
- To fully stop infrastructure:

```bash
docker compose -f .learnhouse/docker-compose.dev.yml -p learnhouse-dev down
```

> Database and Redis data persist across dev sessions in Docker volumes. Running `down` keeps them; add `-v` to delete them.
