Skip to content

ComfyUI 更新教程

本文介绍在智川云实例中更新 ComfyUI 本体和依赖的两种常用方式。

  • 方式一:原地 git 更新
  • 方式二:源码同步更新

建议先尝试方式一。只有在 ComfyUI 目录本身不是完整 git 仓库,或者 .git 损坏导致原地更新失败时,再使用方式二。

更新前说明

适用前提

  • 已经在实例中安装并运行过 ComfyUI
  • 具备实例终端访问权限
  • 知道当前 ComfyUI 安装目录,本文示例默认使用 /ComfyUI

推荐保留的数据目录

无论使用哪种更新方式,都建议保留以下目录,避免覆盖模型、工作流配置和生成结果:

text
models
custom_nodes
user
input
output
temp

方式选择建议

场景推荐方式
ComfyUI 目录是正常 git 仓库,只想更新本体和依赖方式一:原地 Git 更新
git pull 失败,或提示 .git 仓库损坏方式二:源码同步更新

0. 设置变量

两种方式都先执行下面这段,统一后续命令中的目录、端口和 Python 解释器:

bash
export COMFY_DIR=/ComfyUI
export PORT=8188

if [ -x /venv/bin/python ]; then
  export PY_BIN=/venv/bin/python
elif [ -x /opt/conda/envs/comfyui/bin/python ]; then
  export PY_BIN=/opt/conda/envs/comfyui/bin/python
else
  export PY_BIN=$(command -v python3 || command -v python)
fi

echo "COMFY_DIR=$COMFY_DIR"
"$PY_BIN" -V

方式一:原地 Git 更新

适用场景:ComfyUI 目录本身是正常 git 仓库,只需要更新本体和依赖。

优点:改动小,保留现有 modelscustom_nodesuserinputoutput

1. 配置网络

这里的 https://gh-proxy.org 是示例代理地址,实际使用时换成当前机器可访问的 GitHub 镜像或代理地址。

bash
git config --global url."https://gh-proxy.org/https://github.com/".insteadOf "https://github.com/"

"$PY_BIN" -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
"$PY_BIN" -m pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn

cat > /etc/profile.d/comfyui-mirrors.sh <<'EOF'
export HF_ENDPOINT=https://hf-mirror.com
export PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
EOF
chmod 0644 /etc/profile.d/comfyui-mirrors.sh

作用:

  • GitHub 走 gh-proxy.org
  • pip 走清华源
  • Hugging Face 走 hf-mirror.com

2. 配置 ComfyUI-Manager

bash
cp -a "$COMFY_DIR/user/__manager/config.ini" \
  "$COMFY_DIR/user/__manager/config.ini.bak.$(date +%Y%m%d-%H%M%S)"

"$PY_BIN" - <<'PY'
from pathlib import Path
import os

cfg = Path(os.environ["COMFY_DIR"]) / "user/__manager/config.ini"
updates = {
    "channel_url": "https://gh-proxy.org/https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main",
    "default_cache_as_channel_url": "True",
    "update_policy": "nightly-comfyui",
    "network_mode": "public",
}

lines = cfg.read_text().splitlines()
seen = set()
out = []

for line in lines:
    key = line.split("=", 1)[0].strip() if "=" in line else ""
    if key in updates:
        out.append(f"{key} = {updates[key]}")
        seen.add(key)
    else:
        out.append(line)

for key, value in updates.items():
    if key not in seen:
        out.append(f"{key} = {value}")

cfg.write_text("\n".join(out) + "\n")
PY

grep -q '^gh-proxy::' "$COMFY_DIR/user/__manager/channels.list" || \
echo 'gh-proxy::https://gh-proxy.org/https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main' \
  >> "$COMFY_DIR/user/__manager/channels.list"

说明:

  • nightly-comfyui:更新到 master / nightly
  • stable-comfyui:只更新到稳定版

3. 安装 br 解压依赖

bash
"$PY_BIN" -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple brotlicffi

作用:避免 Manager 通过 gh-proxy.org 拉取 raw 文件时报 br 解压错误。

4. 更新 ComfyUI 本体

bash
cd "$COMFY_DIR"
git remote set-url origin https://github.com/Comfy-Org/ComfyUI.git
git fetch origin master
git switch master || git checkout -B master origin/master
git pull --ff-only origin master

如果 git pull 成功,继续下一步。

如果提示 .git/config 损坏,或者仓库状态异常,改用下文的“方式二:源码同步更新”。

5. 更新依赖

bash
cd "$COMFY_DIR"
"$PY_BIN" -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout 30

注意:更新本体后必须更新依赖,否则可能报下面的错误:

text
ModuleNotFoundError: No module named 'comfy_aimdo'

6. 重启 ComfyUI

bash
old_pid=$(ps -eo pid,args | awk '/[p]ython .*main.py/ {print $1; exit}')
[ -n "$old_pid" ] && kill "$old_pid" || true
sleep 3
kill -9 "$old_pid" 2>/dev/null || true

cd "$COMFY_DIR"
nohup env \
  HF_ENDPOINT=https://hf-mirror.com \
  PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple \
  "$PY_BIN" main.py --listen 0.0.0.0 --port "$PORT" \
  >> "$COMFY_DIR/user/comfyui-restart.out" 2>&1 < /dev/null &

echo "new_pid=$!"

7. 验证

bash
ss -ltnp | grep "$PORT"
curl -sS "http://127.0.0.1:$PORT/system_stats" | "$PY_BIN" -m json.tool | head -n 50

cd "$COMFY_DIR"
git fetch origin master
echo "HEAD=$(git rev-parse --short HEAD)"
echo "origin/master=$(git rev-parse --short origin/master)"
git log -1 --oneline --decorate

判断:

  • 端口在监听:服务已启动
  • system_stats 正常返回:ComfyUI 可用
  • HEADorigin/master 一致:master 已是最新

注意:不要只看 comfyui_version 数字。master / nightly 的版本号可能看起来比 stable 小,应以 git commit 为准。

方式二:源码同步更新

适用场景:原地 git 更新失败,或者 .git 仓库损坏。

优点:能绕过坏掉的 .git,重新同步 ComfyUI 程序代码。

1. 配置网络

这里的 https://gh-proxy.org 是示例代理地址,实际使用时换成当前机器可访问的 GitHub 镜像或代理地址。

bash
git config --global url."https://gh-proxy.org/https://github.com/".insteadOf "https://github.com/"

"$PY_BIN" -m pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
"$PY_BIN" -m pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn

cat > /etc/profile.d/comfyui-mirrors.sh <<'EOF'
export HF_ENDPOINT=https://hf-mirror.com
export PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple
EOF
chmod 0644 /etc/profile.d/comfyui-mirrors.sh

2. 克隆新版源码

bash
rm -rf /tmp/comfyui-src
git clone --depth 1 https://github.com/Comfy-Org/ComfyUI.git /tmp/comfyui-src
git -C /tmp/comfyui-src log -1 --oneline

如果这里失败,先确认 GitHub 代理是否生效:

bash
git config --global --get-regexp '^url\..*insteadOf$'
timeout 30 git ls-remote --heads https://github.com/Comfy-Org/ComfyUI.git master

3. 备份当前程序代码

bash
ts=$(date +%Y%m%d-%H%M%S)
cd /

tar -czf "/tmp/ComfyUI-code-backup-$ts.tgz" \
  --exclude=ComfyUI/models \
  --exclude=ComfyUI/output \
  --exclude=ComfyUI/input \
  --exclude=ComfyUI/temp \
  --exclude=ComfyUI/user \
  --exclude=ComfyUI/custom_nodes \
  ComfyUI

ls -lh "/tmp/ComfyUI-code-backup-$ts.tgz"

作用:只备份程序代码,不备份模型和输出,避免备份包过大。

4. 停止 ComfyUI

bash
old_pid=$(ps -eo pid,args | awk '/[p]ython .*main.py/ {print $1; exit}')
[ -n "$old_pid" ] && kill "$old_pid" || true
sleep 3
kill -9 "$old_pid" 2>/dev/null || true

ss -ltnp | grep "$PORT" || true

如果没有端口输出,说明已停止。

5. 同步新版程序代码

bash
rsync -az --delete --no-owner --no-group \
  --exclude=/models/ \
  --exclude=/output/ \
  --exclude=/input/ \
  --exclude=/temp/ \
  --exclude=/user/ \
  --exclude=/custom_nodes/ \
  --exclude=/extra_model_paths.yaml \
  /tmp/comfyui-src/ \
  "$COMFY_DIR/"

注意:排除规则要写 /models//user/ 这种根目录规则,避免误排除源码内部同名目录。

6. 更新依赖

bash
cd "$COMFY_DIR"
"$PY_BIN" -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout 30
"$PY_BIN" -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple brotlicffi

作用:补齐新版 ComfyUI 需要的依赖,并修复 gh-proxy.orgbr 解压问题。

7. 重新配置 ComfyUI-Manager

源码同步不会覆盖 user 目录,但建议重新确认 Manager 配置:

bash
"$PY_BIN" - <<'PY'
from pathlib import Path
import os

cfg = Path(os.environ["COMFY_DIR"]) / "user/__manager/config.ini"
updates = {
    "channel_url": "https://gh-proxy.org/https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main",
    "default_cache_as_channel_url": "True",
    "update_policy": "nightly-comfyui",
    "network_mode": "public",
}

lines = cfg.read_text().splitlines()
seen = set()
out = []

for line in lines:
    key = line.split("=", 1)[0].strip() if "=" in line else ""
    if key in updates:
        out.append(f"{key} = {updates[key]}")
        seen.add(key)
    else:
        out.append(line)

for key, value in updates.items():
    if key not in seen:
        out.append(f"{key} = {value}")

cfg.write_text("\n".join(out) + "\n")
PY

grep -q '^gh-proxy::' "$COMFY_DIR/user/__manager/channels.list" || \
echo 'gh-proxy::https://gh-proxy.org/https://raw.githubusercontent.com/ltdrdata/ComfyUI-Manager/main' \
  >> "$COMFY_DIR/user/__manager/channels.list"

8. 启动 ComfyUI

bash
cd "$COMFY_DIR"

nohup env \
  HF_ENDPOINT=https://hf-mirror.com \
  PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple \
  "$PY_BIN" main.py --listen 0.0.0.0 --port "$PORT" \
  >> "$COMFY_DIR/user/comfyui-restart.out" 2>&1 < /dev/null &

echo "new_pid=$!"

9. 验证

bash
ss -ltnp | grep "$PORT"
curl -sS "http://127.0.0.1:$PORT/system_stats" | "$PY_BIN" -m json.tool | head -n 50
tail -n 120 "$COMFY_DIR/user/comfyui-restart.out"

cd "$COMFY_DIR"
git log -1 --oneline --decorate

日志里看到下面内容说明启动成功:

text
Starting server
To see the GUI go to: http://0.0.0.0:8188

10. 回滚

如果新版本启动失败,用第 3 步生成的备份包回滚。把文件名换成实际备份包。

bash
old_pid=$(ps -eo pid,args | awk '/[p]ython .*main.py/ {print $1; exit}')
[ -n "$old_pid" ] && kill "$old_pid" || true
sleep 3

cd /
tar -xzf /tmp/ComfyUI-code-backup-YYYYMMDD-HHMMSS.tgz

cd "$COMFY_DIR"
"$PY_BIN" -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --default-timeout 30

nohup "$PY_BIN" main.py --listen 0.0.0.0 --port "$PORT" \
  >> "$COMFY_DIR/user/comfyui-restart.out" 2>&1 < /dev/null &

相关文档

智算无疆 川流不息