ubuntu24.04本地部署stable-diffusion-v1.5
周末嘗試在我的電腦上本地部署stable-diffusion-v1.5這里做個記錄
我的ubuntu版本是24.04, 為了在本地用上AI如文生圖等嘗試在本地部署stable-diffusion
先部署 Stable Diffusion WebU,
它是AI繪畫的環境和框架, 包括Web界面、操作工具、插件系統等。
clone stable-diffusion-webui自動化腳本
$ git clone https://github.com/openvinotoolkit/stable-diffusion-webui
由于我的電腦是intel顯卡,所以按照https://github.com/openvinotoolkit/stable-diffusion-webui/wiki/Installation-on-Intel-Silicon的指導clone上述openvinotoolkit定制工程,nvidia或amd建議clone如下
$ git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
之后進入工程
$ cd stable-diffusion-webui
$ ./webui.sh
...
tested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer
...
Looking in indexes: https://pypi.org/simple, https://download.pytorch.org/whl/cu121
ERROR: Could not find a version that satisfies the requirement torch2.1.2 (from versions: 2.2.0, 2.2.0+cu121, 2.2.1, 2.2.1+cu121, 2.2.2, 2.2.2+cu121, 2.3.0, 2.3.0+cu121, 2.3.1, 2.3.1+cu121, 2.4.0, 2.4.0+cu121, 2.4.1, 2.4.1+cu121, 2.5.0, 2.5.0+cu121, 2.5.1, 2.5.1+cu121, 2.6.0, 2.7.0, 2.7.1, 2.8.0, 2.9.0)
ERROR: No matching distribution found for torch2.1.2
Traceback (most recent call last):
File "/home/yma/project/stable-diffusion-webui/launch.py", line 48, in
main()
File "/home/yma/project/stable-diffusion-webui/launch.py", line 39, in main
prepare_environment()
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 381, in prepare_environment
run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True)
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 116, in run
raise RuntimeError("\n".join(error_bits))
RuntimeError: Couldn't install torch.
Command: "/home/yma/venv/bin/python3" -m pip install torch2.1.2 torchvision0.16.2 --extra-index-url https://download.pytorch.org/whl/cu121
Error code: 1
提示torch沒有兼容的版本, 因為我前ubuntu24.04的默認python版本是3.12,不兼容,期望支持的python版本是3.10,嘗試先安裝python3.10
為了不和現有的python沖突,在虛擬環境安裝,以后的執行也都在此虛擬環境,
在虛擬環境安裝python3.10
update the repository
$ sudo add-apt-repository ppa:deadsnakes/ppa -y
$ sudo apt update
$ sudo apt install python3.10 python3.10-venv python3.10-dev -y
stable-diffusion-webui$ python3.10 -m venv venv
stable-diffusion-webui$ source venv/bin/activate
由于我這臺電腦是intel顯卡,所以根據https://github.com/openvinotoolkit/stable-diffusion-webui/wiki/Installation-on-Intel-Silicon
安裝intel的顯卡工具箱toolkit,
(venv) stable-diffusion-webui$ pip install --pre openvino
同時給以下環境變量,linux下禁用多線程(沒有進度條顯示)因為容易出錯,跳過cuda檢查因為我的顯卡不是NVIDIA的,不下載assert庫
(venv) stable-diffusion-webui$ export PYTORCH_TRACING_MODE=TORCHFX
(venv) stable-diffusion-webui$ export TORCHINDUCTOR_DISABLE_PROCESS_POOL=1
(venv) stable-diffusion-webui$ export COMMANDLINE_ARGS="--skip-torch-cuda-test --precision full --no-half"
不然腳本默認認為是NVIDIA顯卡會報錯誤,
第2次執行webui.sh
(venv) stable-diffusion-webui$ `./webui.sh
...
Installing clip
Traceback (most recent call last):
File "/home/yma/project/stable-diffusion-webui/launch.py", line 48, in
main()
File "/home/yma/project/stable-diffusion-webui/launch.py", line 39, in main
prepare_environment()
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 394, in prepare_environment
run_pip(f"install {clip_package}", "clip")
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 144, in run_pip
return run(f'"{python}" -m pip {command} --prefer-binary{index_url_line}', desc=f"Installing {desc}", errdesc=f"Couldn't install {desc}", live=live)
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 116, in run
raise RuntimeError("\n".join(error_bits))
RuntimeError: Couldn't install clip.
Command: "/home/yma/project/stable-diffusion-webui/venv/bin/python3.10" -m pip install https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip --prefer-binary
Error code: 1
stdout: Collecting https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip
Downloading https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip (4.3 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.3/4.3 MB 62.9 kB/s eta 0:00:00
Preparing metadata (setup.py): started
Preparing metadata (setup.py): finished with status 'done'
Collecting ftfy
Downloading ftfy-6.3.1-py3-none-any.whl (44 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 44.8/44.8 kB 2.5 MB/s eta 0:00:00
stderr: ERROR: Could not find a version that satisfies the requirement regex (from clip) (from versions: none)
ERROR: No matching distribution found for regex
[notice] A new release of pip is available: 23.0.1 -> 25.3
[notice] To update, run: pip install --upgrade pip
安裝clip出錯,嘗試更新pip
(venv) stable-diffusion-webui$
pip install --upgrade pip
Requirement already satisfied: pip in ./venv/lib/python3.10/site-packages (23.0.1)
Collecting pip
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/44/3c/d717024885424591d5376220b5e836c2d5293ce2011523c9de23ff7bf068/pip-25.3-py3-none-any.whl
Downloading pip-25.3-py3-none-any.whl (1.8 MB)
━━━━━?━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0.3/1.8 MB 4.0 kB/s eta 0:06:18
ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE. If you have updated the package versions, please update the hashes. Otherwise, examine the package contents carefully; someone may have tampered with them.
pip from https://files.pythonhosted.org/packages/44/3c/d717024885424591d5376220b5e836c2d5293ce2011523c9de23ff7bf068/pip-25.3-py3-none-any.whl:
Expected sha256 9655943313a94722b7774661c21049070f6bbb0a1516bf02f7c8d5d9201514cd
Got 4ebbe2aad4365695844f62d4760bca98008b142c44239c6ab0d4ee0301623471
[notice] A new release of pip is available: 23.0.1 -> 25.3
[notice] To update, run: pip install --upgrade pip
由于從國外服務器下載包網絡超時和不穩定導致的錯誤,
配置pip使用國內鏡像源,
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.timeout 120
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.retries 3
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip install --upgrade pip
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pip in ./venv/lib/python3.10/site-packages (23.0.1)
Collecting pip
ERROR: HTTP error 403 while getting https://pypi.tuna.tsinghua.edu.cn/packages/44/3c/d717024885424591d5376220b5e836c2d5293ce2011523c9de23ff7bf068/pip-25.3-py3-none-any.whl (from https://pypi.tuna.tsinghua.edu.cn/simple/pip/) (requires-python:>=3.9)
ERROR: Could not install requirement pip from https://pypi.tuna.tsinghua.edu.cn/packages/44/3c/d717024885424591d5376220b5e836c2d5293ce2011523c9de23ff7bf068/pip-25.3-py3-none-any.whl because of HTTP error 403 Client Error: Forbidden for url: https://pypi.tuna.tsinghua.edu.cn/packages/44/3c/d717024885424591d5376220b5e836c2d5293ce2011523c9de23ff7bf068/pip-25.3-py3-none-any.whl for URL https://pypi.tuna.tsinghua.edu.cn/packages/44/3c/d717024885424591d5376220b5e836c2d5293ce2011523c9de23ff7bf068/pip-25.3-py3-none-any.whl (from https://pypi.tuna.tsinghua.edu.cn/simple/pip/) (requires-python:>=3.9)
[notice] A new release of pip is available: 23.0.1 -> 25.3
[notice] To update, run: pip install --upgrade pip
看來清華源無效,嘗試改為阿里源,
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ rm -rf ~/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.trusted-host mirrors.aliyun.com
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.timeout 120
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip config set global.retries 3
Writing to /home/yma/.config/pip/pip.conf
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ sync
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip install --upgrade pip
Looking in indexes: https://mirrors.aliyun.com/pypi/simple
Requirement already satisfied: pip in ./venv/lib/python3.10/site-packages (23.0.1)
Collecting pip
Downloading https://mirrors.aliyun.com/pypi/packages/44/3c/d717024885424591d5376220b5e836c2d5293ce2011523c9de23ff7bf068/pip-25.3-py3-none-any.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 4.2 MB/s eta 0:00:00
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 23.0.1
Uninstalling pip-23.0.1:
Successfully uninstalled pip-23.0.1
Successfully installed pip-25.3
pip終于更新成功。
嘗試第3次執行./webui.sh
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$`./webui.sh
它會下載torch等依賴,中間過程由于國內訪問github不穩定可能會數次失敗,重試幾次。重試也不行的repository,嘗試用wget或crul下載到一個臨時目錄然后解壓到當前需要的地方,eg.
Cloning into '/home/yma/project/stable-diffusion-webui/repositories/generative-models'...
fatal: unable to access 'https://github.com/Stability-AI/generative-models.git/': GnuTLS recv error (-110): The TLS connection was non-properly terminated.
Traceback (most recent call last):
File "/home/yma/project/stable-diffusion-webui/launch.py", line 48, in
main()
File "/home/yma/project/stable-diffusion-webui/launch.py", line 39, in main
prepare_environment()
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 413, in prepare_environment
git_clone(stable_diffusion_xl_repo, repo_dir('generative-models'), "Stable Diffusion XL", stable_diffusion_xl_commit_hash)
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 192, in git_clone
run(f'"{git}" clone --config core.filemode=false "{url}" "{dir}"', f"Cloning {name} into {dir}...", f"Couldn't clone {name}", live=True)
File "/home/yma/project/stable-diffusion-webui/modules/launch_utils.py", line 116, in run
raise RuntimeError("\n".join(error_bits))
RuntimeError: Couldn't clone Stable Diffusion XL.
Command: "git" clone --config core.filemode=false "https://github.com/Stability-AI/generative-models.git" "/home/yma/project/stable-diffusion-webui/repositories/generative-models"
Error code: 128
$ wget https://github.com/Stability-AI/generative-models/archive/refs/heads/main.zip /tmp/main.zip
$ unzip /tmp/main.zip -d /home/yma/project/stable-diffusion-webui/repositories/
$ mv /home/yma/project/stable-diffusion-webui/repositories/generative-models-main /home/yma/project/stable-diffusion-webui/repositories/generative-models
還是不行的話可以禁用擴展git倉庫,
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ ./webui.sh --no-download-sd-model --listen
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ pip install huggingface_hub0.16.4 diffusers0.21.4 transformers==4.31.0
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ export HF_ENDPOINT=https://hf-mirror.com
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ echo 'export HF_ENDPOINT=https://hf-mirror.com' >> ~/.bashrc
這個過程比較耗時,約2小時甚至更久,取決于當時網速。
Calculating sha256 for /home/yma/project/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors: Running on local URL: http://0.0.0.0:7860
To create a public link, set
share=Trueinlaunch().
Startup time: 7.7s (prepare environment: 0.1s, import torch: 3.6s, import gradio: 1.0s, setup paths: 0.8s, other imports: 0.4s, setup codeformer: 0.3s, load scripts: 0.7s, create ui: 0.5s, gradio launch: 0.1s).
6ce0161689b3853acaa03779ec93eafe75a02f4ced659bee03f50797806fa2fa
待終端出現類似的字樣,說明安裝成功了。
打開瀏覽器,輸入http://127.0.0.1:7860,即看到webui界面。
首次啟動后,webui界面是空白的,先在終端中按ctrl+c停止,因為它還沒有大模型,也就是說我們有畫室了但是還沒有特定風格的畫家們。
下載和添加模型(繪畫風格/知識庫)
下載一個基礎的Stable Diffusion模型如stable-diffusion-v1-5,大小約4G,然后把v1-5pruned-emaonly.safetensors放入指定文件夾。
stable-diffusion-v1-5國外服務器地址是https://huggingface.co/runwayml/stable-diffusion-v1-5,這個地址國內無法訪問,幸運的是我們國內有鏡像,
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ wget -O models/Stable-diffusion/v1-5-pruned-emaonly.safetensors https://hf-mirror.com/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors
下載VAE改善生成圖片顏色和細節,約300M,
yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ wget -O models/VAE/vae-ft-mse-840000-ema-pruned.safetensors https://hf-mirror.com/stabilityai/sd-vae-ft-mse-original/resolve/main/vae-ft-mse-840000-ema-pruned.safetensors
可以酌情考慮添加其它的大模型,如
全能型寫實
wget -O models/Stable-diffusion/deliberate_v3.safetensors https://hf-mirror.com/XpucT/Deliberate/resolve/main/Deliberate_v3.safetensors
--listen參數可以讓同一網絡下其他設備訪問,
(venv) yma@yma-X1-Carbon:~/project/stable-diffusion-webui$ ./webui.sh --listen --disable-console-progressbars
之后打開瀏覽器,輸入http://127.0.0.1:7860
然后可以輸入文字讓大模型生成圖片了。在我本地非常慢,生成一張圖片耗時約八九分鐘。

浙公網安備 33010602011771號