Stable Diffusion Web UIをインストールする!(Ubuntu Desktop 22.04/CUDA)

Ubuntu Desktop 22.04で、NVIDIAのGPUありの環境で、Stable Diffusion Web UIをインストールします。

PyTorchをインストールする!

以下の記事を参考にPyTorchをインストールします。

Gitをインストールする!

Gitがインストールされていない場合は、Gitを標準のリポジトリからインストールします。

$ sudo apt install git

Git HubからStable Diffusion Web UIをダウンロードする!

Git HubからStable Diffusion Web UIをインストールします。

$ git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
Cloning into 'stable-diffusion-webui'...
remote: Enumerating objects: 26588, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 26588 (delta 7), reused 14 (delta 7), pack-reused 26571
Receiving objects: 100% (26588/26588), 31.91 MiB | 21.43 MiB/s, done.
Resolving deltas: 100% (18632/18632), done.

Stable Diffusion Web UIのディレクトリに移動する!

Stable Diffution Web UIをダウンロードしたディレクトリに移動します。

$ cd stable-diffusion-webui

Stable Diffusion Web UIを起動する!

Stable Diffusion Web UIを起動します。少し時間がかかります。アクセスするためのURLが表示されればインストール完了です。プロンプトは終了しないので注意しましょう。

$ python3 launch.py
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
Version: v1.5.2
Commit hash: c9c8485bc1e8720aba70f029d25cba1c4abf2b5c
Installing gfpgan
Installing clip
Installing open_clip
Cloning Stable Diffusion into /home/usradmin/stable-diffusion-webui/repositories/stable-diffusion-stability-ai...
Cloning into '/home/usradmin/stable-diffusion-webui/repositories/stable-diffusion-stability-ai'...
remote: Enumerating objects: 574, done.
remote: Counting objects: 100% (304/304), done.
remote: Compressing objects: 100% (86/86), done.
remote: Total 574 (delta 244), reused 218 (delta 218), pack-reused 270
Receiving objects: 100% (574/574), 73.43 MiB | 24.17 MiB/s, done.
Resolving deltas: 100% (276/276), done.
Cloning Stable Diffusion XL into /home/usradmin/stable-diffusion-webui/repositories/generative-models...
Cloning into '/home/usradmin/stable-diffusion-webui/repositories/generative-models'...
remote: Enumerating objects: 740, done.
remote: Counting objects: 100% (563/563), done.
remote: Compressing objects: 100% (286/286), done.
remote: Total 740 (delta 337), reused 424 (delta 263), pack-reused 177
Receiving objects: 100% (740/740), 22.32 MiB | 18.58 MiB/s, done.
Resolving deltas: 100% (375/375), done.
Cloning K-diffusion into /home/usradmin/stable-diffusion-webui/repositories/k-diffusion...
Cloning into '/home/usradmin/stable-diffusion-webui/repositories/k-diffusion'...
remote: Enumerating objects: 949, done.
remote: Counting objects: 100% (225/225), done.
remote: Compressing objects: 100% (125/125), done.
remote: Total 949 (delta 146), reused 172 (delta 100), pack-reused 724
Receiving objects: 100% (949/949), 206.24 KiB | 1.93 MiB/s, done.
Resolving deltas: 100% (623/623), done.
Cloning CodeFormer into /home/usradmin/stable-diffusion-webui/repositories/CodeFormer...
Cloning into '/home/usradmin/stable-diffusion-webui/repositories/CodeFormer'...
remote: Enumerating objects: 594, done.
remote: Counting objects: 100% (245/245), done.
remote: Compressing objects: 100% (98/98), done.
remote: Total 594 (delta 176), reused 167 (delta 147), pack-reused 349
Receiving objects: 100% (594/594), 17.31 MiB | 11.49 MiB/s, done.
Resolving deltas: 100% (287/287), done.
Cloning BLIP into /home/usradmin/stable-diffusion-webui/repositories/BLIP...
Cloning into '/home/usradmin/stable-diffusion-webui/repositories/BLIP'...
remote: Enumerating objects: 277, done.
remote: Counting objects: 100% (165/165), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 277 (delta 137), reused 136 (delta 135), pack-reused 112
Receiving objects: 100% (277/277), 7.03 MiB | 16.08 MiB/s, done.
Resolving deltas: 100% (152/152), done.
Installing requirements for CodeFormer
Installing requirements
Launching Web UI with arguments: 
no module 'xformers'. Processing without...
no module 'xformers'. Processing without...
No module 'xformers'. Proceeding without it.
Downloading: "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.safetensors" to /home/usradmin/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors

100%|██████████████████████████████████████████████████████████████████████████████| 3.97G/3.97G [01:17<00:00, 54.9MB/s]
Calculating sha256 for /home/usradmin/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors: Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 175.9s (launcher: 90.6s, import torch: 2.7s, import gradio: 0.7s, setup paths: 0.6s, other imports: 1.4s, list SD models: 79.0s, load scripts: 0.4s, create ui: 0.3s).
6ce0161689b3853acaa03779ec93eafe75a02f4ced659bee03f50797806fa2fa
Loading weights [6ce0161689] from /home/usradmin/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors
Creating model from config: /home/usradmin/stable-diffusion-webui/configs/v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Downloading (…)olve/main/vocab.json: 100%|███████████████████████████████████████████| 961k/961k [00:00<00:00, 1.43MB/s]
Downloading (…)olve/main/merges.txt: 100%|███████████████████████████████████████████| 525k/525k [00:00<00:00, 1.01MB/s]
Downloading (…)cial_tokens_map.json: 100%|█████████████████████████████████████████████| 389/389 [00:00<00:00, 2.85MB/s]
Downloading (…)okenizer_config.json: 100%|█████████████████████████████████████████████| 905/905 [00:00<00:00, 6.31MB/s]
Downloading (…)lve/main/config.json: 100%|█████████████████████████████████████████| 4.52k/4.52k [00:00<00:00, 24.1MB/s]
Applying attention optimization: Doggettx... done.
Model loaded in 15.0s (calculate hash: 8.8s, load weights from disk: 0.2s, create model: 4.4s, apply weights to model: 0.7s, apply half(): 0.5s, move model to device: 0.3s).

Stable Diffution Web UIにアクセスする!

表示された以下のURLにアクセスするとStable DiffusionのWeb UI画面にアクセスできます。

http://127.0.0.1:7860

CUDAのメモリ不足が発生したら・・・

以下のようにCUDAのメモリ不足が発生することがあります。

   torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 128.00 MiB (GPU 0; 2.95 GiB total capacity; 2.18 GiB already allocated; 65.62 MiB free; 2.27 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

その場合は、少ないメモリでも動作するように、「--lowvram --xformers」のオプションを指定して起動し直します。

$ python3 launch.py --lowvram --xformers
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
Version: v1.5.2
Commit hash: c9c8485bc1e8720aba70f029d25cba1c4abf2b5c
Installing xformers
Launching Web UI with arguments: --lowvram --xformers
Loading weights [6ce0161689] from /home/usradmin/stable-diffusion-webui/models/Stable-diffusion/v1-5-pruned-emaonly.safetensors
Running on local URL:  http://127.0.0.1:7860
...

おわりに

Stable Diffusion Web UIは、GitHubからダウンロードすることができます。

参考情報

関連記事