scripts/comfyui_setup.sh
scripts/comfyui_setup.shBrowse 33 files
2,938 tokens
10,447 bytes
Token encoding: o200k_base
Snapshot 24fd22b
← Back to SKILL.md
1#!/usr/bin/env bash2# ComfyUI Setup — Install, launch, and verify using the official comfy-cli.3#4# Improvements over v1:5# - Prefers `pipx` / `uvx` over global `pip install` (avoids polluting system Python)6# - Idempotent: detects already-running server and skips re-launch7# - Configurable port via --port=N (default 8188)8# - Configurable workspace via --workspace=PATH9# - Persistent log file in /tmp/comfyui_setup.<pid>.log for debugging10# - SIGINT trap cleans up partial state11# - Refuses local install when hardware_check.py verdict is "cloud"12# - Forwards extra flags to comfy-cli (e.g. --cuda-version=12.4)13#14# Usage:15# bash scripts/comfyui_setup.sh16# (auto-detects GPU; uses recommendation from hardware_check.py)17# bash scripts/comfyui_setup.sh --nvidia18# bash scripts/comfyui_setup.sh --m-series --port=819019# bash scripts/comfyui_setup.sh --amd --workspace=/data/comfy20#21# Flags:22# --nvidia | --amd | --m-series | --cpu GPU selection (skips hw check)23# --port=N HTTP port (default 8188)24# --workspace=PATH ComfyUI install location25# --skip-launch Install only, don't start server26# --force-cloud-override Install locally even if hw says cloud27# -- Pass remaining args to `comfy install`28 29set -euo pipefail30 31SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"32HARDWARE_CHECK="$SCRIPT_DIR/hardware_check.py"33LOG_FILE="/tmp/comfyui_setup.$$.log"34PORT=818835WORKSPACE=""36GPU_FLAG=""37SKIP_LAUNCH=038FORCE_CLOUD_OVERRIDE=039EXTRA_INSTALL_ARGS=()40 41cleanup() {42 local exit_code=$?43 if [ $exit_code -ne 0 ]; then44 echo "==> Setup exited with status $exit_code. Log: $LOG_FILE" >&245 fi46 exit $exit_code47}48trap cleanup EXIT INT TERM49 50log() { echo "==> $*" | tee -a "$LOG_FILE" >&2; }51err() { echo "ERROR: $*" | tee -a "$LOG_FILE" >&2; }52 53# --- Argument parsing ---54PASSTHROUGH=055for arg in "$@"; do56 if [ "$PASSTHROUGH" -eq 1 ]; then57 EXTRA_INSTALL_ARGS+=("$arg")58 continue59 fi60 case "$arg" in61 --nvidia|--amd|--m-series|--cpu)62 GPU_FLAG="$arg"63 ;;64 --port=*)65 PORT="${arg#*=}"66 ;;67 --workspace=*)68 WORKSPACE="${arg#*=}"69 ;;70 --skip-launch)71 SKIP_LAUNCH=172 ;;73 --force-cloud-override)74 FORCE_CLOUD_OVERRIDE=175 ;;76 --)77 PASSTHROUGH=178 ;;79 --help|-h)80 # Print the leading comment block, stripping the `# ` prefix.81 # Stops at the first blank line which separates docs from code.82 awk '83 NR == 1 { next } # skip shebang84 /^[^#]/ { exit } # stop at first non-comment line85 /^$/ { exit } # ...or first blank line86 { sub(/^# ?/, ""); print }87 ' "$0"88 exit 089 ;;90 *)91 err "Unknown argument: $arg"92 exit 6493 ;;94 esac95done96 97log "Logging to $LOG_FILE"98 99# --- Step 0: Hardware check (skipped if user gave an explicit GPU flag) ---100if [ -z "$GPU_FLAG" ]; then101 if [ ! -f "$HARDWARE_CHECK" ]; then102 log "hardware_check.py not found — defaulting to --nvidia"103 GPU_FLAG="--nvidia"104 else105 log "Running hardware check…"106 set +e107 HW_JSON="$(python3 "$HARDWARE_CHECK" --json 2>>"$LOG_FILE")"108 HW_EXIT=$?109 set -e110 111 if [ -z "$HW_JSON" ]; then112 err "hardware_check.py produced no output (exit $HW_EXIT). Pass an explicit flag."113 exit 1114 fi115 echo "$HW_JSON" | tee -a "$LOG_FILE" >&2116 117 VERDICT="$(echo "$HW_JSON" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("verdict",""))')"118 FLAG="$(echo "$HW_JSON" | python3 -c 'import sys,json; print(json.load(sys.stdin).get("comfy_cli_flag") or "")')"119 120 if [ "$VERDICT" = "cloud" ] && [ "$FORCE_CLOUD_OVERRIDE" -ne 1 ]; then121 log ""122 log "Hardware check: this machine is not suitable for local ComfyUI."123 log "Recommended: Comfy Cloud — https://platform.comfy.org"124 log ""125 log "To override and force a local install, re-run with --force-cloud-override"126 log "or pass an explicit GPU flag (--nvidia|--amd|--m-series|--cpu)."127 exit 2128 fi129 130 if [ "$VERDICT" = "marginal" ]; then131 log "Hardware check: verdict is MARGINAL."132 log " SD1.5 should work; SDXL/Flux may be slow or OOM."133 log " Consider Comfy Cloud for heavier workflows: https://platform.comfy.org"134 fi135 136 if [ -z "$FLAG" ]; then137 log "hardware_check could not pick a comfy-cli flag. Defaulting to --nvidia."138 log "(For Intel Arc or unsupported hardware, use the manual install path.)"139 GPU_FLAG="--nvidia"140 else141 GPU_FLAG="$FLAG"142 fi143 fi144fi145 146log "GPU flag: $GPU_FLAG"147log "Port: $PORT"148[ -n "$WORKSPACE" ] && log "Workspace: $WORKSPACE"149[ "${#EXTRA_INSTALL_ARGS[@]}" -gt 0 ] && log "Extra install args: ${EXTRA_INSTALL_ARGS[*]}"150 151# --- Step 1: Install comfy-cli (prefer pipx / uvx over global pip) ---152COMFY_BIN=""153if command -v comfy >/dev/null 2>&1; then154 COMFY_BIN="comfy"155 log "comfy-cli already on PATH: $(comfy -v 2>/dev/null || echo 'unknown version')"156elif command -v uvx >/dev/null 2>&1; then157 log "Using uvx (no install needed)"158 COMFY_BIN="uvx --from comfy-cli comfy"159elif command -v pipx >/dev/null 2>&1; then160 log "Installing comfy-cli via pipx…"161 pipx install comfy-cli >>"$LOG_FILE" 2>&1162 COMFY_BIN="comfy"163 # pipx adds shims to ~/.local/bin which may need to be on PATH164 if ! command -v comfy >/dev/null 2>&1; then165 if [ -x "$HOME/.local/bin/comfy" ]; then166 export PATH="$HOME/.local/bin:$PATH"167 COMFY_BIN="$HOME/.local/bin/comfy"168 fi169 fi170else171 log "Neither pipx nor uvx found. Falling back to pip install --user…"172 log " (Recommend installing pipx: https://pipx.pypa.io)"173 if ! pip install --user comfy-cli >>"$LOG_FILE" 2>&1; then174 # macOS: PEP 668 externally-managed-environment may block --user175 log "pip install --user failed. Retrying with --break-system-packages…"176 pip install --user --break-system-packages comfy-cli >>"$LOG_FILE" 2>&1 || {177 err "Could not install comfy-cli. Install pipx or uv first."178 exit 1179 }180 fi181 # Resolve the actual `comfy` script — pip --user puts it in:182 # Linux: ~/.local/bin/comfy183 # macOS: ~/Library/Python/<ver>/bin/comfy OR ~/.local/bin/comfy184 COMFY_BIN=""185 for candidate in "$HOME/.local/bin/comfy" \186 "$HOME/Library/Python/3.13/bin/comfy" \187 "$HOME/Library/Python/3.12/bin/comfy" \188 "$HOME/Library/Python/3.11/bin/comfy" \189 "$HOME/Library/Python/3.10/bin/comfy"; do190 if [ -x "$candidate" ]; then191 COMFY_BIN="$candidate"192 export PATH="$(dirname "$candidate"):$PATH"193 break194 fi195 done196 if [ -z "$COMFY_BIN" ]; then197 if command -v comfy >/dev/null 2>&1; then198 COMFY_BIN="comfy"199 else200 err "Installed comfy-cli but couldn't find the 'comfy' script."201 err "Add the right Python user-bin directory to PATH and retry."202 exit 1203 fi204 fi205fi206 207# --- Step 2: Disable analytics tracking (avoid interactive prompt) ---208log "Disabling analytics tracking…"209$COMFY_BIN --skip-prompt tracking disable >>"$LOG_FILE" 2>&1 || true210 211# --- Step 3: Install ComfyUI ---212WORKSPACE_ARG=()213if [ -n "$WORKSPACE" ]; then214 WORKSPACE_ARG=(--workspace "$WORKSPACE")215fi216 217if $COMFY_BIN "${WORKSPACE_ARG[@]}" which 2>/dev/null | grep -q "ComfyUI"; then218 EXISTING_WS="$($COMFY_BIN "${WORKSPACE_ARG[@]}" which 2>/dev/null || true)"219 log "ComfyUI already installed at: $EXISTING_WS"220else221 log "Installing ComfyUI ($GPU_FLAG)…"222 if ! $COMFY_BIN "${WORKSPACE_ARG[@]}" --skip-prompt install "$GPU_FLAG" "${EXTRA_INSTALL_ARGS[@]}" >>"$LOG_FILE" 2>&1; then223 err "Install failed. Tail of log:"224 tail -20 "$LOG_FILE" >&2225 exit 1226 fi227fi228 229if [ "$SKIP_LAUNCH" -eq 1 ]; then230 log "Setup complete (--skip-launch). Run \`$COMFY_BIN launch --background -- --port $PORT\` when ready."231 exit 0232fi233 234# --- Step 4: Detect already-running server ---235if curl -fsS "http://127.0.0.1:$PORT/system_stats" >/dev/null 2>&1; then236 log "Server already running on port $PORT — skipping launch."237 log "Stop with \`$COMFY_BIN stop\` if you want a fresh start."238 curl -fsS "http://127.0.0.1:$PORT/system_stats" | python3 -m json.tool 2>/dev/null || true239 log "Done."240 exit 0241fi242 243# --- Step 5: Launch ---244log "Launching ComfyUI in background on port $PORT…"245LAUNCH_EXTRAS=("--" "--port" "$PORT")246if ! $COMFY_BIN "${WORKSPACE_ARG[@]}" launch --background "${LAUNCH_EXTRAS[@]}" >>"$LOG_FILE" 2>&1; then247 err "Background launch failed. Tail of log:"248 tail -20 "$LOG_FILE" >&2249 err "Try foreground launch to see real-time errors: $COMFY_BIN launch -- --port $PORT"250 exit 1251fi252 253# --- Step 6: Wait for server ---254log "Waiting for server…"255MAX_WAIT=60256ELAPSED=0257while [ $ELAPSED -lt $MAX_WAIT ]; do258 if curl -fsS "http://127.0.0.1:$PORT/system_stats" >/dev/null 2>&1; then259 log "Server is running!"260 curl -fsS "http://127.0.0.1:$PORT/system_stats" | python3 -m json.tool 2>/dev/null || true261 break262 fi263 sleep 2264 ELAPSED=$((ELAPSED + 2))265done266 267if [ $ELAPSED -ge $MAX_WAIT ]; then268 err "Server did not start within ${MAX_WAIT}s."269 err "Inspect log: $LOG_FILE"270 err "Or run foreground: $COMFY_BIN launch -- --port $PORT"271 exit 1272fi273 274log ""275log "Setup complete!"276log " Server: http://127.0.0.1:$PORT"277log " Web UI: http://127.0.0.1:$PORT (open in browser)"278log " Stop: $COMFY_BIN stop"279log " Log: $LOG_FILE (kept until shell closes)"280log ""281log "Next steps:"282log " - Download a model: $COMFY_BIN model download --url <URL> --relative-path models/checkpoints"283log " - Run a workflow: python3 $SCRIPT_DIR/run_workflow.py --workflow <file.json> --args '{...}'"284 285# Disable trap on success path286trap - EXIT287 Referenced from SKILL.md
SKILL.mdView in source ↗
Source excerpt starting at line 11.SKILL.mdView in source ↗11setup:12 help: "Run scripts/hardware_check.py FIRST to decide local vs Comfy Cloud; then scripts/comfyui_setup.sh auto-installs locally (or use Cloud API key for platform.comfy.org)."13metadata:
Source excerpt starting at line 208.SKILL.mdView in source ↗208| **Lifecycle (use comfy-cli)** | | |209| "install ComfyUI" | comfy-cli | `bash scripts/comfyui_setup.sh` |210| "start ComfyUI" | comfy-cli | `comfy launch --background` |
Source excerpt starting at line 304.304```bash305bash scripts/comfyui_setup.sh306# Or with overrides:307bash scripts/comfyui_setup.sh --m-series --port=8190 --workspace=/data/comfy308```