As we shared in the posts earlier Stable Diffusion is quite the hype at the moment. There are numerous Google Colab notebooks available for anyone to use (you need a Google Colab subscription) but as the Stable Diffusion model is open source people have discovered many different ways to run Stable Diffusion on their PCs.
Now in the post we share how to run Stable Diffusion on a M1 or M2 Mac
Minimum Requirements
- A Mac with M1 or M2 chip.
- 16GB RAM or more.
- macOS Monterey 12.3 or higher.
Setup
Open the Terminal and follow the following steps. Install Homebrew using the command below, unless you already have Python 3.10 already installed on your Mac. You can check your version using the command (-V is capital): python3 -V
or python3 --version
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Run the below commands to ensure you have latest versions if you already have Python or HomeBrew installed before.
brew update
brew install python
Next step is to clone the Stable Diffusion repository and setup the virtual environment
git clone -b apple-silicon-mps-support https://github.com/bfirsh/stable-diffusion.git
cd stable-diffusion
mkdir -p models/ldm/stable-diffusion-v1/
python3 -m pip install virtualenv
python3 -m virtualenv venv
Activate the virtualenv just created. Afterwards whenever you want to run Stable Diffusion you will need to run this
source venv/bin/activate
brew install cmake protobuf rust
UPDATE: 29 Sept – Some people have shared that using ‘pip install protobuf==3.19.4’ has helped resolve their errors, so there must be some incompatibility with latest version. So you could modify the above command to be: brew install cmake protobuf==3.19.4 rust
or run it separately as well but then exclude it from above.
Install the dependencies using:
pip install -r requirements.txt
Now this step you need to do in your browser. Navigate to the site Hugging Face, register if you don’t have an account as you need this to download the Stable Diffusion model.
Download sd-v1-4.ckpt
(4 GB) from the site and save it with the name “model.ckpt” under models/ldm/stable-diffusion-v1/model.ckpt
in the directory you created above.
Run Stable Diffusion
Now you are ready to run Stable Diffusion and start creating your own art.
python scripts/txt2img.py --prompt "a beautiful dense amazon forest with tall trees and thick cannopy, moss on tree trunks and rocks, big rocks on the ground, a lonely boy stands staring into the path that leads into the forrest, trending on Artstation" --n_samples 1 --n_iter 1 --plms
Generated images will appear in folder outputs/txt2img-samples/
. You can also fine tune and use other parameters in the script by referring to the details provided on this page.

If you'd like to support our site please consider buying us a Ko-fi, grab a product or subscribe. Need a faster GPU, get access to fastest GPUs for less than $1 per hour with RunPod.io
Couple of suggestions to improve this very helpful blog:
– Checking for installed Python should be “python3 –version”. The suggested “python” command only existed on my machine while I was inside a virtual env, on the pure Terminal I only had “python3”
– Installing the requirements.txt fails due to missing cmake and protobuf packages. Using “brew install cmake” and “brew install protobuf” beforehand fixes this.
This guide mentions a requirements.txt, but that doesn’t exist in the SD git repo (‘any more’ maybe?) I found one on https://gist.github.com/wfng92/0a41b9a13563e9bc67fd5c1fdc8b1ae5 , but I was wondering which requirements.txt is meant here. Thanks.
You need to run this – brew install cmake protobuf rust
before requirement.txt command. See updated blog post above
This always fails for me at the requirements installation step on an M1 Mac Studio: it can’t install onnx:
Collecting onnx
Downloading onnx-1.12.0.tar.gz (10.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.1/10.1 MB 38.9 MB/s eta 0:00:00
Preparing metadata (setup.py) … error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [7 lines of output]
fatal: not a git repository (or any of the parent directories): .git
Traceback (most recent call last):
File “”, line 2, in
File “”, line 34, in
File “/private/var/folders/kh/20pq03r56gq51znz9q7z1sgc0000gn/T/pip-install-g5c_s75t/onnx_a46f0d9f2a3a4de98469d44fd72a1de0/setup.py”, line 81, in
assert CMAKE, “Could not find cmake executable!”
AssertionError: Could not find cmake executable!
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
You need to run this – brew install cmake protobuf rust
before requirement.txt command. See updated blog post above
Yep, it works now. Please feel free to delete by comment, since it’s no longer accurate. Thanks for your help!
Thank you @harmeet for the help. Please consider leaving in @Chris’ comment because it helps people like me who where following instructions on another site, but ran into the same problem.
I have no idea what I am doing, but…
source venv/bin/activate doesn’t appear to point to the correct directory given that all the scripts above create files under ~/stable-diffusion and not just right under ~/ (the user folder). I can adjust “source venv/bin/activate” to “source ~/stable-diffusion/venv/bin/activate” to compensate, but even then I still get this error:
Traceback (most recent call last):
File “~/scripts/txt2img.py”, line 2, in
import cv2
ModuleNotFoundError: No module named ‘cv2’
I’ve gotten a similar error on other attempts of this, on other computers, and seen other users get stuck on this error as well. Are there any fixes?
Not sure I can help with this. Other readers have got it working
I got this – then fixed it by making sure that before I run
“source venv/bin/activate
brew install cmake protobuf rust”
I am in the stable-diffusion directory……i.e. when you open terminal, first type cd stable-diffusion, then type the commands above to set up the virtual environment
try pip install opencv-python
looks like opencv is still missing
Done all of the above – run it gets to
Running PLMS Sampling with 50 timesteps
PLMS Sampler: 0%| | 0/50 [00:04<?, ?it/s]
data: 0%| | 0/1 [00:16<?, ?it/s]
Sampling: 0%|
Then kicks out an error with a load of source information, but ultimately the error is
File "/Users/brucegeddes/stable-diffusion/venv/lib/python3.9/site-packages/torch/nn/functional.py", line 2524, in group_norm
return torch.group_norm(input, num_groups, weight, bias, eps, torch.backends.cudnn.enabled)
RuntimeError: expected scalar type BFloat16 but found Float
Any ideas? – could this be that it is going into a Python 3.9 file? I have Python 3.10 installed, so not sure how to force it to start using that
same to me.
https://huggingface.co/CompVis/stable-diffusion-v1-4/discussions/42
adding “–precision full” as argument helps.
I followed the steps but cant find the output folder
Hi I don’t usually make a comment, but thank you! I was hoping to get this on my own, 2 nights ago when I first heard about the whole compViz from somewhere but your guide set me on my way.
When I try to run the command with requirements.txt, things seem like they are going well but then I reach an error:
Building wheels for collected packages: tokenizers, onnx, grpcio
Building wheel for tokenizers (pyproject.toml) … error
error: subprocess-exited-with-error
× Building wheel for tokenizers (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [2002 lines of output]
It creates a series of errors that ends with:
Could not find . This could mean the following:
* You’re on Ubuntu and haven’t run `apt-get install python3-dev`.
* You’re on RHEL/Fedora and haven’t run `yum install python3-devel` or
`dnf install python3-devel` (make sure you also have redhat-rpm-config
installed)
* You’re on Mac OS X and the usual Python framework was somehow corrupted
(check your environment variables or try re-installing?)
* You’re on Windows and your Python installation was somehow corrupted
(check your environment variables or try re-installing?)
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for grpcio
Running setup.py clean for grpcio
Failed to build tokenizers onnx grpcio
ERROR: Could not build wheels for tokenizers, which is required to install pyproject.toml-based projects
I have tried reinstalling Python but it’s no good. If I try to use Stable Diffusion anyway, I get the following error:
ModuleNotFoundError: No module named ‘torch’
(I used to have the CV2 error but a comment above allowed me to fix it). I am guessing this error relates to not being able to get the full build completed. I have tried reinstalling python and also verified and re-verified that cmake, protobuf, and rust are installed. Any thoughts?
STEP: “pip install -r requirements.txt” I get this error, thoughts?
——————————–
File “”, line 204, in get_source_files
File “/private/var/folders/tw/zc55jdrd6l97j046l5bk3d4r0000gn/T/pip-build-env-67c96r1c/overlay/lib/python3.8/site-packages/setuptools/_distutils/cmd.py”, line 105, in __getattr__
raise AttributeError(attr)
AttributeError: cython_sources
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
I should have been more clear with the error: during this step (pip install -r requirements.txt)
Collecting PyYAML
Using cached PyYAML-6.0.tar.gz (124 kB)
Installing build dependencies … done
Getting requirements to build wheel … error
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
I have the same error. Has someone found a solution?
I get the same PyYaml/cython error
also getting this same error when it gets to PyYAML — how to fix this, pleas?
Try this: pip install –upgrade pip before running the command pip install -r requirements.txt
NOTE: I found this solution proposed on some forums related to this error…so I have not tested it.
Yeah this did not work for me and is now botched.
Why do you install python 3.10 before creating a virtual environment. Many people have lots of version of Python that they need and if you install Python 3.10 from Brew, without creating and activating an environment first, what does this do to Conda and other python installations?
It turns out, although I had many versions of Python installed, the python3 –version command (the 3 is important – very confusing because python –version seems to work too) showed that Python3.8 was being used -it has to be 3.10. I deleted everything and started again with a brew install and then all the requirements in the txt file installed fine. The Conda environments I had seem unaffected.
Perhaps virtualenv venv310 -p python3.10 would have been a better command to use as it would install python 3.10 in only the new environment and not affect anything else?
Did doing it your way work?
I’m also getting the requirements.txt issue. I did `source venv/bin/activate`
my terminal looks like this: `(venv) (base) ramsey@decentration stable-diffusion %`
and then `brew install cmake protobuf rust `
“`
Warning: cmake 3.24.1 is already installed and up-to-date.
To reinstall 3.24.1, run:
brew reinstall cmake
Warning: protobuf 21.5 is already installed and up-to-date.
To reinstall 21.5, run:
brew reinstall protobuf
Warning: rust 1.63.0 is already installed and up-to-date.
To reinstall 1.63.0, run:
brew reinstall rust
“`
but i get:
“`
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirements.txt’
“`
I fixed that (i think) by ` pip3 freeze > requirements.txt` first then pip install -r requirements.tx after
but then i when i run… `python scripts/txt2img.py –prompt “makeup monster lips on a brown skin face” –n_samples 1 –n_iter 1 –plms`
i get error:
“`
Traceback (most recent call last):
File “/Users/ramsey/decentration/stable-diffusion/scripts/txt2img.py”, line 8, in
from imwatermark import WatermarkEncoder
“`
What happens when you type python3 –version in the venv?
note that’s a double dash
python – -version
— version or -V both work
Hi, I have the same issue. python3 -V gives me Python 3.10.6
When the grpcio step fails, what to do? M1 mac mini.
https://github.com/grpc/grpc/issues/30064 says to try
GRPC_PYTHON_BUILD_SYSTEM_OPENSSL=true GRPC_PYTHON_BUILD_SYSTEM_ZLIB=true pip install grpcio
but this failed too.
I did all of the above and it run / setups well.
python3.10.6
did the brew installs before the requirements…
all went without any errors- but when I try to run the script I get this error:
stable-diffusion % python scripts/txt2img.py –prompt “a beautiful dense amazon forest with tall trees and thick cannopy, moss on tree trunks and rocks, big rocks on the ground, a lonely boy stands staring into the path that leads into the forrest, trending on Artstation” –n_samples 1 –n_iter 1 –plms
Traceback (most recent call last):
File “/Users/Python/stable-diffusion/scripts/txt2img.py”, line 13, in
….
File “/Users/Python/stable-diffusion/venv/lib/python3.10/site-packages/google/protobuf/descriptor.py”, line 47, in
from google.protobuf.pyext import _message
ImportError:
dlopen(/Users/Python/stable-diffusion/venv/lib/python3.10/site-packages/google/protobuf/pyext/_message.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace (__ZN6google8protobuf15FieldDescriptor12TypeOnceInitEPKS1_)
I’m stuck at the same spot, and haven’t been able to figure out what to do. Has anyone got around this?
I got the exact same error, but what seemed to solve this was downgrading protobuf to version 3.20.1. Did this through:
pip uninstall protobuf
pip install protobuf==3.20.1
It looks like the protobuff 3.19.5 update is causing the issue you mention. If you downgrade to 3.19.4 (pip install protobuf==3.19.4) it got past that for me.
Hey, exactly the same for me… no errors in any of the steps, everything goes smoothly. But trying to run the test prompt results in the same error for me.
Any ideas?
Thanks!
That fixed it for me -> ‘pip install protobuf==3.19.4’ ( https://github.com/protocolbuffers/protobuf/issues/10571 )
Hi,
i get the following error … 🙁
python scripts/txt2img.py –prompt “a beautiful dense amazon forest with tall trees and thick cannopy, moss on tree trunks and rocks, big rocks on the ground, a lonely boy stands staring into the path that leads into the forrest, trending on Artstation” –n_samples 1 –n_iter 1 –plms
Traceback (most recent call last):
File “/*filtered*/stable-diffusion/scripts/txt2img.py”, line 13, in
from pytorch_lightning import seed_everything
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/__init__.py”, line 21, in
from pytorch_lightning.callbacks import Callback # noqa: E402
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/callbacks/__init__.py”, line 24, in
from pytorch_lightning.callbacks.pruning import ModelPruning
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/callbacks/pruning.py”, line 31, in
from pytorch_lightning.core.lightning import LightningModule
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/core/__init__.py”, line 16, in
from pytorch_lightning.core.lightning import LightningModule
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/core/lightning.py”, line 41, in
from pytorch_lightning.trainer.connectors.logger_connector.fx_validator import FxValidator
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/trainer/__init__.py”, line 18, in
from pytorch_lightning.trainer.trainer import Trainer
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py”, line 31, in
from pytorch_lightning.loggers import LightningLoggerBase
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/loggers/__init__.py”, line 18, in
from pytorch_lightning.loggers.tensorboard import TensorBoardLogger
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/pytorch_lightning/loggers/tensorboard.py”, line 25, in
from torch.utils.tensorboard import SummaryWriter
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/torch/utils/tensorboard/__init__.py”, line 12, in
from .writer import FileWriter, SummaryWriter # noqa: F401
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/torch/utils/tensorboard/writer.py”, line 9, in
from tensorboard.compat.proto.event_pb2 import SessionLog
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/tensorboard/compat/proto/event_pb2.py”, line 8, in
from google.protobuf import descriptor as _descriptor
File “/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/google/protobuf/descriptor.py”, line 47, in
from google.protobuf.pyext import _message
ImportError: dlopen(/*filtered*/stable-diffusion/venv/lib/python3.10/site-packages/google/protobuf/pyext/_message.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace (__ZN6google8protobuf15FieldDescriptor12TypeOnceInitEPKS1_)
Couple questions:
1) Has anybody been able to get img2img to work (the image upscaler/enhancer)? When I run it, it gives me a bunch of additional requirements it wants, and when I install them, complains about something in code. I don’t have it in front of me at the moment, but I can get the exact errors if necessary.
2) Is there an easy way to get this to update or auto-update? I’d like to get the –seamless option for generating tilable images.
Hey, does anybody has an idea why it tells me: ” zsh: permission denied:”?
It comes up when I want to rename and -direct the downloaded file to “models/ldm/stable-diffusion-v1/model.ckpt”
Thanks so much! I followed your steps and got it working.
No matter what I try, I run into the same cv2 error.
In fact, I run into this error no matter what tutorial I follow. I’ve tried multiple guides using conda, venv, jupyter etc. Each time the same final result:
File “/Users/******/MAIN/stable-diffusion/scripts/txt2img.py”, line 2, in
import cv2
ModuleNotFoundError: No module named ‘cv2’
I’ve installed open-cv and I’m using the correct interpreter (all other packages are fine):
opencv-contrib-python 4.7.0.68
opencv-python 4.7.0.68
opencv-python-headless 4.7.0.68
Any help?
I had to set diffusers==0.12.1 in requirements.txt per https://www.reddit.com/r/StableDiffusion/comments/115fmc9/cannot_import_name_safe_weights_name_from/
Hi i have been trying to install it but i am still getting this error:
v=ERROR: Cannot install -r requirements.txt (line 11), -r requirements.txt (line 16), -r requirements.txt (line 18), -r requirements.txt (line 19) and -r requirements.txt (line 8) because these package versions have conflicting dependencies.
The conflict is caused by:
invisible-watermark 0.1.5 depends on onnxruntime
invisible-watermark 0.1.4 depends on onnxruntime
invisible-watermark 0.1.3 depends on onnxruntime
invisible-watermark 0.1.2 depends on onnxruntime
pytorch-lightning 1.4.2 depends on torch>=1.6
torch-fidelity 0.3.0 depends on torch
torchmetrics 0.6.0 depends on torch>=1.3.1
kornia 0.6.0 depends on torch>=1.8.1
invisible-watermark 0.1.1 depends on torch==1.0.1.post2
pytorch-lightning 1.4.2 depends on torch>=1.6
torch-fidelity 0.3.0 depends on torch
torchmetrics 0.6.0 depends on torch>=1.3.1
kornia 0.6.0 depends on torch>=1.8.1
invisible-watermark 0.1.0 depends on torch==1.0.1.post2
To fix this you could try to:
1. loosen the range of package versions you’ve specified
2. remove package versions to allow pip attempt to solve the dependency conflict
ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts
I got the same errors =(((
I think a few commenters mentioned alternate command to get past the error. Did you try that?
Stable diffusion installation
I found in this topic (https://github.com/microsoft/onnxruntime/issues/11037) workaround for installing onnxruntime, that required for stable diffusion but not available for M1/M2 Macs via brew. To fix this I had to install anaconda.
Install Anaconda through Homebrew
1. Run brew install –cask anaconda to install Anaconda
2. Run echo ‘export PATH=/usr/local/anaconda3/bin:$PATH’ >> ~/.zshrc from your terminal
3. Also run echo ‘export PATH=/opt/homebrew/anaconda3/bin:$PATH’ >> ~/.zshrc from your terminal
4. Run source ~/.zshrc from your terminal
5. Type conda to ensure that anaconda linked correctly.
Then I installed onnxruntime:
conda install onnxruntime -c conda-forge
And after this I edited requirements.tx file:
numpy==1.21.3
–pre torch torch-vision torchaudio –extra-index-url https://download.pytorch.org/whl/nightly/cpu
albumentations==0.4.6
diffusers
opencv-python==4.6.0.66
pudb==2019.2
invisible-watermark>=0.1.0
imageio==2.9.0
imageio-ffmpeg==0.4.2
pytorch-lightning>=1.4.2
omegaconf==2.1.1
test-tube>=0.7.5
streamlit>=0.73.1
einops==0.3.0
torch-fidelity>=0.3.0
transformers==4.19.2
torchmetrics>=0.6.0
kornia>=0.6
-e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers
-e git+https://github.com/openai/CLIP.git@main#egg=clip
And after those manipulations I successfully executed:
pip install -r requirements.txt
I have tried everything above to get this installed properly with no luck.
Collecting invisible-watermark (from -r requirements.txt (line 8))
Using cached invisible_watermark-0.1.4-py3-none-any.whl (1.6 MB)
Using cached invisible_watermark-0.1.3-py3-none-any.whl (1.6 MB)
Using cached invisible_watermark-0.1.2-py3-none-any.whl (1.6 MB)
Using cached invisible_watermark-0.1.1-py3-none-any.whl (1.2 MB)
Using cached invisible_watermark-0.1.0-py3-none-any.whl (18 kB)
ERROR: Cannot install -r requirements.txt (line 8) because these package versions have conflicting dependencies.
The conflict is caused by:
invisible-watermark 0.1.5 depends on onnxruntime
invisible-watermark 0.1.4 depends on onnxruntime
invisible-watermark 0.1.3 depends on onnxruntime
invisible-watermark 0.1.2 depends on onnxruntime
invisible-watermark 0.1.1 depends on torch==1.0.1.post2
invisible-watermark 0.1.0 depends on torch==1.0.1.post2
To fix this you could try to:
1. loosen the range of package versions you’ve specified
2. remove package versions to allow pip attempt to solve the dependency conflict
ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts
I tried installing onnxruntime using conda and brew and pip and it looks like it installed but is not being found.
sorry I can’t be of more help here. It looks like there is version conflict, you may need to google a bit to find the fix.