Create a Stable Diffusion Web UI with a Vultr Cloud GPU
Introduction
Stable Diffusion is a deep learning model that can generate detailed images based on input text descriptions. It can also be used to enhance an existing image or generate a new one based on another, guided by a text description prompt. This guide explains how to create a Stable Diffusion web UI on a Vultr Cloud GPU running on Ubuntu 22.04 using this Stable Diffusion web UI repository on GitHub.
Prerequisites
- Create a Hugging Face Account. You need to be registered to Hugging Face and accept Stable Diffusion's conditions to download the Stable Diffusion model.
- Deploy a Vultr Cloud GPU running on Ubuntu 22.04. The recommended GPU RAM for Stable Diffusion is at least 6 GB. You might have to upgrade your server's size if you encounter the error: "RuntimeError: CUDA error: out of memory"
- Update the Ubuntu Server.
- Create a non-root user with sudo access and log in with SSH.
Security Considerations
This guide creates a web UI for Stable Diffusion, but does not provide any protection from unwanted users or attacks. We strongly recommend that you protect your installation with a proxy or firewall, like these options:
- Install an Nginx Proxy with HTTP basic authentication
- Install Caddy as a reverse proxy with basic authentication
- Use a Vultr Firewall with IP filters
- Use Cloudflare and Vultr Firewall to protect a Vultr cloud server
Create a Stable Diffusion Web UI
Install Git Large File Storage (LFS) to clone the Stable Diffusion model.
$ sudo apt install git-lfs
Initialize Git Large File Storage (LFS).
$ git lfs install
Clone the Stable Diffusion repository. Enter your Hugging Face account credentials twice when prompted.
$ git clone https://huggingface.co/CompVis/stable-diffusion-v-1-4-original
You only need the weights or the
sd-v1-4.ckpt
checkpoint file from this repository.Wait for the cloning to finish.
Clone the Stable Diffusion Web UI Repository.
$ git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
Copy the
sd-v1-4.ckpt
checkpoint file from the Stable Diffusion directory to thestable-diffusion-webui/models/Stable-diffusion/
Web UI directory and rename it asmodel.ckpt
$ cp ~/stable-diffusion-v-1-4-original/sd-v1-4.ckpt ~/stable-diffusion-webui/models/Stable-diffusion/model.ckpt
Remove the Stable Diffusion folder you have cloned because you only need the
sd-v1-4.ckpt
file.$ rm -rf ~/stable-diffusion-v-1-4-original
Install the
FastAPI
framework using pip.$ pip3 install fastapi
The Stable Diffusion Web UI runs on port 7860 by default, so you must enable port 7860 on your firewall.
$ sudo ufw allow 7860
Run the Stable Diffusion Web UI with Tmux
When you run your Stable Diffusion Web UI on a normal SSH session, the Web UI's process closes when you exit the SSH session. To continuously run your Web UI even when you leave the SSH session, use tmux, a terminal multiplexer.
To create a Tmux session, run:
$ tmux new -s StableDiffusion
You may change
StableDiffusion
with any session name you prefer. Please see How to Install and Use Tmux for more information about Tmux.Change the directory to
stable-diffusion-webui
.$ cd ~/stable-diffusion-webui
Launch the Stable Diffusion Web UI by running
launch.py
using python.$ python3 launch.py --listen
The
--listen
argument makes your Web UI listen to network connections, not just on localhost. Please see Command Line Arguments and Settings for more information.Wait until your server launches.
Detach from the Tmux session by pressing Ctrl + B then D.
Test the Stable Diffusion Web UI
- In your browser, navigate to port
7860
at your server's IP address. For example,http://192.0.1.2:7860
. - Under the Stable Diffusion checkpoint dropdown menu, select
model.ckpt
. - Select the txt2img tab.
- Enter any text description in the Prompt text field.
- Increase or decrease the sample steps to generate better or poorer images. Higher sample steps produce better images but take more time to generate one. Lower sample steps generate much faster but produce poorer images.
- To learn more about the different sampling methods, seed resize, and other features, please see the Stable Diffusion Web UI Features Documentation.
- You may increase or decrease the image's size by adjusting the width and the height.
- Click Generate to start producing the image.
- Please see the image below for an example.
You have successfully created a Stable Diffusion Web UI on a Vultr Cloud GPU.
More Information
To learn more about Stable Diffusion, please see these resources: