在纯净的虚拟机上安装及使用Stable Diffusion WebUI
淘气小姑娘 2024-10-01 10:03:02 阅读 73
一、环境准备1、创建一台纯净的虚拟机2、安装驱动3、下载安装Anaconda
二、部署stable-diffusion-webui1、Linux 上的自动安装2、导航到您想要安装 webui 的目录并执行以下命令:3、执行webui.sh
三、安装插件1、在WebUI上安装插件2、添加inpainting模型到指定目录
四、进行文生图测试
一、环境准备
1、创建一台纯净的虚拟机
这里我创建的是一台Ubuntu22.04的虚拟机,配置如下:
<code>CPU:8核
内存:24GB
系统盘:50G(本地硬盘)
数据盘:100G(本地硬盘)
透传设备:2块 (算力GPU-GeForce RTX 4090 D)
2、安装驱动
把 install.sh 驱动脚本传到指定目录(我是安装在了/data目录下),执行以下命令进行安装:
sh install.sh
install.sh脚本如下
<code>
#!/bin/sh
# This script installs Ollama on Linux.
# It detects the current operating system architecture and installs the appropriate version of Ollama.
set -eu
status() { echo ">>> $*" >&2; }
error() { echo "ERROR $*"; exit 1; }
warning() { echo "WARNING: $*"; }
TEMP_DIR=$(mktemp -d)
cleanup() { rm -rf $TEMP_DIR; }
trap cleanup EXIT
available() { command -v $1 >/dev/null; }
require() {
local MISSING=''code>
for TOOL in $*; do
if ! available $TOOL; then
MISSING="$MISSING $TOOL"code>
fi
done
echo $MISSING
}
[ "$(uname -s)" = "Linux" ] || error 'This script is intended to run on Linux only.'
ARCH=$(uname -m)
case "$ARCH" in
x86_64) ARCH="amd64" ;;code>
aarch64|arm64) ARCH="arm64" ;;code>
*) error "Unsupported architecture: $ARCH" ;;
esac
IS_WSL2=false
KERN=$(uname -r)
case "$KERN" in
*icrosoft*WSL2 | *icrosoft*wsl2) IS_WSL2=true;;
*icrosoft) error "Microsoft WSL1 is not currently supported. Please upgrade to WSL2 with 'wsl --set-version <distro> 2'" ;;
*) ;;
esac
VER_PARAM="${OLLAMA_VERSION:+?version=$OLLAMA_VERSION}"code>
SUDO=
if [ "$(id -u)" -ne 0 ]; then
# Running as root, no need for sudo
if ! available sudo; then
error "This script requires superuser permissions. Please re-run as root."
fi
SUDO="sudo"code>
fi
NEEDS=$(require curl awk grep sed tee xargs)
if [ -n "$NEEDS" ]; then
status "ERROR: The following tools are required but missing:"
for NEED in $NEEDS; do
echo " - $NEED"
done
exit 1
fi
# Everything from this point onwards is optional.
# WSL2 only supports GPUs via nvidia passthrough
# so check for nvidia-smi to determine if GPU is available
if [ "$IS_WSL2" = true ]; then
if available nvidia-smi && [ -n "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
status "Nvidia GPU detected."
fi
install_success
exit 0
fi
# Install GPU dependencies on Linux
if ! available lspci && ! available lshw; then
warning "Unable to detect NVIDIA/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies."
exit 0
fi
check_gpu() {
# Look for devices based on vendor ID for NVIDIA and AMD
case $1 in
lspci)
case $2 in
nvidia) available lspci && lspci -d '10de:' | grep -q 'NVIDIA' || return 1 ;;
amdgpu) available lspci && lspci -d '1002:' | grep -q 'AMD' || return 1 ;;
esac ;;
lshw)
case $2 in
nvidia) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[10DE\]' || return 1 ;;
amdgpu) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[1002\]' || return 1 ;;
esac ;;
nvidia-smi) available nvidia-smi || return 1 ;;
esac
}
if check_gpu nvidia-smi; then
status "NVIDIA GPU installed."
exit 0
fi
if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu; then
install_success
warning "No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode."
exit 0
fi
if check_gpu lspci amdgpu || check_gpu lshw amdgpu; then
# Look for pre-existing ROCm v6 before downloading the dependencies
for search in "${HIP_PATH:-''}" "${ROCM_PATH:-''}" "/opt/rocm" "/usr/lib64"; do
if [ -n "${search}" ] && [ -e "${search}/libhipblas.so.2" -o -e "${search}/lib/libhipblas.so.2" ]; then
status "Compatible AMD GPU ROCm library detected at ${search}"
install_success
exit 0
fi
done
status "Downloading AMD GPU dependencies..."
$SUDO rm -rf /usr/share/ollama/lib
$SUDO chmod o+x /usr/share/ollama
$SUDO install -o ollama -g ollama -m 755 -d /usr/share/ollama/lib/rocm
curl --fail --show-error --location --progress-bar "https://ollama.com/download/ollama-linux-amd64-rocm.tgz${VER_PARAM}" \
| $SUDO tar zx --owner ollama --group ollama -C /usr/share/ollama/lib/rocm .
install_success
status "AMD GPU ready."
exit 0
fi
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-8-rocky-8
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-9-rocky-9
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#fedora
install_cuda_driver_yum() {
status 'Installing NVIDIA repository...'
case $PACKAGE_MANAGER in
yum)
$SUDO $PACKAGE_MANAGER -y install yum-utils
$SUDO $PACKAGE_MANAGER-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
;;
dnf)
$SUDO $PACKAGE_MANAGER config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
;;
esac
case $1 in
rhel)
status 'Installing EPEL repository...'
# EPEL is required for third-party dependencies such as dkms and libvdpau
$SUDO $PACKAGE_MANAGER -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm || true
;;
esac
status 'Installing CUDA driver...'
if [ "$1" = 'centos' ] || [ "$1$2" = 'rhel7' ]; then
$SUDO $PACKAGE_MANAGER -y install nvidia-driver-latest-dkms
fi
$SUDO $PACKAGE_MANAGER -y install cuda-drivers
}
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
install_cuda_driver_apt() {
status 'Installing NVIDIA repository...'
curl -fsSL -o $TEMP_DIR/cuda-keyring.deb https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-keyring_1.1-1_all.deb
case $1 in
debian)
status 'Enabling contrib sources...'
$SUDO sed 's/main/contrib/' < /etc/apt/sources.list | $SUDO tee /etc/apt/sources.list.d/contrib.list > /dev/null
if [ -f "/etc/apt/sources.list.d/debian.sources" ]; then
$SUDO sed 's/main/contrib/' < /etc/apt/sources.list.d/debian.sources | $SUDO tee /etc/apt/sources.list.d/contrib.sources > /dev/null
fi
;;
esac
status 'Installing CUDA driver...'
$SUDO dpkg -i $TEMP_DIR/cuda-keyring.deb
$SUDO apt-get update
[ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E=code>
DEBIAN_FRONTEND=noninteractive $SUDO_E apt-get -y install cuda-drivers -q
}
if [ ! -f "/etc/os-release" ]; then
error "Unknown distribution. Skipping CUDA installation."
fi
. /etc/os-release
OS_NAME=$ID
OS_VERSION=$VERSION_ID
PACKAGE_MANAGER=
for PACKAGE_MANAGER in dnf yum apt-get; do
if available $PACKAGE_MANAGER; then
break
fi
done
if [ -z "$PACKAGE_MANAGER" ]; then
error "Unknown package manager. Skipping CUDA installation."
fi
if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
case $OS_NAME in
centos|rhel) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -d '.' -f 1) ;;
rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;;
fedora) [ $OS_VERSION -lt '37' ] && install_cuda_driver_yum $OS_NAME $OS_VERSION || install_cuda_driver_yum $OS_NAME '37';;
amzn) install_cuda_driver_yum 'fedora' '37' ;;
debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;;
ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's/\.//') ;;
*) exit ;;
esac
fi
if ! lsmod | grep -q nvidia || ! lsmod | grep -q nvidia_uvm; then
KERNEL_RELEASE="$(uname -r)"code>
case $OS_NAME in
rocky) $SUDO $PACKAGE_MANAGER -y install kernel-devel kernel-headers ;;
centos|rhel|amzn) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;;
fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE ;;
debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;;
*) exit ;;
esac
NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '/added/ { print $1 }')
if [ -n "$NVIDIA_CUDA_VERSION" ]; then
$SUDO dkms install $NVIDIA_CUDA_VERSION
fi
if lsmod | grep -q nouveau; then
status 'Reboot to complete NVIDIA CUDA driver install.'
exit 0
fi
$SUDO modprobe nvidia
$SUDO modprobe nvidia_uvm
fi
# make sure the NVIDIA modules are loaded on boot with nvidia-persistenced
if command -v nvidia-persistenced > /dev/null 2>&1; then
$SUDO touch /etc/modules-load.d/nvidia.conf
MODULES="nvidia nvidia-uvm"code>
for MODULE in $MODULES; do
if ! grep -qxF "$MODULE" /etc/modules-load.d/nvidia.conf; then
echo "$MODULE" | sudo tee -a /etc/modules-load.d/nvidia.conf > /dev/null
fi
done
fi
status "NVIDIA GPU ready."
yum -y install cuda-toolkit-12-4
install_success
驱动安装完成
3、下载安装Anaconda
下载地址:https://repo.anaconda.com/archive/
我下载的Anaconda 版本是:Anaconda3-2024.06-1-Linux-x86_64.sh
安装程序 Python 版本:Python 3.10.12
inux下载命令如下:
<code>wget https://repo.anaconda.com/archive/Anaconda3-2024.06-1-Linux-x86_64.sh
下载完成后执行:
sh Anaconda3-2024.06-1-Linux-x86_64.sh
安装地址:/usr/local/annocada3,剩下一直回车就行:
安装完成显示:
安装完成后,退出客户端重新连一下
到此,环境准备结束!!!!
二、部署stable-diffusion-webui
git地址:stable-diffusion-webui
1、Linux 上的自动安装
<code># Debian-based:
sudo apt install wget git python3 python3-venv libgl1 libglib2.0-0
如果你的系统很新,你需要安装python3.11或者python3.10。(参考git:https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/master)
这里我在标题3、下载安装Anaconda中已安装
2、导航到您想要安装 webui 的目录并执行以下命令:
wget -q https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh
或者直接将 repo 克隆到你想要的任何位置:
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
备注:这里git有时候不好用,所以我是直接到https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/master,下载zip包到本地,再上传到虚拟机的。
3、执行webui.sh
进入到目录:/data/stable-diffusion-webui-master/
<code>root@sssss3322-1:/data# cd stable-diffusion-webui-master/
执行bash webui.sh
这里执行bash webui.sh失败了,遇到了如下问题
这里提示“This script must not be launched as root, aborting(这个脚本不能以root身份启动,否则会中止)”,所以我尝试创建了一个新用户,但是后来也执行失败了。
最后将执行命令改为bash webui.sh -f,执行过程中遇到了以下问题:
该问题解决办法:
执行以下命令:
<code>pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
如果出现:
执行以下命令安装
<code>apt install python3-pip
序号3安装完成后,重新执行序号1,执行完成后显示如下:
再执行bash webui.sh -f,成功运行截图实例如下:
注意:这里虽然执行成功了,服务跑起来了,但是在网页上访问的时候出现了“此地址拒绝访问”的问题
所以,最后将执行命令改为了:
<code>bash webui.sh -f --server-name=0.0.0.0
然后在新创建的同网段的windows窗口进行访问(虚拟机IP:7860),访问成功,截图如下:
三、安装插件
备注:访问SD的WebUI发现,没有checkpoint:
1、在WebUI上安装插件
插件安装完成后,需要重启服务生效。(“Extensions” - “Installed” - “Apply and restart UI”)
2、添加inpainting模型到指定目录
参考链接:https://www.cnblogs.com/zackstang/p/17324263.html
可以将下载的Stable Diffusion模型放在目录stable-diffusion-webui/models/Stable-diffusion/ 下。
例如,假设我们要做inpaint的调整。先在huggingface下载stable-diffusion-inpainting的checkpoint:
https://huggingface.co/runwayml/stable-diffusion-inpainting
并存放在stable-diffusion-webui/models/Stable-diffusion/ 下。
而后我们在web UI 刷新checkpoint目录即可,就得到了上图所示。
四、进行文生图测试
填入提示语,进行文生图
提示语输入可参考网上,例如:提示语
解决办法:
参考连接:Stable Diffusion WebUI linux部署问题
去网址https://huggingface.co/openai/clip-vit-large-patch14/tree/main下载以下文件:
上传至虚拟机,新建目录 mkdir -p .cache/huggingface/transformers
下载后开始进行配置,这些文件(clip)是模型中encoder所需要的,我们可以从报错信息中去发现是哪里调用了这个openai/clip-vit-large-patch14,去修改这个路径配置即可。如图,我这里显示:
根据日志信息,找到对应路径,修改相应信息:
原来是:
修改:
将version=“openai/clip-vit-large-patch14”
改为:version=“/data/stable-diffusion-webui-master/.cache/huggingface/transformers”
配置修改完成后需要重启SD服务
重启后,尝试进行文生图:
细节还可以修改左下角详细参数。
上一篇: 【Java】已解决:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
下一篇: 一篇文章汇总前端ES6和JavaScript数组的所有方法
本文标签
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。