> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy to Railway

> Deploy Bun applications to Railway with zero-configuration builds, automatic SSL, and optional PostgreSQL provisioning.

[Railway](https://railway.com/) is an infrastructure platform with instant deployments from GitHub, automatic SSL, and built-in database provisioning. It auto-detects Bun projects and requires zero configuration for most apps.

**Prerequisites:**

* A Bun application ready for deployment
* A [Railway account](https://railway.app/)
* A GitHub account (for dashboard deployments) or the Railway CLI

***

## Method 1: Deploy via CLI

<Steps>
  <Step title="Install the Railway CLI">
    ```bash theme={null}
    bun install -g @railway/cli
    ```
  </Step>

  <Step title="Log in">
    ```bash theme={null}
    railway login
    ```
  </Step>

  <Step title="Initialize a new project">
    Run this from your project directory:

    ```bash theme={null}
    railway init
    ```
  </Step>

  <Step title="Add a database (optional)">
    If your app uses PostgreSQL, add it before deploying:

    ```bash theme={null}
    # Add PostgreSQL — do this before adding the app service
    railway add --database postgres

    # Add the app service and wire up the database URL
    railway add --service my-bun-app --variables DATABASE_URL=\${{Postgres.DATABASE_URL}}
    ```

    <Note>
      Skip this step if your application does not need a database.
    </Note>
  </Step>

  <Step title="Deploy and expose a public domain">
    ```bash theme={null}
    # Deploy the application
    railway up

    # Generate a public HTTPS domain
    railway domain
    ```

    Your app is now live. Railway auto-deploys on every push to your connected branch.
  </Step>
</Steps>

***

## Method 2: Deploy via Dashboard

<Steps>
  <Step title="Create a new project">
    1. Go to the [Railway Dashboard](https://railway.com/dashboard).
    2. Click **+ New → GitHub repo**.
    3. Authorize Railway and select your repository.
  </Step>

  <Step title="Add a database (optional)">
    1. In the project canvas, click **+ New → Database → Add PostgreSQL**.
    2. Select your app service (not the database).
    3. Go to the **Variables** tab.
    4. Click **+ New Variable → Add Reference** and select `DATABASE_URL` from the Postgres service.

    <Note>
      Skip this step if your application does not need a database.
    </Note>
  </Step>

  <Step title="Generate a public domain">
    1. Select your app service.
    2. Go to the **Settings** tab.
    3. Under **Networking**, click **Generate Domain**.
  </Step>
</Steps>

Your app is now live. Railway re-deploys automatically on every GitHub push.

***

## Configuration

By default, Railway uses [Nixpacks](https://docs.railway.com/guides/build-configuration#nixpacks-options) to auto-detect and build your Bun project with zero configuration.

For better Bun support and guaranteed compatibility with the latest Bun versions, use [Railpack](https://docs.railway.com/guides/build-configuration#railpack) instead. Create a `railway.json` in your project root:

```json railway.json theme={null}
{
  "$schema": "https://railway.com/railway.schema.json",
  "build": {
    "builder": "RAILPACK"
  }
}
```

## Custom start command

To override the default start command, set it in the Railway dashboard under **Settings → Deploy → Start command**, or add it to `railway.json`:

```json railway.json theme={null}
{
  "$schema": "https://railway.com/railway.schema.json",
  "build": {
    "builder": "RAILPACK"
  },
  "deploy": {
    "startCommand": "bun run src/index.ts"
  }
}
```

## Environment variables

Set environment variables in the Railway dashboard under your service's **Variables** tab. They are injected into `process.env` at runtime:

```typescript theme={null}
const port = parseInt(process.env.PORT ?? "3000");
const dbUrl = process.env.DATABASE_URL;
```

<Tip>
  Railway automatically injects a `PORT` environment variable. Always use `process.env.PORT` rather than a hard-coded port number.
</Tip>
