Building a Stable Diffusion WebUI local environment on Ubuntu 22.04
Tags: ubuntu, stablediffusion, ai, aiart
Published: 2023-05-29 09:51:30



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.

```sh
sudo apt install wget git python3 python3-venv
```

After installation, check the Python version.

```sh
python --version
Python 3.10.6
```

## 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](https://pytorch.org/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmwjgd8of3a6nu6p1uy7.png)

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](https://developer.nvidia.com/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1gcmygrjsa4630auxbg.png)

Next, by entering your environment on the [CUDA Toolkit 11.7 Update 1 Downloads](https://developer.nvidia.com/cuda-11-7-1-download-archive?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=22.04&target_type=deb_local) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/18wz1d0ftdhfr7enlgaf.png)

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

```bash
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
```

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.

```sh
/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
```

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.

```bash
# 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"
```

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

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

## Checking the PyTorch Installation Command

Visit the [PyTorch official page](https://pytorch.org/) and select the command to install.

![PyTorch_CUDA_Version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmwjgd8of3a6nu6p1uy7.png)

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.

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

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.

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

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.

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

The PyTorch installation command is also set here.

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

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

```sh
export COMMANDLINE_ARGS="--autolaunch --xformers"
```

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

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

Then run `webui.sh` in the same terminal.

```bash
bash ./webui.sh
```

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

```sh
export COMMANDLINE_ARGS="--autolaunch --xformers"
```

### 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:

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

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.