This guide provides a comprehensive walkthrough for deploying a persistent, multi-service Langfuse server on a Slurm-based HPC cluster like Misha. Traditional HPC environments are designed for short-lived, computational batch jobs, not for hosting long-running web services. This guide presents a working method that overcomes these challenges using Apptainer containers and a robust Slurm script.

Why This Method Works: The Key to Success

Our previous attempts failed because of a subtle but critical issue. We used the apptainer instance start command, which relies on Apptainer’s internal instance management daemon. On this specific cluster environment, that daemon was failing to correctly launch the processes inside the containers, leading to silent crashes with no log output.

The final, working script succeeds because it abandons the apptainer instance subsystem and uses a more direct, traditional, and resilient Linux approach: nohup apptainer run ... &.

  1. Direct Execution with apptainer run: Instead of asking the Apptainer daemon to manage the service, this command directly executes the container’s default runscript as a standard process. This is a simpler and more fundamental way to run a container.

  2. Backgrounding with &: The & at the end of each line tells the shell to run the apptainer run command as a background job and immediately move to the next line of the script. This allows us to launch all six services concurrently.

  3. Manual Log Redirection with > ... 2>&1: This is the most critical change. We are no longer relying on Apptainer’s failing log system. The > ${DATA_DIR}/service.log 2>&1 syntax manually redirects all standard output (stdout) and standard error (stderr) from the container directly into a log file that we control. This guarantees that we can capture any errors or output the service produces.

  4. Self-Verification with ss: The script now includes a verification block that uses the ss command to check if the network ports for each service are actually open and listening. This provides immediate feedback on whether the services started successfully.

In essence, this solution works by bypassing the problematic high-level instance feature and instead using fundamental, battle-tested Linux process and I/O management, which is far more reliable in this complex environment.


Section 1: One-Time Setup

Before you can launch the server, you need to prepare your environment and build the necessary container files.

1.1: Configure Your Shell Environment

It’s highly recommended to set a permanent cache directory for Apptainer to prevent it from filling up your home directory. You do this by adding an export command to your shell’s startup file.

  1. Open your ~/.bashrc file for editing:
    nano ~/.bashrc
  2. Add the following lines to the end of the file. This sets your Apptainer cache to a directory within your scratch space.
    # Set a custom Apptainer cache directory
    export APPTAINER_CACHEDIR="${SCRATCH_DIR}/apptainer_cache"
    mkdir -p $APPTAINER_CACHEDIR
  3. Save the file and exit (Ctrl+X, then Y, then Enter).
  4. Load the new settings:
    source ~/.bashrc

1.2: Build the Apptainer Container Images (.sif files)

This process must be done on a compute node, not the login node.

  1. Request an interactive session on a compute node:

    srun --partition=day --pty --time=00:30:00 --mem=8G /bin/bash
  2. Once you are on the compute node, create a directory for your container images and build them one by one. This process will download several gigabytes of data.

    # Navigate to your project directory and create a folder for the images
    # Replace <your_username> with your actual username
    cd /gpfs/radev/home/<your_username>/project/
    mkdir -p langfuse_apptainer_images
    cd langfuse_apptainer_images
     
    # Build the required images based on the v3 docker-compose file
    echo "Building langfuse-web v3..."
    apptainer build langfuse-web.sif docker://langfuse/langfuse:3
     
    echo "Building langfuse-worker v3..."
    apptainer build langfuse-worker.sif docker://langfuse/langfuse-worker:3
     
    echo "Building postgres..."
    apptainer build postgres.sif docker://docker.io/library/postgres:latest
     
    echo "Building clickhouse..."
    apptainer build clickhouse.sif docker://docker.io/clickhouse/clickhouse-server:latest
     
    echo "Building redis v7..."
    apptainer build redis.sif docker://docker.io/library/redis:7
     
    echo "Building minio..."
    apptainer build minio.sif docker://docker.io/minio/minio:latest
  3. When all builds are complete, you can exit the interactive session. You only need to do this once.


Section 2: The Slurm Deployment Script

Save the following code as run_langfuse.sh in your project directory (e.g., /gpfs/radev/home/<your_username>/project/).

IMPORTANT: Before running, you must open this file and replace all placeholder secrets (marked with # CHANGEME) with your own unique and secure passwords.

#!/bin/bash
 
#==============================================================================
# SLURM SCRIPT FOR LANGFUSE V3
#==============================================================================
 
# --- Slurm Resource Request ---
#SBATCH --partition=week
#SBATCH --time=7-00:00:00
#SBATCH --job-name=langfuse-server
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=8
#SBATCH --mem=32G
#SBATCH --output=langfuse_%j.log
 
# --- Configuration ---
# !!! IMPORTANT: UPDATE ALL VALUES MARKED WITH # CHANGEME !!!
 
# For Postgres
export APPTAINERENV_POSTGRES_USER="postgres"
export APPTAINERENV_POSTGRES_PASSWORD="Langfuse123" # CHANGEME
export APPTAINERENV_POSTGRES_DB="postgres"
 
# For Redis
export APPTAINERENV_REDIS_AUTH="RedisSecret123" # CHANGEME
 
# For Clickhouse
export APPTAINERENV_CLICKHOUSE_USER="clickhouse"
export APPTAINERENV_CLICKHOUSE_PASSWORD="ClickhouseSecret123" # CHANGEME
export APPTAINERENV_CLICKHOUSE_DB="default"
 
# For MinIO
export APPTAINERENV_MINIO_ROOT_USER="minio"
export APPTAINERENV_MINIO_ROOT_PASSWORD="Minio123" # CHANGEME
 
# For Langfuse (Web and Worker) - These should be unique and secret
export APPTAINERENV_NEXTAUTH_URL="http://localhost:3000"
export APPTAINERENV_NEXTAUTH_SECRET="AuthSecret123" # CHANGEME
export APPTAINERENV_SALT="SaltSecret123" # CHANGEME
export APPTAINERENV_ENCRYPTION_KEY=$(openssl rand -hex 32) # Automatically generates a new secure key
export APPTAINERENV_TELEMETRY_ENABLED="true"
export APPTAINERENV_LANGFUSE_ENABLE_EXPERIMENTAL_FEATURES="true"
 
# --- Translate docker-compose URLs to localhost for the single-node environment ---
export APPTAINERENV_DATABASE_URL="postgresql://${APPTAINERENV_POSTGRES_USER}:${APPTAINERENV_POSTGRES_PASSWORD}@localhost:5432/${APPTAINERENV_POSTGRES_DB}"
export APPTAINERENV_CLICKHOUSE_URL="http://localhost:8123"
export APPTAINERENV_REDIS_HOST="localhost"
export APPTAINERENV_REDIS_PORT="6379"
export APPTAINERENV_LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT="http://localhost:9000"
export APPTAINERENV_LANGFUSE_S3_MEDIA_UPLOAD_ENDPOINT="http://localhost:9000"
 
# S3 Configuration
export APPTAINERENV_LANGFUSE_S3_EVENT_UPLOAD_BUCKET="langfuse"
export APPTAINERENV_LANGFUSE_S3_EVENT_UPLOAD_ACCESS_KEY_ID=${APPTAINERENV_MINIO_ROOT_USER}
export APPTAINERENV_LANGFUSE_S3_EVENT_UPLOAD_SECRET_ACCESS_KEY=${APPTAINERENV_MINIO_ROOT_PASSWORD}
export APPTAINERENV_LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE="true"
 
# --- Define Paths ---
# Replace <your_username> with your actual username
SIF_DIR="/gpfs/radev/home/<your_username>/project/langfuse_apptainer_images"
DATA_DIR="/gpfs/radev/home/<your_username>/project/langfuse_data"
 
# Create data directories if they don't exist
mkdir -p "${DATA_DIR}/postgres_data"
mkdir -p "${DATA_DIR}/clickhouse_data"
mkdir -p "${DATA_DIR}/minio_data"
 
# --- Output Hostname for SSH Tunneling ---
echo "################################################################"
echo "Langfuse Job Started on node: $(hostname)"
echo "To access the web UI, use this SSH tunnel from your LOCAL machine:"
echo "ssh -L 3000:$(hostname):3000 <your_username>@misha.ycrc.yale.edu"
echo "Then open http://localhost:3000 in your local web browser."
echo "################################################################"
 
# --- Launch Services in the Background using nohup and apptainer run ---
 
# 1. Start Postgres
echo "Starting Postgres..."
nohup apptainer run --bind ${DATA_DIR}/postgres_data:/var/lib/postgresql/data ${SIF_DIR}/postgres.sif > ${DATA_DIR}/postgres.log 2>&1 &
sleep 20
 
# 2. Start Redis
echo "Starting Redis..."
nohup apptainer run ${SIF_DIR}/redis.sif redis-server --port 6379 --bind 0.0.0.0 --requirepass ${APPTAINERENV_REDIS_AUTH} > ${DATA_DIR}/redis.log 2>&1 &
sleep 5
 
# 3. Start Clickhouse
echo "Starting Clickhouse..."
nohup apptainer run --bind ${DATA_DIR}/clickhouse_data:/var/lib/clickhouse ${SIF_DIR}/clickhouse.sif > ${DATA_DIR}/clickhouse.log 2>&1 &
sleep 15
 
# 4. Start MinIO
echo "Starting MinIO..."
# Pre-create the bucket directory within the persistent volume
mkdir -p "${DATA_DIR}/minio_data/langfuse"
nohup apptainer run --bind ${DATA_DIR}/minio_data:/data ${SIF_DIR}/minio.sif server --address ":9000" --console-address ":9001" /data > ${DATA_DIR}/minio.log 2>&1 &
sleep 10
 
# 5. Start Langfuse Worker
echo "Starting Langfuse Worker..."
nohup apptainer run ${SIF_DIR}/langfuse-worker.sif > ${DATA_DIR}/langfuse_worker.log 2>&1 &
sleep 5
 
# 6. Start Langfuse Server (Web UI)
echo "Starting Langfuse Server..."
nohup apptainer run ${SIF_DIR}/langfuse-web.sif > ${DATA_DIR}/langfuse_web.log 2>&1 &
echo "Langfuse web server started. It may take a minute to become available."
 
# --- Verification and Status Check ---
sleep 20 # Give the web server time to initialize
echo ""
echo "--- Service Status Check ---"
 
if ss -lntp | grep -q ":3000 "; then
    echo "✓ SUCCESS: Langfuse web server is listening on port 3000."
else
    echo "✗ ERROR: Langfuse web server FAILED to start. Check logs:"
    echo "  tail -n 50 ${DATA_DIR}/langfuse_web.log"
fi
 
# --- Keep the Job Alive ---
echo ""
echo "Deployment script finished. Entering infinite sleep loop to keep job alive."
echo "Use 'scancel ${SLURM_JOB_ID}' to stop the server."
echo "Monitor web server logs with: tail -f ${DATA_DIR}/langfuse_web.log"
 
while true; do
    sleep 86400 # Sleep for a day
done

Section 3: Launching and Using Langfuse

3.1: Starting the Server

Navigate to your project directory on the Misha login node and submit the script to Slurm.

# Replace <your_username> with your actual username
cd /gpfs/radev/home/<your_username>/project/
sbatch run_langfuse.sh

3.2: Checking the Status

  • Check the Job Status: Use squeue -u <your_username> to see if the job is in the R (Running) state.
  • Check the Main Log: Check the langfuse_<jobid>.log file for the output of the script, including the vital SSH tunnel command.
  • Check Service-Specific Logs: If a service fails to start, you can now check its dedicated log file in your data directory, for example: tail -n 50 /gpfs/radev/home/<your_username>/project/langfuse_data/postgres.log

3.3: Accessing the Web UI

  1. From the main job log file (langfuse_<jobid>.log), copy the SSH tunnel command.
  2. Open a new terminal on your local computer (not the server).
  3. Paste and run the command, e.g., ssh -L 3000:r817u03n01.misha.ycrc.yale.edu:3000 <your_username>@misha.ycrc.yale.edu.
  4. Enter your HPC password when prompted. Keep this terminal window open.
  5. Open a web browser on your local computer and navigate to http://localhost:3000.

3.4: Stopping the Server

To shut down all services and release the compute node, simply cancel the Slurm job:

scancel <your_job_id>

Section 4: Getting Started with Langfuse

Once you access the UI for the first time, you can set up your projects and collaborate.

4.1: Create Your Admin Account

The first time you visit the Langfuse UI at http://localhost:3000, you will be greeted with a setup screen. Here you will create the first organization and your personal admin user account with an email and a new password.

4.2: Create a Project

Once logged in, you’ll be on the main dashboard.

  1. Click the “New Project” button.
  2. Give your project a name (e.g., “My LLM Experiment”) and an optional description.
  3. Click “Create Project”.

4.3: Get API Keys

API keys are used to send data from your applications (e.g., a Python script using LangChain or OpenAI) to your Langfuse server.

  1. Navigate into the project you just created.
  2. In the left sidebar, click on Settings.
  3. Go to the “API Keys” tab.
  4. Here you will find your Secret Key (SK) and Public Key (PK). You will use these in your application’s environment variables (LANGFUSE_SECRET_KEY and LANGFUSE_PUBLIC_KEY).

4.4: Invite Team Members

You can let other people on your team view the traces and analytics for your projects.

  1. In the top right corner, click the dropdown with your organization’s name and select Settings.
  2. Go to the “Members” tab.
  3. Click “Invite Member” and enter their email address to send an invitation. They will be able to sign up and access the projects within your organization.