π This is for testing purposes. It is using Docker. It will be slow and may crash. I suggest you read the FAQ and Docker Pre-requisites sections before jumping into this.
Stable Diffusion Prompt: A beautifully colored cat sitting in the clouds with a rainbow in the background, in the style of Andy Warhol |
Like this? β
Are you already familiar with Docker, GitHub, Visual Studio Code, etc and want to start off fast? Check out my
The GitHub user AUTOMATIC1111 maintains a repo that allows you to run Stable Diffusion locally on your computer with a web interface. Here is the direct link to the repo: https://github.com/AUTOMATIC1111/stable-diffusion-webui
π Stable Diffusion Web UI = SDWUI or SDUI: I will refer to the Stable Diffusion Web UI as SDWUI (or SDUI) throughout this gist.
Since I do not like installing lots of software and dependencies directly on my computer, I run my code in a Docker container.
Using Docker containers is much easier with Visual Studio Code's Docker and Dev Containers extensions, so I will use those as well.
- Run AUTOMATIC1111's Stable Diffusion Web UI on a Mac M1 using Docker and Visual Studio Code
- FAQ
- My computer setup
- GitHub setup
- Docker setup
- Configure Visual Studio Code (VSC)
- Create a local repo and set up the Dockerfile
- Clone the SDWUI repo
- Create a non-root user
- Download the Stable Diffusion model
- β‘οΈ Before you run the app
- Edit the COMMAND_LINE_ARGS in
launch.py
- π RUN SDWUI
- Close SDWUI when you are done
- Use SDWUI again
- π‘ Tip: Create a script to run each time
Q: Will Stable Diffusion be super fast? No
Q: Can I ask you to create more scripts and test on other machines and GPUs? You can ask! I am currently doing this for research, but if you want to purchase the hardware for me and pay for my services to get things up and running, I would be open to a conversation (but no commitments)
Q: Do I need Docker? No, but you do for this guide. To the best of my knowledge, you can install the software on Mac, Windows, and Linux. I like Docker I dislike installing lots of software development code on my machine
Q: Do I need Visual Studio Code (VSC)? No, but you do for this guide. I like using VSC because it has excellent Docker extensions
Q: How long will this take? Set aside a few hours to get set up. After that, SDWUI should run fairly quickly. Creating images will take some time though. As a I said, it will not be super fast
Q: Did you test everything that SDWUI has to offer? No. I only tested text2img
prompt generations
Q: If I encounter an issue, can I ask you? If my instructions are incorrect or could be improved, feel free to reach out. If you are having general issues or encountering bugs, please reach out to any of the active communities first
Q: Are you in any way affiliated with Docker, Visual Studio Code, Stable Diffusion, Automatic1111, or the other products you list here: No, or at least not at the time of this writing. I am learning machine learning and AI.
Q: Have any good image prompts? A beautifully colored cat sitting in the clouds with a rainbow in the background, in the style of Andy Warhol
Q: Why is it after I run the SDWUI and open the browser link, I get an error when I type a prompt? This is because the models and weights are still loading. Keep an eye on the terminal window and wait for the weights and models to finish loading
Q: Why do I have to modify the launch.py
COMMANDLINE_ARGS? CUDA and some other features are not supported in this Docker/macOS setup. I slowly added the arguments listed in this article until I could successfully run the code. Do not feel limitd by what I recommend. Feel free to change, add, or remove my settings. Also, I found a list of -- what I believe to be -- official COMMANDLINE_ARGS, so you can check them out here.
Q: Why does the terminal say Killed.
sometimes? This is likely due to the need to allocate more resources to Docker. See the Docker section to learn more. Here are some other things to try:
- Freeing up disk space in the container itself. Run the terminal and as the root user, type:
apt -y autoremove && apt autoclean
and press return on your keyboard - It never hurts to quit unused apps and even restart your computer if this persists. There could be other reasons and I am just too new to know
- Try deleting the virtual environmnet. In VSC, open the Docker container. If the terminal is running, click the ποΈ
trashcan
icon to stop any services from running. Click the πExplorer
icon and locate thestable-diffusion-web-ui/venv
folder. Option-click (right-click) the folder and delete it. The next time you runwebui.sh
(or the script I provide later in this article), the environment will rebuild. Seems a little dramatic to do this, but it worked for me recently!
Q: I like this! How can I support you? β
Later I will show you how my Docker resources are set up, but here is my desktop so you are aware of what I am workinng with:
- MacBook Pro
- M1 Pro
- Ventura
- Memory: 32GB
- Drive: 2TB (1 TB free)
The instructions I provide are based on my experience and resolve some of the most common errors I have run into.
π For purposes of these instructions, you only need to create a free GitHub account. If you have already done so, skip this step
- Go to https://github.com and create a free account
- Follow any steps (like email verifications) to make sure you can successfuly login to GitHub
- If you have not already done so, go to https://docker.com. Download the
Apple Chip
version of the desktop app - Create a Docker account, install the app locally and sign in
- (optional) If you use Docker already, now would be a good time to delete any unused containers or images because this stuff is memory and disk intensive
- Run Docker Desktop and click the βοΈ
settings
icon - Select
Resources
β οΈ System resources: These changes can impact the performance of your comptuer, so modify at your own risk. I suggest bumping settings up nice and high and then slowly working your way down after you get SDWUI running
- At the end of these instructions, when you run the SDWUI, you might run into a situation where the app simply says
killed
(see FAQs for more details). I had to bump up my settings to support them, so here are the big changes I made:
- CPUs:
8GB
- Memory:
16GB
- Virtual disk limit
104GB
(probably the most important) - Swap:
4GB
- You will have to play with these settings based on your computer's specs, what you are running in the background, and any number of other details.
The following image shows the settings I am using on my computer:
These are my settings, but were not changed specifically for SDWUI |
- Download the stable macOS Universal Visual Studio Code app here
- Install VSC per the instructions
- Run VSC for the first time and select the
Extensions
icon. Search for and install the following extensions:
- Docker (Microsoft)
- Dev Containers (Microsoft)
Install the Dev Containers extension from Microsoft |
Install the Docker extension from Microsoft |
- Quit and restart VSC (may not be necessary, but it is something I did)
π You can create the
automatic1111
folder anywhere you want and name it whatever you want. For these instructions, I am creating the folder with that name on my desktop.
- On your desktop, create a new folder and name it
automatic1111
- Run VSC and open the
automatic1111
folder - Create a new file and name it
Dockerfile
- Copy and paste the following code:
π Ubuntu Linux: Ubuntu is based on Debian Linux, which SDWUI supports. You can feel free to try others, but I cannot gaurantee the instructions will work. Also, you could modify the first line to
FROM ubuntu:latest
to get the latest OS. I do not do that here because I tested with a specific version
π Time zone: You can modify the TZ (timezone) to anything you like
π GitHub settings: On the last lines, update the Git settings with those you would like to use
FROM ubuntu:22.04
# These settings prevent a timezone prompt when Python installs
ENV TZ=US/Pacific \
DEBIAN_FRONTEND=noninteractive
RUN echo "#################################################"
RUN echo "Get the latest APT packages"
RUN echo "apt-get update"
RUN apt-get update
# Install AUTOMATIC1111 pre-requisites
RUN apt-get install -y \
cmake \
rustc \
git-all \
wget \
apt-utils \
jq
RUN apt -y autoremove && apt autoclean
# Install Python pre-requisites, including Python 3.x
# Google perftools includes TCMalloc, which helps with CPU memory usage
RUN apt-get install -y \
software-properties-common \
python3 \
python3-pip \
python3-ipykernel \
libopencv-dev \
python3-opencv \
python3.10-venv \
google-perftools \
sudo
RUN apt -y autoremove && apt autoclean
# Install tensorflow. Note that AWS manages Linux distributions
# RUN pip install tensorflow-cpu-aws pyyaml h5py
# Configure git
RUN git config --global user.name "Some One" &&\
git config --global user.email [email protected] &&\
git config --global init.defaultBranch main
- Save the file
- Type
SHIFT+COMMAND+P
on your keyboard. A menu appears. TypeDev Containers: Rebuild and Reopen in Container...
. PressEnter
on your keyboard - An Add Dev Container Configuration Files dialog appears. Select
From 'Dockerfile'
- An Select Features dialog appears. Do not select any features. Select the OK button
- β³ Wait for the docker image and container to build. This could take a long time! Next time, it will open nice and fast though
- You should now be working within the Dev (Docker) Container. The easiest way to check is to look at the bottom of VSC and look for some
Dev container:
text. You can see mine in blue in the image below:
π Make sure you have VSC running and the
automatic1111
container is open, per the previous instructions
- In VSC, type
CONTROL+BACKTICK
(usually it is the key with the ~ above it). That will open a bash terminal window that should look like the following image:
git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui
cd /workspaces/automatic1111/stable-diffusion-webui
git config --global --add safe.directory "*"
Once complete, you will have a folder with the name stable-diffusion-webui
as shown in the following image:
You have successfully cloned the SDWUI code from GitHub to your local computer |
When you create the Docker file, you are automatically logged in as the root user. SDWUI does not allow you to run with that user, so you must create your own.
π Create any username you want For purposes of this demo, I am going to create a user that is easy to type and call it
sdwui
, but feel free to use any name you like.
- With the terminal open in VSC, create the new user and then login as the new user. Press return on your keyboard after each line (ignore the comments starting with #):
# Create a new user
useradd -s /bin/bash -d /home/sdwui/ -m -G sudo sdwui
# Login as that user
su sdwui
You should now be logged in as the sdwui
user as shown in the image taken from the terminal in VSC below:
Verify you are logged in with the new sdwui user. There may be an @ sign with extra characters, which is okay. |
β οΈ Unless specified, always use sdwui When you exit and then open the VSC terminal window, you will always be logged in asroot
. Unless otherwise specified in these instructions, always typesu sdwui
before continuing.
For purposes of this gist, you will download a specific version of Stable Diffusion as the model that SDWUI will use. You can feel free to download any model (or model version) you like.
β As of this writing, the model is >3GB. This process will take a long time.
π If you find the model does not download, download the one you want from this list and then paste it into the appropriate models folder.
- With the dev container running, the terminal window open, the user
sdwui
is logged in, type the following commands, pressing return after each line (ignore the lines with a # in them).
# Go to the folder where SDWUI expects the model to exist
cd /workspaces/automatic1111/stable-diffusion-webui/models/Stable-diffusion
# Download the model
curl -LJO "https://huggingface.co/runwayml/stable-diffusion-v1-5/resolve/main/v1-5-pruned-emaonly.ckpt"
- You will know the model is downloaded after the command prompt returns in the termminal and there are no errors.
- To make sure the file is downloaded, you can also select it from the VSC explorer as shown in the image below. Do not open the file. You just want to be sure it is >3GB
The file should be >3GB and the terminal should have no errors |
It is important to know that SDWUI is an amalgamation of custom code and dependencies from many other code repos. Further, SDWUI can run with or without CUDA, which is am NVIDIA programming language for their GPUs.
Despite our running Ubuntu Linux in a Docker container, Docker uses many of the resources provided by the host operating system. In this case, the host OS is macOS, which does not have the option to use CUDA. If you want to rant about why CUDA is not available on macOS, please find another place to do that. I am just the messenger.
To make things even more interesting, SDWUI works on Windows, Macs, and Debian Linux variants (like Ubuntu), so the app provides various files for different operating systems. For purposes of this setup, you will be using the Linux files.
The reason I share all this is because of the following:
- You will be turning off some features that the macOS host will not support
- After you get the system up and running, feel free to play with the settings to see if your Mac and Docker version support them! Check out the FAQ for more details, especially those relating to Docker and COMMANDLINE_ARGS,
As mentioned previously, there are certain settings that this combination of Docker, Linux, and macOS do not support. That is why you have to modify the command line arguments that SDWUI uses in order to run.
- In VSC, select the π Explorer icon
- Expand the
stable-diffusion-webui
and then select thelaunch.py
file - The editor should now show the contents of the file. Locate the line
commandline_args = os.environ.get('COMMANDLINE_ARGS', "")
, and modify it as follows:
commandline_args = os.environ.get('COMMANDLINE_ARGS', "--skip-torch-cuda-test --opt-sdp-attention --precision full --no-half")
- Save the file (but I recommend you keep it open, because you will probably need to edit it!)
The following image shows a sample of what the new file should look like. Reminder to save the file!!
The launch.py file with updated COMMANDLINE_ARGS |
β οΈ Before you begin, make sure you are running VSC with the Docker dev container running and you are logged in as the usersdwui
. You will also need to be connected to the internet.
- Type the following lines in the terminal, pressing return on your keyboard after each line:
cd /workspaces/automatic1111/stable-diffusion-webui
git config --global --add safe.directory "*"
./webui.sh
π safe directory: I know you already ran the
git config --global --add safe.directory "*"
command already, but you should do this every time you run SDWUI.
SDWUI will take some β time to download and install a number of dependencies. Be patient because it can take a while.
If, at the end of running ./webui.sh
you see a URL, use your mouse to COMMAND+CLICK
the URL (like http://127.0.0.1:7860
) and you can start using Stable Diffusion on your computer! You can see that URL in the folloiwng image:
COMMAND+Click the url or copy and paste it into your broswer of choice |
When you open the URL on your locate computer's browser, you will be presented with the Stable Diffusion web user interface. As you can see in the following image, you may need to select the model and wait a bit for that to prepare. When ready, select the txt2img
tab to type a prompt, like A cat sitting on puffy clouds, hyperrealistic
. Finally, press the Generate
button and after some more β patience, you will receive an image!
When you run SDWUI, you will first select the checkpoint (model), then type a prompt, and finally generate a prompt |
If you want to make sure the model is running and check progress, you can always go back to VSC and watch the terminal window. In the following image, I ran another prompt and can see it is 15% complete done.
View detailed prompt generation status in VSC |
Follow these steps to nicely close up SDWUI and free up memory on your computer:
- Kill the terminal session: Go to VSC and click the ποΈ
trash
icon in the terminal area - Close the remote dev container: Type
SHIFT+COMMAND+P
on your computer and typeClose remote connection
to close the Dev Container. In general, Docker will shut down the container after a few minutes - Quit VSC
Follow these steps if you quit VSC and want to start using it again:
- Run VSC
- Open the Docker dev container: Type
COMMAND+SHIFT+P
on your keyboard and then typeOpen folder in container...
, then, if you followed these instructions, selectDesktop->automatic1111
and select the Open button - Open the terminal: Type
CONTROL+BACKTICK
(usually the same key containing the ~) - Type the following code into the terminal window, pressing return on your keyboard after each line:
su sdwui
cd /workspaces/automatic1111/stable-diffusion-webui
git config --global --add safe.directory "*"
./webui.sh
To make life easier, you can create a script that will automatically run all the important steps for you.
- Run VSC and use the
Open folder in container..."
option as before - Create two new files under the
automatic1111
folder (not the stable-diffusion-webui folder). Name them:
runsdwui.sh
sdwui.sh
- Open the
runsdwui.sh
file and add the following code:
su sdwui "sdwui.sh"
- Save the file and close it
- Open the
sdwui.sh
file and add the following code:
cd /workspaces/automatic1111/stable-diffusion-webui
git config --global --add safe.directory "*"
./webui.sh
- Save and close the file
You will now have two files at the root of your folder, as you can see in the following image:
Now, you can run a single command to start up SDWUI |
- Open VSC and then use the
Open folder in remote container...
option, selecting theDesktop->automatic1111
folder - Open the terminal window with
CONTROL+BACKTICK
(usually the key with the ~ symbol) - Type
sh runsdwui.sh
(orbash runsdwui.sh
) - When SDWUI runs, locate the local url and
COMMAND+CLICK
the link (reminder, it will probably behttp://127.0.0.1:7860
)
β What about the sdwui user?: When you run the
runsdwui.sh
, it will login as thesdwui
user for you and then run all the commands you need to get up and running.
Like this? [βοΈ](buymeacoffee.com/williameraymond}