DEV Community

Cover image for Building a Stable Diffusion WebUI local environment on Ubuntu 22.04
gladevise
gladevise

Posted on

Building a Stable Diffusion WebUI local environment on Ubuntu 22.04

This article guides you on how to set up a Stable Diffusion environment on Ubuntu 22.04.

Installation of Python, wget, git

First, install the necessary applications such as python, wget, and git.



sudo apt install wget git python3 python3-venv


Enter fullscreen mode Exit fullscreen mode

After installation, check the Python version.



python --version
Python 3.10.6


Enter fullscreen mode Exit fullscreen mode

CUDA Installation

Next, install CUDA.

Confirming the version of CUDA compatible with PyTorch

Before installing CUDA, check the version of CUDA that is compatible with PyTorch. By accessing the PyTorch official site and setting the PyTorch Build to Stable, the OS to Linux, the Package to Pip, and the Language to Python, you can find out which version of CUDA is available.

PyTorch_CUDA_Version

In this case, it was confirmed that CUDA 11.7 or 11.8 could be used. Since this article might be outdated, I recommend you to confirm the available CUDA versions using this method in advance.

Installing CUDA

To download an older version of CUDA, first access the cuda-toolkit-archive.

Since we are downloading version 11.7 this time, choose either 11.7.0 or 11.7.1. As corrections at the patch level might be included, it is recommended to choose 11.7.1 unless you have a specific reason.

CUDA_Archive

Next, by entering your environment on the CUDA Toolkit 11.7 Update 1 Downloads site, the necessary installation commands will be generated. This time we are using an Ubuntu 22.04 environment with an Intel CPU, so make the selections as follows. Please choose according to your PC environment.

CUDA_Select_Target_Platform

As a result of the selection, the following commands are displayed.



wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.7.1/local_installers/cuda-repo-ubuntu2204-11-7-local_11.7.1-515.65.01-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2204-11-7-local_11.7.1-515.65.01-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2204-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt update
sudo apt install cuda-11-7


Enter fullscreen mode Exit fullscreen mode

Before starting the installation process, there is one thing to note. The last apt install cuda command needs to include a version, like sudo apt install cuda-11-7. Otherwise, the latest version of CUDA will be installed. PyTorch may not be compatible with the latest version of CUDA. Therefore, always specify the version when installing.

After installation is complete, you can check the version of CUDA by running the following command.



/usr/local/cuda-11.7/bin/nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Jun__8_16:49:14_PDT_2022
Cuda compilation tools, release 11.7, V11.7.99
Build cuda_11.7.r11.7/compiler.31442593_0


Enter fullscreen mode Exit fullscreen mode

If the path is not set correctly, you may run into issues later. Therefore, I recommend adding the following description to your ~/.bashrc file to set the path to CUDA.



# Setting CUDA PATH
export PATH="/usr/local/cuda-11.7/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.7/lib64:$LD_LIBRARY_PATH"


Enter fullscreen mode Exit fullscreen mode

After saving, open a new terminal and execute which nvcc to confirm that the path is set correctly.



❯ which nvcc
/usr/local/cuda-11.7/bin/nvcc


Enter fullscreen mode Exit fullscreen mode

Checking the PyTorch Installation Command

Visit the PyTorch official page and select the command to install.

PyTorch_CUDA_Version

Currently, as the latest version of CUDA that PyTorch supports is 11.7, you should use pip3 install torch torchvision torchaudio. However, it is recommended to note down the command specifying the version of CUDA for future reference when versions like 11.8 or 12.0 are added.



pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117


Enter fullscreen mode Exit fullscreen mode

This command is to be noted in the place where you set the command to install PyTorch in the Stable Diffusion WebUI.

Installing Stable Diffusion WebUI

First, clone the repository.



git clone --depth=1 --branch v1.2.1 https://github.com/AUTOMATIC1111/stable-diffusion-webui


Enter fullscreen mode Exit fullscreen mode

The webui-user.sh file is used for detailed configuration.

For instance, the Stable Diffusion WebUI is designed to be cloned under ~/ by default. But if you've cloned it somewhere else, you need to change install_dir as follows.



install_dir="/home/$(whoami)/Apps"


Enter fullscreen mode Exit fullscreen mode

The PyTorch installation command is also set here.



export TORCH_COMMAND="pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117"


Enter fullscreen mode Exit fullscreen mode

xformers, which slightly speeds up image generation, can also be enabled by adding COMMANDLINE_ARGS as follows.



export COMMANDLINE_ARGS="--autolaunch --xformers"


Enter fullscreen mode Exit fullscreen mode

The script is granted execution rights and run with the following command.



chmod 755 webui-user.sh
bash ./webui-user.sh


Enter fullscreen mode Exit fullscreen mode

Then run webui.sh in the same terminal.



bash ./webui.sh


Enter fullscreen mode Exit fullscreen mode

On the first run, it may take 1-3 hours as it will need to download the model and dependencies.

If the installation is successful, the WebUI will be displayed at http://127.0.0.1:7860/.

Troubleshooting

Here is a list of common errors and their solutions.

The program 'nvcc' is currently not installed. You can install it by typing: sudo apt install nvidia-cuda-toolkit

This error occurs when CUDA is not installed, or when the path is not set correctly. Please make sure that an executable file exists at /usr/local/cuda-11.7/bin/nvcc.

No module 'xformers'. Proceeding without it.

This error occurs when xformers is not installed. Add --xformers to COMMANDLINE_ARGS in webui-user.sh.



export COMMANDLINE_ARGS="--autolaunch --xformers"


Enter fullscreen mode Exit fullscreen mode

RuntimeError: Couldn't install torch.

This error occurs when the installation of PyTorch fails for some reason.

In my case, the cause of this error was that the version of CUDA supported by the PyTorch I was trying to install did not match the version of CUDA installed on my system.

By default, the Stable Diffusion WebUI tries to install PyTorch with the following command:



pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113


Enter fullscreen mode Exit fullscreen mode

Here, it is trying to install PyTorch that is compatible with CUDA 11.3. However, as a result of running sudo apt install cuda when I was installing CUDA, CUDA 12.1 was installed instead.

As explained at the beginning of the article, I was able to successfully install by matching the versions of PyTorch and CUDA.

However, this solution may not work in all environments. I hope that those who encounter a similar error will find this information helpful in resolving the issue.

Top comments (0)