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 https://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地址前邊都加上https://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 https://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+https://github.com/TencentARC/[email protected]")
clip_package = os.environ.get('CLIP_PACKAGE', "git+https://github.com/openai/[email protected]")
openclip_package = os.environ.get('OPENCLIP_PACKAGE', "git+https://github.com/mlfoundations/[email protected]")

stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/Stability-AI/stablediffusion.git")
taming_transformers_repo = os.environ.get('TAMING_TRANSFORMERS_REPO', "https://github.com/CompVis/taming-transformers.git")
k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/crowsonkb/k-diffusion.git')
codeformer_repo = os.environ.get('CODEFORMER_REPO', 'https://github.com/sczhou/CodeFormer.git')
blip_repo = os.environ.get('BLIP_REPO', 'https://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 https://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 https://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 'https://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地址前邊都加上https://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)

本文正在參加「金石計劃」