安装与配置 #
硬件要求 #
GPU 要求 #
Stable Diffusion 对 GPU 有一定要求,主要需要 NVIDIA 显卡支持 CUDA。
text
┌─────────────────────────────────────────────────────────────┐
│ GPU 配置建议 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 最低配置(可运行 SD 1.5) │
│ ├── GPU: NVIDIA GTX 1060 6GB / AMD RX 580 8GB │
│ ├── 显存: 6GB │
│ └── 生成速度: 约 30秒/张 │
│ │
│ 推荐配置(流畅运行 SDXL) │
│ ├── GPU: NVIDIA RTX 3060 12GB / RTX 4060 │
│ ├── 显存: 12GB │
│ └── 生成速度: 约 10秒/张 │
│ │
│ 专业配置(训练和高负载) │
│ ├── GPU: NVIDIA RTX 4090 / A100 │
│ ├── 显存: 24GB+ │
│ └── 生成速度: 约 3秒/张 │
│ │
└─────────────────────────────────────────────────────────────┘
显存与功能对照表 #
| 显存 | SD 1.5 | SDXL | 训练 LoRA | ControlNet | 批量生成 |
|---|---|---|---|---|---|
| 4GB | ✅ 低配 | ❌ | ❌ | ❌ | ❌ |
| 6GB | ✅ | ⚠️ 优化 | ❌ | ⚠️ | ❌ |
| 8GB | ✅ | ✅ | ⚠️ | ✅ | ⚠️ |
| 12GB | ✅ | ✅ | ✅ | ✅ | ✅ |
| 16GB+ | ✅ | ✅ | ✅ | ✅ | ✅ |
其他硬件要求 #
text
内存(RAM):
├── 最低: 16GB
├── 推荐: 32GB
└── 训练: 64GB+
存储:
├── 类型: SSD(推荐 NVMe)
├── 空间: 至少 50GB
└── 模型库: 建议 200GB+
操作系统:
├── Windows 10/11 ✅
├── Linux (Ubuntu 20.04+) ✅
└── macOS (M1/M2) ⚠️ 需要特殊配置
安装方式选择 #
主流安装方案对比 #
| 方案 | 难度 | 灵活性 | 适合人群 |
|---|---|---|---|
| 整合包 | 低 | 中 | 新手入门 |
| WebUI 源码安装 | 中 | 高 | 进阶用户 |
| ComfyUI | 中 | 极高 | 专业用户 |
| Docker | 中 | 高 | 开发者 |
方案一:整合包安装(推荐新手) #
Windows 整合包 #
最简单的安装方式,一键安装所有依赖。
text
推荐整合包:
1. 秋叶整合包(国内推荐)
├── 下载: B站搜索"秋叶Stable Diffusion整合包"
├── 特点: 国内优化,下载快
└── 包含: WebUI + 常用插件 + 模型
2. Stability Matrix
├── 下载: https://github.com/LykosAI/StabilityMatrix
├── 特点: 多版本管理
└── 包含: WebUI + ComfyUI 管理器
安装步骤:
├── 1. 下载整合包压缩文件
├── 2. 解压到非中文路径(如 D:\SD)
├── 3. 运行启动脚本
└── 4. 等待自动打开浏览器
整合包使用注意事项 #
text
注意事项:
1. 路径问题
├── 不要使用中文路径
├── 不要有空格
└── 示例: D:\StableDiffusion ✅
D:\我的文件\SD ❌
2. 显卡驱动
├── 更新到最新 NVIDIA 驱动
└── 确认 CUDA 可用
3. 杀毒软件
├── 可能误报,添加信任
└── 或临时关闭
方案二:WebUI 源码安装 #
环境准备 #
text
┌─────────────────────────────────────────────────────────────┐
│ 环境准备清单 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 1. Python 3.10.x │
│ ├── 下载: python.org │
│ ├── 注意: 勾选 "Add to PATH" │
│ └── 验证: python --version │
│ │
│ 2. Git │
│ ├── 下载: git-scm.com │
│ └── 验证: git --version │
│ │
│ 3. CUDA Toolkit(可选) │
│ ├── 版本: 11.8 或 12.x │
│ └── PyTorch 会自动安装 CUDA │
│ │
└─────────────────────────────────────────────────────────────┘
Windows 安装步骤 #
powershell
# 1. 克隆仓库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
# 2. 进入目录
cd stable-diffusion-webui
# 3. 运行启动脚本(首次会自动安装依赖)
webui-user.bat
# 4. 等待安装完成,自动打开浏览器
# 默认地址: http://127.0.0.1:7860
Linux 安装步骤 #
bash
# 1. 安装依赖
sudo apt update
sudo apt install -y python3.10-venv git
# 2. 克隆仓库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
# 3. 进入目录
cd stable-diffusion-webui
# 4. 运行启动脚本
./webui.sh
# 5. 访问 WebUI
# 默认地址: http://127.0.0.1:7860
macOS (Apple Silicon) 安装 #
bash
# 1. 安装 Homebrew(如未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 2. 安装依赖
brew install cmake protobuf rust python@3.10 git
# 3. 克隆仓库
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
# 4. 进入目录
cd stable-diffusion-webui
# 5. 创建虚拟环境
python3.10 -m venv venv
source venv/bin/activate
# 6. 安装 PyTorch(MPS 加速)
pip install torch torchvision torchaudio
# 7. 运行
./webui.sh --no-half --use-cpu all --skip-torch-cuda-test
启动参数配置 #
创建或编辑 webui-user.bat(Windows)或 webui-user.sh(Linux):
bash
# 常用启动参数
# 基本配置
--xformers # 使用 xformers 加速(推荐)
--medvram # 中等显存优化(8GB 显存)
--lowvram # 低显存优化(6GB 显存)
# 网络配置
--listen # 允许局域网访问
--port 7860 # 指定端口
--share # 创建公网链接
# 模型配置
--ckpt-dir "D:\models" # 自定义模型目录
--lora-dir "D:\loras" # 自定义 LoRA 目录
# API 配置
--api # 启用 API
--api-auth user:pass # API 认证
# 示例配置
set COMMANDLINE_ARGS=--xformers --medvram --listen --api
方案三:ComfyUI 安装 #
什么是 ComfyUI? #
ComfyUI 是一个基于节点的 Stable Diffusion 界面,提供更高的灵活性和控制力。
text
┌─────────────────────────────────────────────────────────────┐
│ WebUI vs ComfyUI │
├─────────────────────────────────────────────────────────────┤
│ │
│ WebUI (Automatic1111) │
│ ├── 优点: 界面友好,上手简单 │
│ ├── 缺点: 灵活性较低 │
│ └── 适合: 初学者,日常使用 │
│ │
│ ComfyUI │
│ ├── 优点: 节点式,高度灵活 │
│ ├── 缺点: 学习曲线陡峭 │
│ └── 适合: 专业用户,复杂工作流 │
│ │
└─────────────────────────────────────────────────────────────┘
ComfyUI 安装步骤 #
bash
# 1. 克隆仓库
git clone https://github.com/comfyanonymous/ComfyUI.git
# 2. 进入目录
cd ComfyUI
# 3. 创建虚拟环境
python -m venv venv
# Windows 激活
venv\Scripts\activate
# Linux/macOS 激活
source venv/bin/activate
# 4. 安装依赖
pip install -r requirements.txt
# 5. 安装 PyTorch(CUDA)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# 6. 运行
python main.py
# 访问: http://127.0.0.1:8188
ComfyUI Manager 安装 #
ComfyUI Manager 是一个扩展管理器,方便安装和管理节点:
bash
# 进入 ComfyUI 自定义节点目录
cd ComfyUI/custom_nodes
# 克隆 Manager
git clone https://github.com/ltdrdata/ComfyUI-Manager.git
# 重启 ComfyUI,侧边栏会出现 Manager 按钮
模型下载与配置 #
模型类型 #
text
模型类型说明:
1. Checkpoint(主模型)
├── 文件大小: 2GB - 6GB
├── 存放位置: models/Stable-diffusion/
└── 用途: 基础生成能力
2. LoRA(微调模型)
├── 文件大小: 50MB - 200MB
├── 存放位置: models/Lora/
└── 用途: 风格/角色微调
3. VAE(变分自编码器)
├── 文件大小: ~300MB
├── 存放位置: models/VAE/
└── 用途: 改善颜色
4. Embedding(文本嵌入)
├── 文件大小: ~20KB
├── 存放位置: embeddings/
└── 用途: 负向提示词
推荐模型 #
text
基础模型推荐:
SD 1.5 系列:
├── v1-5-pruned.ckpt(官方)
├── realisticVisionV51(写实风格)
├── deliberate_v3(通用风格)
└── anything-v5(动漫风格)
SDXL 系列:
├── sd_xl_base_1.0.safetensors(官方)
├── juggernautXL(写实风格)
└── dreamshaperXL(通用风格)
SD 3 系列:
├── sd3_medium_incl_clips.safetensors
└── sd3.5_large_incl_clips_t5xxlfp8.safetensors
模型下载来源 #
text
模型下载平台:
1. Civitai(推荐)
├── 网址: civitai.com
├── 特点: 模型丰富,有预览图
└── 国内访问: 可能需要代理
2. Hugging Face
├── 网址: huggingface.co
├── 特点: 官方模型,稳定可靠
└── 国内访问: hf-mirror.com
3. LiblibAI(国内)
├── 网址: liblib.art
├── 特点: 国内访问快
└── 模型: 国内模型为主
模型配置 #
text
目录结构:
stable-diffusion-webui/
├── models/
│ ├── Stable-diffusion/ # 主模型
│ │ ├── sd_v1.5.safetensors
│ │ └── sd_xl_base.safetensors
│ ├── Lora/ # LoRA 模型
│ │ └── style_lora.safetensors
│ ├── VAE/ # VAE 模型
│ │ └── vae-ft-mse.safetensors
│ └── ControlNet/ # ControlNet 模型
│ └── control_v11p_sd15_canny.safetensors
├── embeddings/ # 文本嵌入
│ └── EasyNegative.safetensors
└── outputs/ # 输出目录
常见问题解决 #
CUDA 相关问题 #
text
问题: CUDA out of memory
解决方案:
1. 降低生成分辨率
2. 使用 --medvram 或 --lowvram 参数
3. 减少 batch size
4. 启用 xformers
启动参数:
set COMMANDLINE_ARGS=--medvram --xformers
模型加载问题 #
text
问题: 模型加载失败
解决方案:
1. 检查文件完整性(重新下载)
2. 确认文件格式(.safetensors 或 .ckpt)
3. 检查文件路径是否正确
4. 确认模型版本与 SD 版本匹配
依赖安装问题 #
text
问题: pip 安装失败
解决方案:
1. 使用国内镜像
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
2. 手动安装 PyTorch
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121
3. 清除缓存重试
pip cache purge
生成速度慢 #
text
问题: 生成速度太慢
优化方案:
1. 启用 xformers
--xformers
2. 使用更快的采样器
DPM++ 2M Karras
3. 减少采样步数
20-30 步通常足够
4. 使用 TensorRT 加速
安装 TensorRT 扩展
配置验证 #
检查 GPU 状态 #
python
# 在 Python 中验证 CUDA
import torch
print(f"PyTorch 版本: {torch.__version__}")
print(f"CUDA 可用: {torch.cuda.is_available()}")
print(f"CUDA 版本: {torch.version.cuda}")
print(f"GPU 数量: {torch.cuda.device_count()}")
if torch.cuda.is_available():
print(f"GPU 名称: {torch.cuda.get_device_name(0)}")
print(f"显存总量: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB")
WebUI 检查 #
text
验证清单:
1. 页面能正常打开
└── http://127.0.0.1:7860
2. 模型能正常加载
└── 左上角显示模型名称
3. 能成功生成图像
└── 使用默认参数测试
4. 显存使用正常
└── 任务管理器查看 GPU 使用
下一步 #
环境配置完成后,继续学习 快速开始,生成你的第一张 AI 图像!
最后更新:2026-04-05