「Stable-Diffusion」Stable-Diffusion-WebUI-For-Linux-安装教程

背景

之前我们讲解了如何在Mac 上搭建 Stable-Diffusion-WebUI,但是因为仅能使用CPU的原因,生成图片特别慢。

本文讲解如何在一台有GPU的Linux服务器上部署。

配置服务器

首先我们在云服务器商申请购买一台服务器,GPU建议使用 12GB 以上显存的。

系统镜像我这里选择的是公共镜像CentOS-64位-CentOS 8.4 64位。一定要选择后台自动安装GPU驱动,避免自己手动安装GPU驱动。然后等待一会等系统安装好驱动后再进行安装stable-diffusion-webui

带宽计费方式可以选择按使用流量计费,带宽值建议拉到 100Mbps,因为安装时需要下载的依赖和模型非常多,非常大,后续我们可以再动态调配带宽。

查看显卡驱动

1
nvidia-smi

如果显示了一个表格,里面有NVIDIA、Driver Version、CUDA version等信息,则代表GPU驱动已经安装完成。

nvcc -V 也可以查看版本信息,在腾讯云中如果返回 nvcc: command not found 则需要配置一下环境变量。

进入环境配置

1
2
# 进入环境配置
vim ~/.bash_profile

添加 cuda 环境设置

1
2
3
4
# 在PATH 中添加 /usr/local/cuda/bin,如
PATH=$PATH:/usr/local/cuda/bin

export PATH

然后按 Esc 退出,输入 :wq 保存 bash_profile, 然后在输入

1
source ~/.bash_profile

安装 Anaconda

官方仓库下载

1
wget https://repo.anaconda.com/archive/Anaconda3-2023.03-Linux-x86_64.sh

通过清华镜像仓库下载

清华大学开源软件镜像站 Anaconda Archive 可以查看现在最新版本

1
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.03-Linux-x86_64.sh

如果遇到不能下载的,则在 wget后添加参数 wget -U NoSuchBrowser,完整如下:

1
wget -U NoSuchBrowser https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2023.03-Linux-x86_64.sh

下载后安装

1
sh Anaconda3-2023.03-Linux-x86_64.sh

提示enter按回车,提示yes or no的地方全部输入yes即可,安装完成后执行 “bash”命令,重新载入bash终端,确保conda命令可用。安装后还可以配置 Conda 镜像(参考文章:anaconda 镜像站使用帮助

验证安装,可以看到输出信息

1
conda info

安装 PyTorch

pytorch需要安装到anaconda的虚拟环境中,所以使用conda时需要先有一个虚拟环境并进入

1
2
3
4
5
#若还没有虚拟环境需要创建一个,<env_name>是环境名称,python选择指定的版本,sd需要的python>3.10 #conda create -n <env_name> python=x.xx.x
conda create -n sd python=3.10.11

#启动虚拟环境,activate就是启动,sd是环境名称
conda activate sd

去pytorch官网配置自己服务器所需要的pytorch:Start Locally | PyTorch

Pytorch build选择stable稳定版,操作系统是linux,由于上面安装了anaconda所以package选择conda,语言python,服务器上 CUDA是12.0所以选择了CUDA11.8

选择完成之后下面会有提示的执行代码(Run this Command:),执行pytorch的安装:

1
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

如果出现提示需要升级conda版本的就输入,没有的话就跳过这步。

1
conda update -n base -c defaults conda

验证安装结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#首先要启动我们的sd虚拟环境,因为我们所有与stable diffusion有关的配置环境都是建立在这个虚拟环境之下的python,pytorch等都是如此
#进入虚拟环境
conda activate sd

#进入python环境
python

#导入torch
import torch

#查看torch的版本
torch.__version__ #各有两条下划线

#查看cuda版本
torch.version.cuda

#查看GPU是否运行
torch.cuda.is_available()

#退出
exit()

安装 Stable Diffusion-Web-UI

依赖安装

1
2
sudo yum install mesa-libGL -y
sudo yum install git -y

下载 web-ui

1
bash <(wget -qO- https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh)

或者通过 git 命令下载(此处使用了镜像源)

1
git clone https://github.moeyy.xyz/https://github.com/AUTOMATIC1111/stable-diffusion-webui.git

如果提示错误 OpenSSL SSL_read: Connection was reset, errno 10054

则需要关闭 githttps 证书验证,然后重新执行下载

1
git config --global http.sslVerify false

预先配置

下载完成之后我们进行一些预先的配置,防止后面配置失败。

配置 pip

设置 pip 源,这里设置的是腾讯云内源

1
2
pip config set global.index-url http://mirrors.cloud.tencent.com/pypi/simple
pip config set global.trusted-host mirrors.cloud.tencent.com

更新 pip 版本

1
pip install --upgrade pip

配置 SD

链接换源。打开 stable-diffusion-webui/modules/launch.until.py 文件

1
torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==2.0.1 torchvision==0.15.2")

每个https://github.com/ (就在上一步的下面几行,每一个网址都要改)链接前面添加https://ghproxy.com/,替换为以下内容

1
2
3
4
5
6
7
8
gfpgan_package = os.environ.get('GFPGAN_PACKAGE', "https://ghproxy.com/https://github.com/TencentARC/GFPGAN/archive/8d2447a2d918f8eba5a4a01463fd48e45126a379.zip")
clip_package = os.environ.get('CLIP_PACKAGE', "https://ghproxy.com/https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip")
openclip_package = os.environ.get('OPENCLIP_PACKAGE', "https://ghproxy.com/https://github.com/mlfoundations/open_clip/archive/bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b.zip")
stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://ghproxy.com/https://github.com/Stability-AI/stablediffusion.git")
taming_transformers_repo = os.environ.get('TAMING_TRANSFORMERS_REPO', "https://ghproxy.com/https://github.com/CompVis/taming-transformers.git")
k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://ghproxy.com/https://github.com/crowsonkb/k-diffusion.git')
codeformer_repo = os.environ.get('CODEFORMER_REPO', 'https://ghproxy.com/https://github.com/sczhou/CodeFormer.git')
blip_repo = os.environ.get('BLIP_REPO', 'https://ghproxy.com/https://github.com/salesforce/BLIP.git')

配置用户

通常 SD 不允许使用 root 用户进行,我们需要额外创建一个用户

1
2
3
4
adduser sd  (创建账户sd)
passwd sd (创建密码)
usermod -aG wheel sd (增加sudo权限)
su - sd (切换为sd用户)

或者修改 stable-diffusion-webui 目录下的 webui.sh 文件,禁掉用户判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 修改 can_run_as_root 为 1
# this script cannot be run as root by default
can_run_as_root=1


# 或者找到下面的代码进行注释

# Do not run as root
# if [[ $(id -u) -eq 0 && can_run_as_root -eq 0 ]]
# then
# printf "\n%s\n" "${delimiter}"
# printf "\e[1m\e[31mERROR: This script must not be launched as root, aborting...\e[0m"
# printf "\n%s\n" "${delimiter}"
# exit 1
# else
printf "\n%s\n" "${delimiter}"
printf "Running on \e[1m\e[32m%s\e[0m user" "$(whoami)"
printf "\n%s\n" "${delimiter}"
# fi

启动

cd 到 stable-diffusion-webui目录下执行

1
./webui.sh --share
  • -- share 会启动启动公网环境,给出一个随机的地址,每次启动都会随机生成。因此建议使用 --listen 参数,再做映射转发,这点后续再讲

第一次执行会自动下载基础模型,如果下载速度还行就耐心等待,如果网络慢的同学,可以先“ctrl+C”断开下载步骤,然后到C站随便下个大模型上传上去。

命令行中也会有提示安装的目录,通常是放在 stable-diffusion-webui/models/Stable-diffusion 中

启动成功后,出现http:/0.0.0.0:7860就说明已启动成功,如果是用share命令还会映射一个随机的外网访问地址。

例如:https://9ac95a343d5d32d1c6.gradio.live/ ,访问即可

参考

基于腾讯云CVM搭建stable-diffusion-webui环境-腾讯云开发者社区-腾讯云 (tencent.com)

Stable Diffusion云服务器部署完整版教程 - 哔哩哔哩 (bilibili.com)