Max Rohowsky, Ph.D.

77 views

How to Run ComfyUi on Google Cloud Platform

ComfyUi lets you create custom stable diffusion pipelines to generate AI images and videos. Many people choose to run it locally, but if you have a slow GPU (like me) it sucks the fun out of the learning experience. In this post I'll guide you through setting up a Google Cloud instance with a performant GPU for running ComfyUi.

Setup Google Cloud Account

To begin, you need to create and activate your account on Google Cloud. Unfortunately, the activation requires entering your payment details - nobody likes to that. But there's no way around it because trial accounts can't use GPUs.

The good news is that your free credits don't expire when you activate an account, so you'll have a small budget to play around with. Once you've activated your account, you'll arrive at this welcome screen.

Activate Compute Engine API

The next step is to activate the Compute Engine API, which lets us create virtual machines (VMs) on Google Cloud. We also need to update the GPU quota from 0 to 1 to be able to add GPUs to our VMs.

There's one catch: in order to update the GPU quota from 0 to 1, we need to create a VM instance first. It's a bit silly, you're basically creating a dummy instance that you can delete right after you update the quota.

In the "VM Instances" tab, you can see the main screen of the Compute Engine API. This is where you can create and manage your VM instances. In the "Create an Instance" tab, you can see the same screen with the dummy instance. The configuration of the dummy instance is irrelevant, you can just create one with the default settings.

Update GPU Quota

With the dummy instance created, the GPU quota can be updated. To do this, search for "quotas and system limits" in the search bar, filter by "GPUs (all regions)" in the property field, and navigate to the "Edit Quota" button. Each of these steps is labeled in the screenshot below:

After clicking on the "Edit Quota" button, you'll need to enter the new value of 1 and add a request description. Requests are usually approved instantly. Then, in the second step, you need to add a name and submit the request.

Configure VM Instance

Now we can create a VM instance with a GPU. We already created a dummy instance earlier, you can delete that one. The configuration of the new instance is given in the tabs below.

The most important things to note:

  • This configuration uses a NVIDIA Tesla T4 GPU, which is the cheapest option but they aren't available in all regions.
  • Make sure to be generous with the storage, as the models are quite large - I've set it to 110 GB.
  • Make sure to add the network tag 'port8188' because it is necessary to set up the firewall rules.

Set Firewall Rules

We want to access the VM instance from our browser window. At the same time, we want to make sure that the VM instance is only accessible from our IP address.

To do this, select View Network Details for the instance, then click on Firewall Policies and Create Firewall Rule. Set the rules as specified in the tab "Set Firewall Rule" below and add your IP address to the source IPv4 ranges field.

Remember that if your IP address changes, you'll need to update the rule.

SSH connection via Editor

The best way to work with the VM instance is to use a secure shell (SSH) connection with an editor.

  1. Install the Remote - SSH extension from the marketplace.
  2. Generate an SSH key pair by running this command in your terminal:
     ssh-keygen -t rsa -f ~/.ssh/FOLDER_NAME -C USERNAME -b 2048
    
    This command creates two files:
    • A private key at ~/.ssh/FOLDER_NAME.
    • A public key at ~/.ssh/FOLDER_NAME.pub.
  3. Open the "Command Palette" and search for the entry "Remote-SSH: Open Configuration File".
  4. Set a configuration to the SSH config file:
    Host Remote-SSH-Example
        HostName YOUR_VM_IP_ADDRESS
        User YOUR_GCP_USERNAME
        IdentityFile ~/.ssh/FOLDER_NAME/KEY_NAME
    
  5. Add your public key to VM instance:
    • Click on the instance name in the VM instances overview.
    • Click on the "Edit" button.
    • Add the public key that was generated.

Finally, open the Command Palette again and select "Remote-SSH: Connect to Host" to connect to your VM instance.

Install ComfyUi

Follow these steps to install ComfyUI on your VM instance:

  1. Clone the installation repository and make the scripts executable:

    git clone https://github.com/maxrohowsky/comfyui-on-cloud
    chmod -v +x ./comfyui-on-cloud/src/install.sh
    chmod -v +x ./comfyui-on-cloud/src/virgin_vm.sh
    
  2. Run the initial VM setup script (only needed for fresh VMs):

    ./comfyui-on-cloud/src/virgin_vm.sh
    

    After successful execution, the GPU should be printed to the console, in my case Tesla T4.

  3. Activate the Conda environment:

    source ~/.bashrc
    
  4. Run the main installation script:

    ./comfyui-on-cloud/src/install.sh
    

When the installation completes successfully, it will output: setup complete. The file tree should look similar to this:

Run ComfyUi

To start using ComfyUi via the browser, follow these steps:

  1. Make sure the VM instance is running.
  2. Create an SSH connection.
  3. Navigate to the ComfyUi directory:
    cd ComfyUi
    
  4. Start the ComfyUi server:
    python main.py --listen
    
  5. Now you can open your browser and navigate to:
    http://<your-vm-external-ip>:8188
    

Troubleshooting

If you encounter issues, here are some common solutions:

Port Already in Use

# Check if port 8188 is in use
lsof -i :8188

# If needed, kill the process using the port
sudo kill <process_id>

Connection Issues

  • Verify your IP address is listed in the firewall rules
  • Ensure the VM instance is running
  • Check that port 8188 is open in your firewall settings