Linux下Stable Diffusion部署方法

AI技术
小华
2025-12-31

Linux下Stable Diffusion部署方法
一 环境准备与系统要求

  • 操作系统:优先选择 Ubuntu 20.04/22.04 LTSCentOS 7+,内核与驱动支持更好。
  • Python:建议使用 Python 3.10(可用 Miniconda 管理)。
  • GPU与驱动:NVIDIA 显卡建议 显存≥8GB(如 T4/V100/A10),安装匹配版本的 NVIDIA 驱动、CUDA、cuDNN
  • 资源与存储:内存至少 8GB(推荐16GB+),可用磁盘 ≥200GB(模型与缓存占用较大)。
  • 基础工具:安装 Docker(便于容器化部署与隔离环境)。

二 方式一 Docker快速部署 WebUI

  • 安装 Docker(Ubuntu示例):
  • 卸载旧版本(如有)→ 安装依赖 → 添加 Docker GPG 与仓库 → 安装引擎 → 验证:
  • 命令示例:
  • sudo apt-get remove docker docker-engine docker.io containerd runc
  • sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl gnupg lsb-release
  • curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  • echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /devref
  • sudo apt-get update && sudo apt-get install -y docker-ce docker-ce-cli containerd.io
  • sudo docker run --rm hello-world
  • 拉取并运行 AUTOMATIC1111/stable-diffusion-webui(GPU):
  • 建议将本地模型目录挂载到容器内(示例挂载到 /models):
  • docker run -d --gpus all -p 7860:7860 -v /path/to/models:/models automatic1111/stable-diffusion-webui
  • 访问地址:http://服务器IP:7860
  • 无公网IP或需脚本化一键部署时,可使用云端提供的安装脚本(示例开放 8860 端口):
  • curl https://bcets.cdn.bcebos.com/sd-server-install-ubuntu.sh | bash
  • 完成后按提示访问 http://IP:8860,并确保安全组/防火墙放行对应端口。

三 方式二 手动安装 WebUI(便于定制与调试)

  • 安装 Miniconda 并创建环境:
  • wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
  • bash Miniconda3-latest-Linux-x86_64.sh && source ~/.bashrc
  • conda create -n sd_env python=3.10 && conda activate sd_env
  • 克隆仓库并启动:
  • git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
  • cd stable-diffusion-webui && ./webui.sh
  • 模型放置与加载:
  • .ckpt / .safetensors 模型放入:~/stable-diffusion-webui/models/Stable-diffusion/,重启 WebUI 即可识别。
  • 常见启动参数(在 webui.sh 前追加或写入 COMMANDLINE_ARGS):
  • 显存优化:--medvram / --lowvram
  • 指定端口:--port 7860
  • 其他常用:--skip-torch-cuda-test(仅当驱动/测试不通过时临时使用)

四 方式三 手动安装 推理与LoRA训练(diffusers方案)

  • 创建环境并安装依赖(会自动匹配 GPU 版本的 PyTorch 与相关库):
  • conda create -n sd_env python=3.10 && conda activate sd_env
  • pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
  • pip install diffusers transformers accelerate ftfy safetensors
  • pip install peft # LoRA 训练必备
  • 推理最小示例(Python):
  • from diffusers import StableDiffusionPipeline

import torch
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda")
image = pipe("a photo of a cat on the moon").images[0]
image.save("cat.png")

  • LoRA 训练要点:
  • 准备结构化数据集(图片+标注/说明),使用 PEFT + diffusers 的训练脚本进行低秩微调。
  • 训练完成后保存 LoRA 权重,在 WebUI 或推理脚本中加载以生成定制化结果。

五 常见问题与优化

  • GPU未生效或显存不足:
  • 确认 nvidia-smi 可见 GPU,PyTorch 能识别 CUDA;必要时添加启动参数 --medvram/--lowvram,并降低分辨率(如 512×512 → 256×256)。
  • 模型加载失败:
  • 检查模型文件路径是否包含 中文/特殊字符,校验文件完整性(如 MD5),必要时重新下载。
  • 端口无法访问:
  • 云服务器需放行安全组/防火墙(如 78608860);本地防火墙如 ufw 需允许对应端口。
  • 依赖安装问题:
  • 安装 protobufsentencepiece 等缺失库;若 datasets 构建失败,优先安装预编译 wheel:pip install --upgrade pip setuptools wheel && pip install pyarrow --only-binary=:all: && pip install datasets。
  • 资源建议:
  • 显存 ≥8GB 更流畅;内存 ≥16GB 更稳;磁盘 ≥200GB 便于多模型与缓存。
亿速云提供售前/售后服务

售前业务咨询

售后技术保障

400-100-2938

7*24小时售后电话

官方微信小程序