Stable Diffusion web UIを用いた、ローカル環境での画像生成を行いたいと思います。自分がコマンドプロンプト操作よりもUNIXコマンドのほうが好きなこともあり、WSL2上のUbuntuに環境構築をします。
また、プロジェクトごとにライブラリを分けられると聞きましたのでvenvを使用します。あとPythonのバージョン管理にpyenvを使用します。
大枠の手順をAIに聞いたのでまとめる意味は…
環境
- Windows11 home 24H2
- NVIDIAのGPUが入っている(画像生成で使う)
- WSL2 (WSL バージョン: 2.3.26.0)
- Ubuntu 24.04.1 LTS
作業
WSL2の設定確認
- WSL2とUbuntuがインストール済みであることを確認。
- NVIDIA GPUドライバのWSL対応バージョンがインストール済みか確認
# Ubuntuコンソールで
nvidia-smi
# インストール済みだと表形式の出力がされる
パッケージの最新化、必要パッケージのインストール
# Ubuntuコンソールで
sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential curl wget git python3 python3-pip python3-venv
Pythonバージョン管理ツールの導入(pyenv使用)
- pyenvインストール
# Ubuntuコンソールで
curl https://pyenv.run | bash
- 出力
(前略)
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by appending
# the following to
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
# Restart your shell for the changes to take effect.
# Load pyenv-virtualenv automatically by adding
# the following to ~/.bashrc:
eval "$(pyenv virtualenv-init -)"
- 言われた通りに~/.bashrcを編集しシェル設定を更新
# vimなどのエディタで~/.bashrcに追記
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
- 設定の反映
source ~/.bashrc
pyenvを使ってPythonのインストールをしたいが、まずはpyenv用の環境を整える
- pyenvには推奨ビルド環境があるらしい。https://github.com/pyenv/pyenv/wiki#suggested-build-environment
- すげぇ大量のパッケージを入れる
sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
pythonのインストール
- 実施時のStable Diffusion web UIが「python 3.10.6でテストした」とあったのでそれに合わせた
pyenv install 3.10.6
プロジェクトごとの環境を分ける準備
- 仮想環境作成
mkdir ~/my_project
cd ~/my_project
python3 -m venv venv
- 仮想環境有効化方法(今やるわけではない)
$ source venv/bin/activate
# 表記が変わった
(venv) ch1hh1@CK-Lightning-G:~/pworks/first_py$
(venv) ch1hh1@CK-Lightning-G:~/pworks/first_py$
- 仮想環境の無効化方法(今やるわけではない)
deactivate
CUDAとPyTorchのインストール
- RTX 4070を利用するには、CUDAおよびPyTorchの対応バージョンをインストールする必要があるらしい。
- (1) NVIDIAのCUDAツールキット:CUDAが正しくインストールされているか確認する。WSL用のドライバは自動的にCUDAを含むので、通常追加インストールは不要。
- (2) PyTorchのインストール
- 公式のインストールガイドを参照https://pytorch.org/get-started/locally/
- 私が実施したとき、インストール表の選択肢にCUDAが12.4までしかなかった。手元はどうやら12.6っぽいのだが…わからんので、以下の組み合わせでの導入コマンド試したところ問題なくインストールできた模様。
- stable
- linux
- pip
- python
- cuda12.4
リポジトリのクローン
- Stable Diffusion web UI用のリポジトリをクローンします(例:automatic1111/stable-diffusion-webui)。
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
依存関係のインストール
仮想環境を有効化した状態で stable-diffusion-webuiディレクトリにて以下を実行:
pip install -r requirements.txt
この時エラーに陥るかもしれない。
前略、
error: can't find Rust compiler
If you are using an outdated pip version, it is possible a prebuilt wheel is available for this package but pip is not able to install from it. Installing from the wheel would avoid the need for a Rust compiler.
To update pip, run:
pip install --upgrade pip
and then retry package installation.
If you did intend to build this package from source, try installing a Rust compiler from your system package manager and ensure it is on the PATH during installation. Alternatively, rustup (available at https://rustup.rs) is the recommended way to download and update the Rust compiler toolchain.
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for tokenizers
Failed to build tokenizers
ERROR: ERROR: Failed to build installable wheels for some pyproject.toml based projects (tokenizers)
これの解決に以下記事を参考にさせていただきました
https://qiita.com/siruku6/items/a133f79ff4c80e73cea3
transformersというライブラリのバージョンの問題っぽい。
stable-diffusion-webui/requirements.txt に記載のあるライブラリバージョンを変更する。
transformers==4.37.2
に変えてみたところ、requirement のインストールができた。
画像生成用のモデルのダウンロード・配置
Hugging Faceなどからモデルをダウンロードし、stable-diffusion-webui/models/Stable-diffusion/フォルダに配置する。
動作確認
bash ./webui.sh
ブラウザで http://127.0.0.1:7860 にアクセスし、画面が表示されれば成功。
画像生成を試せる。