Skip to content

Instantly share code, notes, and snippets.

@manniru
Forked from BillRaymond/README.md
Created December 2, 2023 18:26
Show Gist options
  • Save manniru/4be03d8cf22f0627e9390f0510a9e40c to your computer and use it in GitHub Desktop.
Save manniru/4be03d8cf22f0627e9390f0510a9e40c to your computer and use it in GitHub Desktop.
Run AUTOMATIC1111's Stable Diffusion Web UI in Docker and VSC on Mac M-series chips

Run AUTOMATIC1111's Stable Diffusion Web UI on a Mac M1 using Docker and Visual Studio Code

πŸ‘‰ 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.

FAQ

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 the stable-diffusion-web-ui/venv folder. Option-click (right-click) the folder and delete it. The next time you run webui.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? β˜•

My computer setup

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.

GitHub setup

Pre-requisites

πŸ“ For purposes of these instructions, you only need to create a free GitHub account. If you have already done so, skip this step

  1. Go to https://github.com and create a free account
  2. Follow any steps (like email verifications) to make sure you can successfuly login to GitHub

Docker setup

Pre-requisites

  1. If you have not already done so, go to https://docker.com. Download the Apple Chip version of the desktop app
  2. Create a Docker account, install the app locally and sign in
  3. (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

Configure Docker

  1. Run Docker Desktop and click the βš™οΈ settings icon
  2. 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

  1. 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

Configure Visual Studio Code (VSC)

Pre-requisites

  1. Download the stable macOS Universal Visual Studio Code app here
  2. Install VSC per the instructions

Configure VSC

  1. 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
  1. Quit and restart VSC (may not be necessary, but it is something I did)

Create a local repo and set up the Dockerfile

πŸ“ 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.

  1. On your desktop, create a new folder and name it automatic1111
  2. Run VSC and open the automatic1111 folder
  3. Create a new file and name it Dockerfile
  4. 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 some.one@some.one &&\
    git config --global init.defaultBranch main

  1. Save the file
  2. Type SHIFT+COMMAND+P on your keyboard. A menu appears. Type Dev Containers: Rebuild and Reopen in Container.... Press Enter on your keyboard
  3. An Add Dev Container Configuration Files dialog appears. Select From 'Dockerfile'
  4. An Select Features dialog appears. Do not select any features. Select the OK button
  5. ⏳ Wait for the docker image and container to build. This could take a long time! Next time, it will open nice and fast though
  6. 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:

Screenshot 2023-05-23 at 12 00 22 PM

Clone the SDWUI repo

πŸ“ Make sure you have VSC running and the automatic1111 container is open, per the previous instructions

  1. 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:

Screenshot 2023-05-23 at 12 13 29 PM

2. Type the following command in the terminal and then press return on your keyboard:
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

Create a non-root user

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.

  1. 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 as root. Unless otherwise specified in these instructions, always type su sdwui before continuing.

Download the Stable Diffusion model

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.

  1. 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

➑️ Before you run the app

A little about SDWUI

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,

Edit the COMMAND_LINE_ARGS in launch.py

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.

  1. In VSC, select the πŸ“„ Explorer icon
  2. Expand the stable-diffusion-webui and then select the launch.py file
  3. 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")
  1. 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

πŸŽ‰ RUN SDWUI

⚠️ Before you begin, make sure you are running VSC with the Docker dev container running and you are logged in as the user sdwui. You will also need to be connected to the internet.

  1. 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

Close SDWUI when you are done

Follow these steps to nicely close up SDWUI and free up memory on your computer:

  1. Kill the terminal session: Go to VSC and click the πŸ—‘οΈ trash icon in the terminal area
  2. Close the remote dev container: Type SHIFT+COMMAND+P on your computer and type Close remote connection to close the Dev Container. In general, Docker will shut down the container after a few minutes
  3. Quit VSC

Use SDWUI again

Follow these steps if you quit VSC and want to start using it again:

  1. Run VSC
  2. Open the Docker dev container: Type COMMAND+SHIFT+P on your keyboard and then type Open folder in container..., then, if you followed these instructions, select Desktop->automatic1111 and select the Open button
  3. Open the terminal: Type CONTROL+BACKTICK (usually the same key containing the ~)
  4. 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

πŸ’‘ Tip: Create a script to run each time

To make life easier, you can create a script that will automatically run all the important steps for you.

To create the script

  1. Run VSC and use the Open folder in container..." option as before
  2. Create two new files under the automatic1111 folder (not the stable-diffusion-webui folder). Name them:
runsdwui.sh
sdwui.sh
  1. Open the runsdwui.sh file and add the following code:
su sdwui "sdwui.sh"
  1. Save the file and close it
  2. Open the sdwui.sh file and add the following code:
cd /workspaces/automatic1111/stable-diffusion-webui
git config --global --add safe.directory "*"
./webui.sh
  1. 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

To use the scipt

  1. Open VSC and then use the Open folder in remote container... option, selecting the Desktop->automatic1111 folder
  2. Open the terminal window with CONTROL+BACKTICK (usually the key with the ~ symbol)
  3. Type sh runsdwui.sh (or bash runsdwui.sh)
  4. When SDWUI runs, locate the local url and COMMAND+CLICK the link (reminder, it will probably be http://127.0.0.1:7860)

❓ What about the sdwui user?: When you run the runsdwui.sh, it will login as the sdwui user for you and then run all the commands you need to get up and running.

Like this? [β˜•οΈ](buymeacoffee.com/williameraymond}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment