Linux上如何使用Stable Diffusion WebUI

语言: CN / TW / HK

theme: v-green highlight: agate


在我把所有的坑都踩了一遍之后,决定记录一下linux上的Stable Diffusion webui是怎么搞的。

image.png


前提条件:

  1. 已安装CUDA

  2. 已安装git

  3. 已安装Anaconda

直接安装Anaconda不要指望Linux自带的Python。虽然Linux自带的Python,但是缺胳膊少腿,所以还是直接用了conda。


捣鼓好Stable Diffusion WebUI需要的环境

创建并激活进入虚拟环境:

conda create -n webui python=3.10.6

conda activate webui

成功进入虚拟环境之后就可以开搞了。


下载Stable Diffusion WebUI

从github上下载,终端中输入:

git clone http://github.com/AUTOMATIC1111/stable-diffusion-webui.git

进入文件夹:

cd stable-diffusion-webui

为了减少你的麻烦,请做好以下几个铺垫步骤

pip换源

pip config set global.index-url <http://mirrors.aliyun.com/pypi/simple/>

pip config set global.trusted-host mirrors.aliyun.com

笔者提示: 安装过程中可能会遇到奇怪的问题,一般都是网络造成的,很大一部分是pip源造成的。

我搭的时候弄得阿里云源,疯狂报错。师兄搭的时候用的清华源,一下就成功了。我又配另一个服务器,阿里云全部成功。我俩复盘了一下,就是当时网络的问题。所以阿里云源不好使,多换几个别的。

image.png

修改lunch.py

找到下面这段代码,给每个github地址前边都加上http://ghproxy.com/

```py def prepare_environment(): global skip_install

torch_command = os.environ.get('TORCH_COMMAND', "pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url http://download.pytorch.org/whl/cu117")
requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")
commandline_args = os.environ.get('COMMANDLINE_ARGS', "")

xformers_package = os.environ.get('XFORMERS_PACKAGE', 'xformers==0.0.16rc425')
gfpgan_package = os.environ.get('GFPGAN_PACKAGE', "git+http://github.com/TencentARC/GFPGAN.git@8d2447a2d918f8eba5a4a01463fd48e45126a379")
clip_package = os.environ.get('CLIP_PACKAGE', "git+http://github.com/openai/CLIP.git@d50d76daa670286dd6cacf3bcd80b5e4823fc8e1")
openclip_package = os.environ.get('OPENCLIP_PACKAGE', "git+http://github.com/mlfoundations/open_clip.git@bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b")

stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "http://github.com/Stability-AI/stablediffusion.git")
taming_transformers_repo = os.environ.get('TAMING_TRANSFORMERS_REPO', "http://github.com/CompVis/taming-transformers.git")
k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'http://github.com/crowsonkb/k-diffusion.git')
codeformer_repo = os.environ.get('CODEFORMER_REPO', 'http://github.com/sczhou/CodeFormer.git')
blip_repo = os.environ.get('BLIP_REPO', 'http://github.com/salesforce/BLIP.git')

```

修改完成之后如图:

笔者提示: 刚才说安装过程中可能会遇到奇怪的问题,一般都是网络造成的,另一个原因就是从github下载东西的时候失败,所以这里直接加个代理省事。用梯子也不好使。

下载默认模型

笔者提示: 这一步不是必须的,webui.sh会自动运行下载的,但是我用服务器下的巨慢,所以手动下的。

下载 v1-5-pruned-emaonly.safetensors,放到stable-diffusion-webui/models/Stable-diffusion/里:

使用webui

终端输入启动webui:

bash webui.sh

出现URL就是成功! 点击链接用浏览器打开。


开始画图吧!

Give it a try yourself!


部分报错处理方法

一般都是网络问题,方法就是找到Traceback里边的Command,重新给它换个别的国内源,阿里云不行换清华、豆瓣等等。

  1. RuntimeError

    RuntimeError: Couldn't install torch.

    Command: "/home/Ann/stable-diffusion-webui/venv/bin/python3" -m pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url http://download.pytorch.org/whl/cu117

    Error code: 2

    下载源有问题,看一下你换源了么?已经换源的话不好使,那就再试一下别的源。

    command后边那块复制下来,改一改:

    pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --trusted-host mirrors.aliyun.com --extra-index-url http://download.pytorch.org/whl/cu117 -i 别的源的地址

  2. Preparing metadata (setup.py) ... error

    subprocess.CalledProcessError: Command '['/home/Ann/stable-diffusion-webui/venv/bin/python3', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpmlg_i0y8', '--quiet', 'cython']' returned non-zero exit status 2.

    distutils.errors.DistutilsError: Command '['/home/Ann/stable-diffusion-webui/venv/bin/python3', '-m', 'pip', '--disable-pip-version-check', 'wheel', '--no-deps', '-w', '/tmp/tmpmlg_i0y8', '--quiet', 'cython']' returned non-zero exit status 2.

    还是看Command。报错转化出来是这个命令:

    "/home/Ann/stable-diffusion-webui/venv/bin/python3" -m pip --disable-pip-version-check wheel --no-deps -w /tmp/tmpmlg_i0y8 --quiet cython

    表示在使用 pip 安装 cython 时,生成了一个 Wheel 文件。

    • 其中 /home/liuyx169/stable-diffusion-webui/venv/bin/python3 表示使用指定的 Python 解释器来执行 pip 命令。
    • -m pip 表示使用 pip 模块来执行命令。
    • --disable-pip-version-check 表示禁用 pip 版本检查
    • wheel 表示使用 Wheel 格式打包安装包
    • --no-deps 表示不安装依赖包
    • -w /tmp/tmpmlg_i0y8 表示将生成的 Wheel 文件放置在指定的目录下
    • --quiet 表示以安静模式运行,不输出详细信息
    • cython 表示要安装的 Python 包的名称。

    归根结底还是在安装cythom这个包的时候没装上,还是网的问题。

    如果你确定换源了,那解决方法还是再换个源手动装一下:

    pip install cython -i 别的源的地址 --disable-pip-version-check wheel --no-deps

  3. pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

    还是源的问题。找到Traceback里边的Command再换个源。

  4. The TLS connection was non-properly terminated.

    fatal: unable to access 'http://github.com/TencentARC/GFPGAN.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated. error: subprocess-exited-with-error

    出现这个问题,回去看一下你的launch.py改对了么?确定给launch.py所有的github地址前边都加上http://ghproxy.com/ 即可。

  5. 还有什么解决不了的尽管往评论区丢,我给你解决

附录

  1. Stable Diffusion Webui:AUTOMATIC1111/stable-diffusion-webui: Stable Diffusion web UI (github.com)
  2. Stable Diffusion Webui 文档:Home · AUTOMATIC1111/stable-diffusion-webui Wiki (github.com)

本文正在参加「金石计划」