本文以Ubuntu 22.04 LTS为例安装Stable Diffusion WebUI。安装过程请使用一般用户登录,不要直接用root用户安装。安装过程主要包括显卡驱动安装,Anaconda安装,下载Stable Diffusion git repo,设置并启动Stable Diffusion。
1 安装显示卡驱动
1.1. Nvidia
安装 专有Nvidia驱动。
sudo apt-get update
ubuntu-drivers devices
选择系统推荐的nVidia驱动并安装。
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00002684sv00001043sd000088E2bc03sc00i00
vendor : NVIDIA Corporation
driver : nvidia-driver-535-server-open - distro non-free
driver : nvidia-driver-535-open - distro non-free
driver : nvidia-driver-525 - distro non-free
driver : nvidia-driver-545 - distro non-free
driver : nvidia-driver-535 - distro non-free recommended
driver : nvidia-driver-545-open - distro non-free
driver : nvidia-driver-535-server - distro non-free
driver : nvidia-driver-525-open - distro non-free
driver : nvidia-driver-525-server - distro non-free
driver : xserver-xorg-video-nouveau - distro free builtin
sudo apt install nvidia-driver-535
重启后确认nVidia驱动是否正常安装
nvidia-smi
nvcc --version
安装CUDA 12.3
$ wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
$ sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
$ wget https://developer.download.nvidia.com/compute/cuda/12.3.2/local_installers/cuda-repo-ubuntu2204-12-3-local_12.3.2-545.23.08-1_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu2204-12-3-local_12.3.2-545.23.08-1_amd64.deb
$ sudo cp /var/cuda-repo-ubuntu2204-12-3-local/cuda-*-keyring.gpg /usr/share/keyrings/
$ sudo apt-get update
$ sudo apt-get -y install cuda-toolkit-12-3
$ vim ~/.bashrc
>> export PATH=/usr/local/cuda/bin:$PATH
>> export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
$ source ~/.bashrc
1.2. AMD
- 安装AMD驱动程式,重开机。
sudo apt update && sudo apt upgrade -y
wget https://repo.radeon.com/amdgpu-install/22.40.3/ubuntu/focal/amdgpu-install_5.4.50403-1_all.deb
sudo apt-get install ./amdgpu-install_5.4.50403-1_all.deb
- 安装AMD ROCm,中间需要重开机。
sudo apt install linux-headers-`uname -r` linux-modules-extra-`uname -r`
curl -fsSL https://repo.radeon.com/rocm/rocm.gpg.key|sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/rocm-keyring.gpg
echo 'deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/rocm-keyring.gpg] https://repo.radeon.com/amdgpu/5.4.2/ubuntu jammy main'|sudo tee /etc/apt/sources.list.d/amdgpu.list
sudo apt-get update
sudo apt install amdgpu-dkms
sudo reboot
echo 'deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/rocm-keyring.gpg] https://repo.radeon.com/rocm/apt/5.4.2 jammy main'|sudo tee /etc/apt/sources.list.d/rocm.list
echo -e 'Package: *\nPin: release o=repo.radeon.com\nPin-Priority: 600'|sudo tee /etc/apt/preferences.d/rocm-pin-600
sudo apt-get update
sudo apt install rocm-hip-sdk rocm-opencl-sdk
echo "export LD_LIBRARY_PATH=/opt/rocm-5.4.2/lib:/opt/rocm-5.4.2/lib64" >> ~/.bashrc
source ~/.bashrc
- 確认驱动是否有正確安装
# 应会看到driver=amdgpu
sudo lshw -c video
# 查看ROCm是否正確安装
sudo apt show rocm-libs
2. 安装Anaconda
- 安装Python、wget、git。
sudo apt install python3 python3-pip python3-virtualenv wget git
- 安装 Anaconda。
- 建立Python 3.10.6的虚拟环境
conda create --name sdwebui python=3.10.6
3. 复製Stable Diffusion WebUI储存库
- 將Stable Diffusion WebUI复制至用户目录
cd ~
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
- (选择性) 预先將模型下载至
stable-diffusion-webui/models/Stable-diffusion
目录,这里使用Anything v4.5
wget -nc -P ~/stable-diffusion-webui/models/Stable-diffusion https://huggingface.co/andite/anything-v4.0/resolve/main/anything-v4.5-pruned.safetensors -O anything-v4.5-pruned.safetensors
- 启动虚擬环境:
conda activate sdwebui
如果想要於bash script自动以conda启动虚拟环境,可以把下面两行代码加入至webui-user.sh
最开始:
eval "$(conda shell.bash hook)"
conda activate sdwebui
在conda环境下,最好使用conda指令来安装套件。不过Stable Diffusion WebUI仍偏好以pip建立虚拟环境。
4. 设定启动参数
用vim编辑webui-user.sh
。
若显示卡VRAM在8GB以上,则设置:
COMMANDLINE_ARGS=--xformers --enable-insecure-extension-access
若显示卡VRAM小于或等于4GB,则设置:
COMMANDLINE_ARGS=--medvram --opt-split-attention --xformers --enable-insecure-extension-access
额外加上--listen
参数让其他电脑能以IP存取网页界面,--share
则是产生一组Gradio网址。
加上--xformers
可进一步减少VRAM佔用,但是这个选项只支援Nvidia显示卡。加入--no-half-vae
减少使用VAE时算出黑图的机率。
加上--enable-insecure-extension-access
让你在网页界面直接装扩充功能。加上--nowebui
则不会启动网页界面,单纯接收WebUI API之用。
其余参数设置请参阅Stable Diffusion WebUI系列|命令行脚本参数。
5. 启动Stable Diffusion WebUI
执行webui.sh
,它会自动下载PyTorch並安装依赖套件。跑完它应该会显示一组网址,本机用瀏览器开启http://127.0.0.1:7860
进入网页图形界面。
cd ~/stable-diffusion-webui
./webui.sh
文章评论