0%

ubuntu 下安装原生 stable diffusion webui

OS、GPU等版本确认

Ubuntu 22.04.3 LTS + NVIDIA GeForce RTX 3090

更新阿里云加速源 [可选]

Ubuntu 22.04.3 LTS jammy

将加速源更新至 /etc/apt/sources.list

1
2
3
4
5
6
7
8
9
10
echo 'deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse' >/etc/apt/sources.list

获取OS版本

1
2
3
4
5
6
7
$ lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy

查看显卡型号

系统安装了 NVIDIA GeForce RTX 3090 显卡,推荐安装的驱动程序是 nvidia-driver-535

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ sudo ubuntu-drivers devices

== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00002204sv00007377sd0000120Bbc03sc00i00
vendor : NVIDIA Corporation
model : GA102 [GeForce RTX 3090]
driver : nvidia-driver-535-server - distro non-free
driver : nvidia-driver-525-server - distro non-free
driver : nvidia-driver-535-open - distro non-free
driver : nvidia-driver-525 - distro non-free
driver : nvidia-driver-525-open - distro non-free
driver : nvidia-driver-470-server - distro non-free
driver : nvidia-driver-470 - distro non-free
driver : nvidia-driver-535-server-open - distro non-free
driver : nvidia-driver-535 - distro non-free recommended
driver : xserver-xorg-video-nouveau - distro free builtin

安装驱动和CUDA

因SD WebUI的webui.sh中指定的xFormers版本需要PyTorch 2.0.1+cu118,需要确保正确安装CUDA v11.8

通过 CUDA Toolkit 11.8 Downloads | NVIDIA Developer 检索发现 CUDA v11.8 配对的驱动最低为v520,且页面有对应的安装包地址

卸载错误的驱动(可选)

1
2
sudo apt-get remove --purge nvidia*
sudo apt autoremove

卸载错误的CUDA(可选)

1
sudo apt-get --purge remove "*cublas*" "cuda*"

若有卸载操作,执行以下安装前建议reboot

安装正确的驱动

如果接受 ubuntu-drivers 给到的驱动版本建议不低于v520,可以直接使用 ubuntu-drivers 进行安装

1
$ sudo ubuntu-drivers autoinstall

或者,使用命令有选择地安装所需的驱动程序,如

1
$ sudo apt install nvidia-driver-535

安装正确的CUDA

1
2
wget wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
sudo sh cuda_11.8.0_520.61.05_linux.run

完成安装后在~/.bashrc里加入环境变量:

1
2
export PATH="/usr/local/cuda-12.1/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-12.1/lib64:$LD_LIBRARY_PATH"
异常处理

仍有其他方式安装的CUDA,nvcc -V仍有输出,如:

1
2
3
4
5
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Jun__8_16:49:14_PDT_2022
Cuda compilation tools, release 11.7, V11.7.99
Build cuda_11.7.r11.7/compiler.31442593_0

可按以下方式继续尝试清理CUDA:

1
2
3
4
5
6
7
8
9
10
11
12
#进入cuda的安装目录
cd /usr/local/cuda/bin
#执行cuda自带的卸载程序
sudo ./cuda-uninstaller

#清理目录残余
# sudo rm -fr /usr/local/cuda-*
#清理安装包残余
# 检索安装包 sudo dpkg -l |grep cuda

#清理后重启
sudo reboot

CUDA包附带的显卡驱动不匹配:

sh cuda_x.x.x_y.y.y_linux.run时,提示Installation failed. See log at /var/log/cuda-installer.log for details.,而cuda-installer.log明确提示[ERROR]: Install of driver component failed.

因为funfile中带的驱动不兼容当前显卡,建议按sudo ubuntu-drivers devices给出的清单,选择大于y.y.y版本的驱动指定安装,如:

1
sudo apt install nvidia-driver-535

重启系统

安装完成后,重新启动系统即可完成。

1
sudo reboot

检查驱动和CUDA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 检查显卡驱动版本
# NVIDIA Driver v535.146.02
$ nvidia-smi
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.146.02 Driver Version: 535.146.02 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 3090 Off | 00000000:01:00.0 Off | N/A |
| 0% 32C P8 18W / 350W | 26MiB / 24576MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 1257 G /usr/lib/xorg/Xorg 9MiB |
| 0 N/A N/A 1414 G /usr/bin/gnome-shell 8MiB |
+---------------------------------------------------------------------------------------+

# 检查CUDA版本
# CUDA v12.1
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Tue_Feb__7_19:32:13_PST_2023
Cuda compilation tools, release 12.1, V12.1.66
Build cuda_12.1.r12.1/compiler.32415258_0

安装SD WebUI

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# ENV
# GPU::GeForce RTX 3090
# OS::Ubuntu Server 22.04 LTS 64位

# update ubuntu package 安装依赖包
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install linux-headers-$(uname -r)
sudo apt-get build-dep gcc
sudo apt-get install build-essential

gcc --version

# install conda 安装 conda
# latest:https://docs.conda.io/projects/conda/en/stable/user-guide/install/linux.html
wget https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh
bash Anaconda3-2023.09-0-Linux-x86_64.sh conda

# 回车&&yes
bash

# 升级最新conda
conda update conda

# 升级最新anaconda
conda update anaconda

# create env 创建环境
conda create -n sd python=3.10
conda activate sd

# show env 查看环境
conda info -e

# delete env 删除环境
# conda remoave -n sd --all

# download sd 下载 webui
# git clone https://ghproxy.com/https://github.com/AUTOMATIC1111/stable-diffusion-webui # 连不上用代理
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
cd stable-diffusion-webui

# accelerate pytorch torch 下载过慢可使用腾讯源
# pip install -i https://mirrors.cloud.tencent.com/pypi/simple torch
# 阿里:http://mirrors.aliyun.com/pypi/simple/
# 豆瓣:http://pypi.douban.com/simple/
# 清华:https://pypi.tuna.tsinghua.edu.cn/simple/
# 中科大:https://pypi.mirrors.ustc.edu.cn/simple/
# 中科大:http://pypi.mirrors.ustc.edu.cn/simple/

# install depend 安装依赖
# torchvision xformers相关的问题较多,先清理
pip uninstall torch torchvision xformers torchaudio -y
sudo apt-get install libgoogle-perftools4 libtcmalloc-minimal4 -y
# pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# pip install xformers

############################################################################################
## --xformers 一个可选的参数,它会启用Codeformer功能,这是一个可以生成和优化代码的人工智能模型。
## --listen 一个可选的参数,它会使服务器监听网络连接,这样本地网络或互联网上的其他计算机就可以访问Web UI。
## --port 一个可选的参数,它会指定服务器监听的端口号。默认值是7860,但如果该端口已被占用,可以使用其他端口。
## --share 一个可选的参数,它会使用Gradio的共享功能,生成一个可以从外部网络访问的URL。这是在Colab等在线平台上使用Web UI的一种方法。
## --enable-insecure-extension-access 一个可选的参数,它会允许Web UI访问一些不安全的扩展程序,例如运行自定义代码或使用ngrok。
## --disable-safe-unpickle 一个可选的参数,它会禁用对PyTorch模型的恶意代码检查。这可能会提高加载模型的速度,但也会增加安全风险。
############################################################################################
bash webui.sh --listen --xformers --port 7860 --enable-insecure-extension-access

异常处理

Style database not found: /data/AI/stable-diffusion-webui/styles.csv

styles.csv 下载后放入提示位置即可。

ImportError: Using SOCKS proxy

如果bash webui.sh运行中出现错误提示,证明当前session环境变量设置了代理,在运行webui.sh前先运行unset https_proxy http_proxy all_proxy

1
ImportError: Using SOCKS proxy, but the 'socksio' package is not installed. Make sure to install httpx using `pip install httpx[socks]`.

Cannot locate TCMalloc (improves CPU memory usage)

如果出现红字错误提醒:Cannot locate TCMalloc (improves CPU memory usage),是因为系统缺少 libgoogle-perftools4 和 libtcmalloc-minimal4 这两个库,它们可以提高 CPU 的内存使用效率。可以通过以下命令安装它们:

1
sudo apt-get install libgoogle-perftools4 libtcmalloc-minimal4 -y

no module ‘xformers’. Processing without…

若出现以下提示,在启动命令添加--xformers参数,以强制使用xformers模块:bash webui.sh --xformers

1
2
3
4
Launching Web UI with arguments:
no module 'xformers'. Processing without...
no module 'xformers'. Processing without...
No module 'xformers'. Proceeding without it.

Running on local URL: http://127.0.0.1:7860

如果监听端口需要改为0.0.0.0,在启动命令添加--listen参数,将监听改为0.0.0.0bash webui.sh --listen

安装双语插件

由于SD WebUI的扩充功能发展太快,翻译可能跟不上,建议另外安装双语对照 sd-webui-bilingual-localization,同时显示中文和英文的文本,这样看教学时就不会找无按钮了。

双语扩充功能安装方法:在Extensions页面按Install from URL,填入https://github.com/journey-ad/sd-webui-bilingual-localization,再按Install

在激活双语扩充功能前,要到Settings → User interface → Localization设为None再重启WebUI才会生效。

AssertionError: extension access disabled because of command line flags

如果安装插件时提示:AssertionError: extension access disabled because of command line flags,以在启动 WebUI 的时候添加一个参数 --enable-insecure-extension-access,或者在 webui-user.sh 的 export COMMANDLINE_ARGS 参数中增加以下参数 --enable-insecure-extension-access

常用 model

主模型 Stable-diffusion

SD1.5 [基础模型,生成各种风格的图像]

1
wget https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt -P models/Stable-diffusion/

pastel-Mix [生成柔和色彩的二次元风格的图像]

1
wget https://civitai.com/api/download/models/6297 -O models/Stable-diffusion/pastelMixStylizedAnime_pastelMixPrunedFP16.safetensors

MIX-Pro-V3 [生成写实风格的图像,适合人物、风景、动物等]

1
wget https://civitai.com/api/download/models/8511 -O models/Stable-diffusion/mixProV3_v3.safetensors

chilloutmix [生成清新风格的图像,适合人物、风景、动物等]

1
wget https://civitai.com/api/download/models/11745 -O models/Stable-diffusion/chilloutmix_NiPrunedFp32Fix.safetensors

Cetus-Mix [生成海洋生物和水下风景的图像]

1
wget https://civitai.com/api/download/models/18496 -O models/Stable-diffusion/cetusMix_cetusVersion3.safetensors

MeinaMix [生成美女风格的图像,适合人物、服装、化妆等]

1
wget https://civitai.com/api/download/models/16925 -O models/Stable-diffusion/meinamix_meinaV7.safetensors

Counterfeit-V2.5 [生成仿真风格的图像,适合人物、风景、动物等]

1
wget https://civitai.com/api/download/models/7425 -O models/Stable-diffusion/CounterfeitV25_25.safetensors

ligneClaireAnime-V1 [生成线条清晰的二次元风格的图像]

1
wget https://civitai.com/api/download/models/4279 -O models/Stable-diffusion/ligneClaireAnime_v1.safetensors

AnythingAndEverything [生成各种风格的图像,是一个通用的模型]

1
wget https://civitai.com/api/download/models/8365 -O models/Stable-diffusion/anythingAndEverything_anythingAndEverythingVer.safetensors

VAE模型 滤镜和微调使用

Pastel-Waifu-Diffusion [增加柔和色彩的效果]

1
wget https://civitai.com/api/download/models/6297?type=VAE -O models/VAE/pastel-waifu-diffusion.vae.pt

kl-f8-anime2 [增加二次元风格的效果]

1
wget https://civitai.com/api/download/models/8511?type=VAE -O models/VAE/kl-f8-anime2.ckpt

Lora模型 微调使用

moxin [固定墨心风格的效果]

1
wget https://civitai.com/api/download/models/14856 -O models/Lora/Moxin_10.safetensors

fashion-gir [固定时尚女孩风格的效果]

1
wget https://civitai.com/api/download/models/18070 -O models/Lora/fashionGirl_v47.safetensors

line [固定线条风格的效果]

1
wget https://civitai.com/api/download/models/19075 -O models/Lora/animeLineartStyle_v20Offset.safetensors

eyes [固定眼睛风格的效果]

1
2
wget https://civitai.com/api/download/models/6433 -O models/Lora/eyeLora_eyesV10.safetensors
wget https://civitai.com/api/download/models/19859 -O models/Lora/animeTarotCardArtStyleLora_v20Offset.safetensors

控制模型

control_canny [增加边缘检测的效果]

1
wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_canny-fp16.safetensors -P extensions/sd-webui-controlnet/models/

control_depth [增加深度检测的效果]

1
wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_depth-fp16.safetensors -P extensions/sd-webui-controlnet/models/

control_openpose [增加人体姿态检测的效果]

1
wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_openpose-fp16.safetensors -P extensions/sd-webui-controlnet/models/

control_hed [增加全局边缘检测的效果]

1
wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_hed-fp16.safetensors -P extensions/sd-webui-controlnet/models/

control_mlsd [增加多尺度线条检测的效果]

1
wget https://huggingface.co/webui/ControlNet-modules-safetensors/resolve/main/control_mlsd-fp16.safetensors -P extensions/sd-webui-controlnet/models/