Artificial intelligence (AI) art is all the rage right now, but most AI image generators run in the cloud. Stable Diffusion is different – you can run it on your own computer and generate as many images as you want. Here’s how to install and use Stable Diffusion on Windows.
What is stable diffusion?
Stable Diffusion is an open-source machine learning model that can generate images from text, edit images based on text, or fill in details on low-resolution or low-detail images. It has been trained on billions of images and can produce results that are comparable to what you would get from DALL-E 2 and MidJourney. It is developed by Stability AI and was first publicly released on August 22, 2022.
Stable Diffusion doesn’t (yet) have a clean user interface like some AI image generators, but it has an extremely permissive license and—best of all—it’s completely free to use on your own PC (or Mac).
Don’t be intimidated by the fact that Stable Diffusion currently runs in a command line interface (CLI). Getting it up and running is pretty simple. If you can double-click the executable and enter the fields, you can run it in minutes.
What do you need to run Stable Diffusion on your PC?
Stable Diffusion won’t run on your phone or most laptops, but it will run on the average gaming PC in 2022. Here are the requirements:
How to install and run Stable Diffusion on Windows
You need two pieces of software: Git and Miniconda3.
Note: Git and Miniconda3 are secure programs created by reputable organizations. You don’t have to worry about malware with them if you download them from the official sources linked in this article.
Git is a tool that allows developers to manage different versions of the software they are developing. They can simultaneously maintain multiple versions of the software they are working on in a central repository and allow other developers to contribute to the project.
RELATED: What is GitHub and what is it used for?
If you’re not a developer, Git provides a convenient way to access and download these projects, so we’ll use that in this case. Download the Windows x64 installer from Git and then run it.
When you start the installer, you will be prompted to select several options – leave them at their default settings. One option page, “Modify Environment PATH” is particularly important. It must be set to “Git from the command line and also from third-party software”.
Stable Diffusion draws from several different Python libraries. If you don’t know much about Python, don’t worry—suffice it to say that libraries are just software packages that your computer can use to perform specific functions, such as image transformation or complex calculations.
RELATED: What is Python?
Miniconda3 is basically a convenient tool. It allows you to download, install and manage all the libraries needed for Stable Diffusion to work without much manual intervention. It will also be how we actually use Stable Diffusion.
Go to the Miniconda3 download page and click on “Miniconda3 Windows 64-bit” to get the latest installer.
After downloading the executable file, double-click to start the installation. Installing Miniconda3 requires less page-clicking than Git, but you have to be careful with this option:
Make sure you select “All Users” before clicking Next to complete the installation.
After installing Git and Miniconda3, you will be prompted to restart your computer. We haven’t found this to be necessary, but it won’t hurt if you do.
Download the Stable Diffusion GitHub repository and the latest checkpoint
Now that we have installed the necessary software, we are ready to download and install Stable Diffusion.
Download the latest checkpoint first – version 1.4 is almost 5GB, so it might take a while. You need to create an account to download a checkpoint, but they only require a name and email address. Everything else is optional.
Note: At the time of writing (September 2, 2022), the latest checkpoint is version 1.4. If there is a newer version, download it.
Click on “sd-v1-4.ckpt” to start the download.
Note: The second file, “sd-v1-4-full-ema.ckpt”, could they give better results, but it is about twice as large. You can use both.
Then you need to download Stable Diffusion from GitHub. Click the green “Code” button and then click “Download ZIP”. Alternatively, you can use this direct download link.
Now we need to prepare several folders where we will extract all the Stable Diffusion files. Click the Start button and type “miniconda3” in the Start menu search box, then click “Open” or press Enter.
Using the command line, we will create a folder called “stable-diffusion”. Copy and paste the code block below into the Miniconda3 window and press Enter.
cd C:/ mkdir stable-diffusion cd stable-diffusion
Note: Almost any time you put a block of code into a terminal like Miniconda3, you have to press Enter at the end to run the last command.
If everything went well, you’ll see something like this:
Leave the Miniconda3 window open, we’ll need it again shortly.
Open the “stable-diffusion-main.zip” zip file you downloaded from GitHub in your favorite file archiver. Alternatively, Windows can also open ZIP files itself if you don’t have one. Leave the ZIP file open in one window, then open another File Explorer window and navigate to the “C:\stable-diffusion” folder we just created.
RELATED: Get help with File Explorer in Windows 10
Drag the folder in the “stable-diffusion-main” ZIP file into the “stable-diffusion” folder.
Switch back to Miniconda3, then copy and paste the following commands into the window:
cd C:\stable-diffusion\stable-diffusion-main conda env create -f environment.yaml conda activate ldm mkdir models\ldm\stable-diffusion-v1
Do not interrupt this process. Some files are larger than a gigabyte, so it may take a while to download. If you accidentally interrupt the process, you will have to delete the environment folder and start
conda env create -f environment.yaml again. If this happens, go to “C:\Users\(Your user account)\.conda\envs” and delete the “ldm” folder, then run the previous command.
Note: So, what did we just do? Python allows you to sort coding projects into “Environments”. Each environment is separate from the other environments, so you can load different Python libraries into different environments without worrying about conflicting versions. It’s invaluable if you’re working on multiple projects on one PC.
The lines we ran created a new environment called “ldm”, downloaded and installed all the necessary Python libraries for Stable Diffusion to work, activated the ldm environment, and then changed directory to the new folder.
We are at the last step of the installation. In File Explorer, navigate to “C:\stable-diffusion\stable-diffusion-main\models\ldm\stable-diffusion-v1”, then copy and paste the checkpoint file (sd-v1-4.ckpt) into the folder.
Wait for the file transfer to finish, right-click “sd-v1-4.ckpt” and then click “Rename”. Type “model.ckpt” in the highlighted field, then press Enter to change the file name.
Note: If you’re using Windows 11, you won’t see “rename” in the right-click context menu. Instead, there’s an icon that looks like a miniature text box.
RELATED: The tiny context menu buttons in Windows 11 will confuse people
And that’s it – we’re done. Now we are ready to actually use Stable Diffusion.
How to use stable diffusion
The ldm environment we created is necessary and you must activate it whenever you want to use Stable Diffusion. Enter
conda activate ldm into the Miniconda3 window and press “Enter”. (ldm) on the left indicates that the ldm environment is active.
Note: You only need to enter this command when you open Miniconda3. The ldm environment remains active until you close the window.
Then we need to change the directory (so the command
cd) to “C:\stable-diffusion\stable-diffusion-main” before we can generate images. Insert
cd C:\stable-diffusion\stable-diffusion-main to the command line.
How to create an image with stable diffusion
We will call a script, txt2img.py, that allows us to convert text prompts to 512×512 images. Here is an example. Try this to make sure everything is working properly:
python scripts/txt2img.py --prompt "a close-up portrait of a cat by pablo picasso, vivid, abstract art, colorful, vibrant" --plms --n_iter 5 --n_samples 1
Your console will give you a progress bar as you create images.
This command creates five cat images, all located in “C:\stable-diffusion\stable-diffusion-main\outputs\txt2img-samples\samples”.
It’s not perfect, but it closely resembles Pablo Picasso’s style, as we specified in the challenge. Your images should look similar, but not necessarily identical.
Whenever you want to change the generated image, just change the text enclosed in the following quotes
Tip: Don’t rewrite the whole line every time. Use the arrow keys to move the text cursor and just replace the prompt.
python scripts/txt2img.py --prompt "YOUR, DESCRIPTIONS, GO, HERE" --plms --n_iter 5 --n_samples 1
Let’s say we wanted to create a realistic looking gopher in an enchanted forest wearing a wizard hat. We can try the command:
python scripts/txt2img.py --prompt "a photograph of a gopher wearing a wizard hat in a forest, vivid, photorealistic, magical, fantasy, 8K UHD, photography" --plms --n_iter 5 --n_samples 1
It really is that easy – just describe what you want as specifically as possible. If you want something photorealistic, be sure to include terms related to a realistic image. If you want something inspired by a particular artist’s style, please include the artist.
Stable Diffusion is not limited to portraits and animals, it can also create remarkable landscapes.
What do the arguments in the command mean?
Stable Diffusion has a huge number of settings and arguments you can provide to customize your results. The few listed here are basically necessary to get Stable Diffusion running on the average gaming PC.
- –plms — Specifies how images will be sampled. There is a paper about it if you want to look at the math.
- –n_iter — specifies the number of iterations to generate for each prompt. 5 is a decent number to see what results you get.
- –n_samples — specifies the number of samples to be generated. The default value is 3, but most computers do not have enough VRAM to support this. Leave it at 1 unless you have a specific reason to change it.
Of course, Stable Diffusion has a lot of different arguments that you can implement to improve your results. Run
python scripts/txt2img.py --help to get an exhaustive list of arguments you can use.
It takes a lot of trial and error to get great results, but that’s at least half the fun. Be sure to write down or save arguments and descriptions that return results you like. If you don’t want to do all the experiments yourself, there are growing communities on Reddit (and elsewhere) dedicated to sharing the images and the challenges that generated them.