Linux上安装Stable Diffusion的可行方案
一 方案总览与准备
- 推荐在Ubuntu 22.04/20.04或CentOS 7/8上使用AUTOMATIC1111/stable-diffusion-webui脚本化部署,图形界面默认端口为7860。硬件建议:至少16GB内存、10GB+ SSD,有NVIDIA GPU更佳(显存≥4GB)。基础工具准备:安装git、Python 3.10.6、venv/Anaconda;NVIDIA 环境建议安装匹配版本的驱动、CUDA(如11.8)与cuDNN,并确保驱动与CUDA版本匹配。可用
nvidia-smi与nvcc --version检查环境;如需容器化,可准备Docker与NVIDIA Container Toolkit。
二 Ubuntu本地部署步骤
- 安装基础依赖
- sudo apt update && sudo apt install -y git python3 python3-pip python3-venv
- 获取源码
- git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
- cd stable-diffusion-webui
- 创建虚拟环境(推荐)
- python3 -m venv venv && source venv/bin/activate
- pip install -U pip
- 启动安装与运行
- 首次运行:./webui.sh(会自动安装依赖;网络慢可配置镜像或代理)
- 启用加速:./webui.sh --xformers(需与PyTorch/CUDA版本匹配)
- 访问界面
- 本机:http://127.0.0.1:7860
- 其他设备:http://服务器IP:7860(注意放行防火墙端口,如 sudo ufw allow 7860/tcp)
- 备注
- 若以root运行被拒,可使用:./webui.sh -f
- 若提示缺少TCMalloc:sudo apt install -y libgoogle-perftools4 libtcmalloc-minimal4
三 CentOS本地部署步骤
- 安装基础工具
- sudo yum install -y git
- 安装Python 3.10.6(示例编译安装)
- 下载并解压Python-3.10.6.tgz,执行:./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl --with-openssl-rpath=auto
- make -j$(nproc) && sudo make install
- 建立软链:ln -sfn /usr/local/python3/bin/python3.10 /usr/bin/python3;ln -sfn /usr/local/python3/bin/pip3.10 /usr/bin/pip3
- 可选:安装Miniconda/Anaconda以管理环境
- 获取源码并启动
- git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
- cd stable-diffusion-webui
- 使用venv或conda创建环境后,运行:./webui.sh(首次会自动安装依赖)
- 访问与防火墙
- 默认端口7860;如需远程访问,开放对应端口(firewalld/ufw)
四 Docker与容器化部署
- 安装Docker与NVIDIA Container Toolkit(略,按官方指引)
- 拉取并运行镜像(示例)
- docker pull edwardbaldo/stable-diffusion-webui:latest
- docker run -it --gpus all -p 8080:8080 --name sd-webui edwardbaldo/stable-diffusion-webui:latest /bin/bash
- 进入容器后执行:/start.sh
- 访问:http://服务器IP:8080
- 提示
- 端口可自定义映射;确保宿主机已安装nvidia-docker2并正确配置GPU可见性
五 常见问题与排查
- 版本兼容
- 确认Python 3.10.6、PyTorch 2.x、xformers与CUDA 11.8的匹配;用以下代码自检:
- python - <<'PY'
import torch
print("torch:", torch.__version__, "cuda:", torch.cuda.is_available())
print("cudnn:", torch.backends.cudnn.version())
print("device:", torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
PY
- 网络与模型下载
- 若出现HuggingFace连接失败(如CLIP tokenizer无法加载),可手动下载所需文件到缓存/指定目录,或修改相关路径配置后再启动。
- 常见报错
- “Cannot locate TCMalloc”:sudo apt install -y libgoogle-perftools4 libtcmalloc-minimal4
- “This script must not be launched as root, aborting”:使用./webui.sh -f
- “RuntimeError: GET was unable to find an engine to execute this computation”:检查torch/cuda/python版本兼容与GPU可用性
- 显存不足
- 降低分辨率(如768→512)、启用--xformers、使用--medvram或--lowvram参数;必要时改用更轻量的模型