Skip to content

Instantly share code, notes, and snippets.

@doggeddalle
Last active July 23, 2025 19:42
Show Gist options
  • Select an option

  • Save doggeddalle/0bf1b600b76f4a17f42031970f245f87 to your computer and use it in GitHub Desktop.

Select an option

Save doggeddalle/0bf1b600b76f4a17f42031970f245f87 to your computer and use it in GitHub Desktop.

Revisions

  1. doggeddalle renamed this gist Jul 23, 2025. 1 changed file with 100 additions and 44 deletions.
    144 changes: 100 additions & 44 deletions ComfyUI -- Arch -- Nvidia → ComfyUI -- Arch -- Nvidia.sh
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,31 @@
    #!/bin/bash

    echo "--- ComfyUI Installation Script ---"
    echo "Targeting PyTorch with CUDA 12.8, Python 3.12."

    # --- Configuration ---
    COMFYUI_DIR=$(pwd)
    VENV_DIR="${COMFYUI_DIR}/venv"
    PYTHON_VERSION="3.12" # The desired Python version
    COMFYUI_REPO="https://github.com/comfyanonymous/ComfyUI.git"
    XFORMERS_REPO="https://github.com/facebookresearch/xformers.git"
    COMFYUI_MANAGER_REPO="https://github.com/Comfy-Org/ComfyUI-Manager.git"

    # Specific PyTorch index URL for stable CUDA
    # Specific PyTorch index URL for stable CUDA 12.8
    PYTORCH_CUDA_INDEX_URL="https://download.pytorch.org/whl/cu128"

    # --- Helper Functions ---

    # Function to check if a Python module is importable within the activated venv
    check_module_import() {
    local module_name="$1"
    # Ensure venv is activated before checking
    if [ -z "${VIRTUAL_ENV}" ]; then
    echo "Error: Virtual environment not activated. Cannot check module import for ${module_name}."
    return 1
    fi
    python -c "import ${module_name}" &> /dev/null
    # Use the venv's python executable
    "${VENV_DIR}/bin/python" -c "import ${module_name}" &> /dev/null
    return $?
    }

    @@ -30,7 +35,8 @@ check_pytorch_cuda() {
    echo "Error: Virtual environment not activated. Cannot check PyTorch CUDA status."
    return 1
    fi
    python -c "
    # Use the venv's python executable
    "${VENV_DIR}/bin/python" -c "
    import torch
    if torch.cuda.is_available():
    print('PyTorch with CUDA is available.')
    @@ -43,47 +49,57 @@ else:
    # --- Prerequisites Check ---
    echo ""
    echo "Checking for essential build tools..."

    if ! command -v git &> /dev/null; then
    echo "Error: git is not installed. Please install it, for example: 'sudo pacman -S git'."
    echo "Error: git is not installed. Please install it with 'sudo pacman -S git'."
    exit 1
    fi

    if ! command -v cmake &> /dev/null; then
    echo "Error: cmake is not installed. Please install it with 'sudo pacman -S cmake'."
    exit 1
    fi

    if ! command -v python &> /dev/null; then
    echo "The 'python' command is not installed. Attempting to install it via pacman..."
    echo "This is exclusive to Arch Linux. You may be prompted for your password."
    sudo pacman -S --noconfirm python
    # Check and install tkinter dependencies for Python
    if ! pacman -Qs tcl &> /dev/null || ! pacman -Qs tk &> /dev/null; then
    echo "Tkinter dependencies (tcl and tk) not found. Installing now..."
    sudo pacman -S --noconfirm tcl tk
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install Python with pacman."
    echo "Please install it manually and re-run the script. Example: 'sudo pacman -S python'"
    exit 1
    echo "Warning: Failed to install tcl and tk. Python installation might not include tkinter."
    fi
    echo "Python installed successfully."
    fi

    if ! command -v cmake &> /dev/null; then
    echo "cmake is not installed. Attempting to install it via pacman..."
    echo "This is exclusive to Arch Linux. You may be prompted for your password."
    sudo pacman -S --noconfirm cmake

    # --- pyenv and Python 3.12 Installation ---
    echo ""
    echo "Setting up Python 3.12 using pyenv..."
    if ! command -v pyenv &> /dev/null; then
    echo "pyenv not found. Installing pyenv with pacman."
    sudo pacman -S pyenv --noconfirm
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install cmake with pacman."
    echo "Please install it manually and re-run the script. Example: 'sudo pacman -S cmake'"
    echo "Error: Failed to install pyenv. Please install it manually with 'sudo pacman -S pyenv'."
    exit 1
    fi
    echo "cmake installed successfully."
    fi

    if ! pkg-config --exists sentencepiece &> /dev/null; then
    echo "The 'sentencepiece' system library is not installed. Attempting to install it via pacman..."
    echo "This is exclusive to Arch Linux. You may be prompted for your password."
    sudo pacman -S --noconfirm sentencepiece
    # Make sure pyenv is initialized for the current shell session
    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    if command -v pyenv &> /dev/null; then
    eval "$(pyenv init --path)"
    eval "$(pyenv init -)"
    fi

    if ! pyenv versions --bare | grep -q "${PYTHON_VERSION}"; then
    echo "Python ${PYTHON_VERSION} not installed via pyenv. Installing now..."
    pyenv install "${PYTHON_VERSION}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install sentencepiece with pacman."
    echo "Please install it manually and re-run the script. Example: 'sudo pacman -S sentencepiece'"
    echo "Error: Failed to install Python ${PYTHON_VERSION} with pyenv."
    echo "Check the pyenv log for details or try installing dependencies."
    exit 1
    fi
    echo "sentencepiece installed successfully."
    fi

    echo "Python ${PYTHON_VERSION} is available."

    # --- ComfyUI Cloning ---
    echo ""
    @@ -102,10 +118,21 @@ fi
    # --- Virtual Environment Setup ---
    echo ""
    echo "Setting up Python virtual environment at ${VENV_DIR}..."

    # Set the local pyenv version to ensure 'pyenv which' works correctly
    pyenv local "${PYTHON_VERSION}"
    PYENV_PYTHON_BIN=$(pyenv which python)
    if [ -z "${PYENV_PYTHON_BIN}" ]; then
    echo "Error: Could not find Python ${PYTHON_VERSION} binary via pyenv."
    echo "Please ensure pyenv is configured correctly and Python ${PYTHON_VERSION} is installed."
    exit 1
    fi

    if [ ! -d "${VENV_DIR}" ]; then
    python -m venv "${VENV_DIR}"
    echo "Creating virtual environment with ${PYENV_PYTHON_BIN}..."
    "${PYENV_PYTHON_BIN}" -m venv "${VENV_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to create virtual environment."
    echo "Error: Failed to create virtual environment with pyenv's Python ${PYTHON_VERSION}."
    exit 1
    fi
    echo "Virtual environment created."
    @@ -123,19 +150,19 @@ echo "Virtual environment activated."

    # --- Python Package Installation ---
    echo ""
    echo "Installing/Updating core PyTorch, torchvision, and torchaudio with CUDA support..."
    echo "Installing/Updating core PyTorch, torchvision, and torchaudio with CUDA 12.8 support..."
    if check_pytorch_cuda; then
    echo "PyTorch with CUDA is already installed. Skipping."
    else
    echo "Attempting to uninstall existing PyTorch installations before clean install..."
    pip uninstall -y torch torchvision torchaudio 2>/dev/null
    pip install torch torchvision torchaudio --extra-index-url "${PYTORCH_CUDA_INDEX_URL}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install PyTorch with CUDA support. Check your CUDA setup and network connection."
    echo "Error: Failed to install PyTorch with CUDA 12.8 support. Check your CUDA setup and network connection."
    deactivate
    exit 1
    fi
    echo "PyTorch, torchvision, and torchaudio installed."
    echo "PyTorch, torchvision, and torchaudio installed for CUDA 12.8."
    fi

    echo ""
    @@ -148,6 +175,43 @@ if [ $? -ne 0 ]; then
    fi
    echo "ComfyUI requirements installed."

    # --- xFormers Installation (from source) ---
    echo ""
    echo "xFormers setup..."
    XFORMERS_DIR="${COMFYUI_DIR}/xformers"
    if [ ! -d "${XFORMERS_DIR}" ]; then
    git clone "${XFORMERS_REPO}" "${XFORMERS_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to clone xFormers repository."
    deactivate
    exit 1
    fi
    echo "Initializing and updating xFormers submodules..."
    (cd "${XFORMERS_DIR}" && git submodule update --init --recursive)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to update xFormers submodules. This is required for compilation."
    deactivate
    exit 1
    fi
    else
    echo "xFormers directory already exists. Pulling latest changes and updating submodules."
    (cd "${XFORMERS_DIR}" && git pull && git submodule update --init --recursive)
    fi

    echo "Installing xFormers from source (this may take a while, no build isolation)..."
    if check_module_import "xformers"; then
    echo "xFormers is already installed. Skipping build."
    else
    (cd "${XFORMERS_DIR}" && pip install -e . --no-build-isolation)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to build and install xFormers. Check build logs for details."
    echo "Ensure your CUDA Toolkit is properly installed and accessible."
    deactivate
    exit 1
    fi
    echo "xFormers installed."
    fi

    # --- ComfyUI-Manager Installation ---
    echo ""
    echo "ComfyUI-Manager setup..."
    @@ -182,6 +246,10 @@ else
    fi
    fi

    # --- Final Message ---
    echo ""
    echo "--- ComfyUI installation process completed"

    # --- Create Shortcuts ---
    echo ""
    echo "Creating desktop launchers for quick access..."
    @@ -214,17 +282,5 @@ Icon=comfyui-terminal
    EOL
    chmod +x "${VENV_DESKTOP_FILE_PATH}"


    # --- Final Message ---
    echo ""
    echo "--- ComfyUI installation process completed ---"
    echo "You will now find the following files in this directory:"
    echo " - ComfyUI.desktop"
    echo " - ComfyUI_Terminal.desktop"
    echo ""
    echo "To get started, you can double-click 'ComfyUI.desktop'."
    echo "You may need to grant it permission to run the first time."
    echo "If you want to install more nodes, double-click 'ComfyUI_Terminal.desktop'."

    # Deactivate the environment at the end of the script to not leave it active globally
    deactivate
  2. doggeddalle renamed this gist Jul 22, 2025. 1 changed file with 79 additions and 111 deletions.
    190 changes: 79 additions & 111 deletions install.sh → ComfyUI -- Arch -- Nvidia
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,21 @@
    #!/bin/bash

    echo "--- ComfyUI Installation Script ---"
    echo "Targeting NVIDIA RTX 3090, PyTorch with CUDA 12.8, Python 3.12, GCC12."

    # --- Configuration ---
    COMFYUI_DIR=$(pwd)
    VENV_DIR="${COMFYUI_DIR}/venv"
    PYTHON_VERSION="3.12"
    COMFYUI_REPO="https://github.com/comfyanonymous/ComfyUI.git"
    SAGEATTENTION_REPO="https://github.com/thu-ml/SageAttention.git"
    XFORMERS_REPO="https://github.com/facebookresearch/xformers.git" # Official xformers repo
    COMFYUI_MANAGER_REPO="https://github.com/Comfy-Org/ComfyUI-Manager.git"

    # Specific PyTorch index URL for stable CUDA 12.8
    # Specific PyTorch index URL for stable CUDA
    PYTORCH_CUDA_INDEX_URL="https://download.pytorch.org/whl/cu128"

    # --- Helper Functions ---

    # Function to check if a Python module is importable within the activated venv
    check_module_import() {
    local module_name="$1"
    # Ensure venv is activated before checking
    if [ -z "${VIRTUAL_ENV}" ]; then
    echo "Error: Virtual environment not activated. Cannot check module import for ${module_name}."
    return 1
    @@ -49,33 +44,47 @@ else:
    echo ""
    echo "Checking for essential build tools..."
    if ! command -v git &> /dev/null; then
    echo "Error: git is not installed. Please install it with 'sudo pacman -S git'."
    echo "Error: git is not installed. Please install it, for example: 'sudo pacman -S git'."
    exit 1
    fi

    if ! command -v python${PYTHON_VERSION} &> /dev/null; then
    echo "Error: Python ${PYTHON_VERSION} is not installed. Please install it, e.g., 'sudo pacman -S python'."
    exit 1
    if ! command -v python &> /dev/null; then
    echo "The 'python' command is not installed. Attempting to install it via pacman..."
    echo "This is exclusive to Arch Linux. You may be prompted for your password."
    sudo pacman -S --noconfirm python
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install Python with pacman."
    echo "Please install it manually and re-run the script. Example: 'sudo pacman -S python'"
    exit 1
    fi
    echo "Python installed successfully."
    fi

    if ! command -v cmake &> /dev/null; then
    echo "Error: cmake is not installed. Please install it with 'sudo pacman -S cmake'."
    exit 1
    echo "cmake is not installed. Attempting to install it via pacman..."
    echo "This is exclusive to Arch Linux. You may be prompted for your password."
    sudo pacman -S --noconfirm cmake
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install cmake with pacman."
    echo "Please install it manually and re-run the script. Example: 'sudo pacman -S cmake'"
    exit 1
    fi
    echo "cmake installed successfully."
    fi

    # --- Explicitly setting GCC 12 for compilation ---
    if command -v gcc-12 &> /dev/null && command -v g++-12 &> /dev/null; then
    echo "Found gcc-12 and g++-12. Setting CC and CXX environment variables for builds."
    export CC="gcc-12"
    export CXX="g++-12"
    echo "CC is set to: ${CC}"
    echo "CXX is set to: ${CXX}"
    else
    echo "Error: Could not find gcc-12 or g++-12 in PATH. Please ensure they are installed and accessible."
    echo "You might need to adjust the script to point to the exact path if they are not in standard locations."
    exit 1
    if ! pkg-config --exists sentencepiece &> /dev/null; then
    echo "The 'sentencepiece' system library is not installed. Attempting to install it via pacman..."
    echo "This is exclusive to Arch Linux. You may be prompted for your password."
    sudo pacman -S --noconfirm sentencepiece
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install sentencepiece with pacman."
    echo "Please install it manually and re-run the script. Example: 'sudo pacman -S sentencepiece'"
    exit 1
    fi
    echo "sentencepiece installed successfully."
    fi


    # --- ComfyUI Cloning ---
    echo ""
    echo "ComfyUI repository setup..."
    @@ -94,7 +103,7 @@ fi
    echo ""
    echo "Setting up Python virtual environment at ${VENV_DIR}..."
    if [ ! -d "${VENV_DIR}" ]; then
    python${PYTHON_VERSION} -m venv "${VENV_DIR}"
    python -m venv "${VENV_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to create virtual environment."
    exit 1
    @@ -114,19 +123,19 @@ echo "Virtual environment activated."

    # --- Python Package Installation ---
    echo ""
    echo "Installing/Updating core PyTorch, torchvision, and torchaudio with CUDA 12.8 support..."
    echo "Installing/Updating core PyTorch, torchvision, and torchaudio with CUDA support..."
    if check_pytorch_cuda; then
    echo "PyTorch with CUDA is already installed. Skipping."
    else
    echo "Attempting to uninstall existing PyTorch installations before clean install..."
    pip uninstall -y torch torchvision torchaudio 2>/dev/null
    pip install torch torchvision torchaudio --extra-index-url "${PYTORCH_CUDA_INDEX_URL}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install PyTorch with CUDA 12.8 support. Check your CUDA setup and network connection."
    echo "Error: Failed to install PyTorch with CUDA support. Check your CUDA setup and network connection."
    deactivate
    exit 1
    fi
    echo "PyTorch, torchvision, and torchaudio installed for CUDA 12.8."
    echo "PyTorch, torchvision, and torchaudio installed."
    fi

    echo ""
    @@ -139,93 +148,12 @@ if [ $? -ne 0 ]; then
    fi
    echo "ComfyUI requirements installed."

    # --- Triton Installation (Latest) ---
    echo ""
    echo "Installing latest Triton..."
    if check_module_import "triton"; then
    echo "Triton is already installed. Skipping."
    else
    pip install --upgrade triton
    if [ $? -ne 0 ]; then
    echo "Warning: Failed to install Triton directly. This might be due to a missing pre-built wheel."
    echo "Pip will attempt to build from source if possible. Ensure 'cmake' and build-essential packages are present."
    fi
    echo "Triton installation attempted."
    fi

    # --- SageAttention Installation (from source) ---
    echo ""
    echo "SageAttention 2++ setup..."
    SAGEATTENTION_DIR="${COMFYUI_DIR}/SageAttention"
    if [ ! -d "${SAGEATTENTION_DIR}" ]; then
    git clone "${SAGEATTENTION_REPO}" "${SAGEATTENTION_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to clone SageAttention repository."
    deactivate
    exit 1
    fi
    else
    echo "SageAttention directory already exists. Pulling latest changes."
    (cd "${SAGEATTENTION_DIR}" && git pull)
    fi

    echo "Installing SageAttention from source (no build isolation)..."
    if check_module_import "sageattention"; then
    echo "SageAttention is already installed. Skipping build."
    else
    (cd "${SAGEATTENTION_DIR}" && pip install -e . --no-build-isolation)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to build and install SageAttention. Check build logs for details."
    echo "Ensure your CUDA Toolkit is properly installed and accessible, and that GCC 12 is correctly configured via CC/CXX."
    deactivate
    exit 1
    fi
    echo "SageAttention 2++ installed."
    fi

    # --- xFormers Installation (from source) ---
    echo ""
    echo "xFormers setup..."
    XFORMERS_DIR="${COMFYUI_DIR}/xformers"
    if [ ! -d "${XFORMERS_DIR}" ]; then
    git clone "${XFORMERS_REPO}" "${XFORMERS_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to clone xFormers repository."
    deactivate
    exit 1
    fi
    echo "Initializing and updating xFormers submodules..."
    (cd "${XFORMERS_DIR}" && git submodule update --init --recursive)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to update xFormers submodules. This is required for compilation."
    deactivate
    exit 1
    fi
    else
    echo "xFormers directory already exists. Pulling latest changes and updating submodules."
    (cd "${XFORMERS_DIR}" && git pull && git submodule update --init --recursive)
    fi

    echo "Installing xFormers from source (this may take a while, no build isolation)..."
    if check_module_import "xformers"; then
    echo "xFormers is already installed. Skipping build."
    else
    (cd "${XFORMERS_DIR}" && pip install -e . --no-build-isolation)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to build and install xFormers. Check build logs for details."
    echo "Ensure your CUDA Toolkit is properly installed and accessible, and that GCC 12 is correctly configured via CC/CXX."
    deactivate
    exit 1
    fi
    echo "xFormers installed."
    fi

    # --- ComfyUI-Manager Installation ---
    echo ""
    echo "ComfyUI-Manager setup..."
    COMFYUI_MANAGER_DIR="${COMFYUI_DIR}/ComfyUI/custom_nodes/ComfyUI-Manager"

    mkdir -p "${COMFYUI_DIR}/ComfyUI/custom_nodes" # Ensure custom_nodes directory exists
    mkdir -p "${COMFYUI_DIR}/ComfyUI/custom_nodes"

    if [ ! -d "${COMFYUI_MANAGER_DIR}" ]; then
    echo "Cloning ComfyUI-Manager repository..."
    @@ -254,9 +182,49 @@ else
    fi
    fi

    # --- Create Shortcuts ---
    echo ""
    echo "Creating desktop launchers for quick access..."

    # 1. Create the desktop launcher for running ComfyUI
    DESKTOP_FILE_PATH="${COMFYUI_DIR}/ComfyUI.desktop"
    echo "Creating ComfyUI.desktop launcher..."
    cat > "${DESKTOP_FILE_PATH}" << EOL
    [Desktop Entry]
    Name=ComfyUI
    Comment=Launch ComfyUI in a new terminal
    Exec=zsh -c "cd ${COMFYUI_DIR}/ComfyUI && source ${VENV_DIR}/bin/activate && python main.py; read -p 'Press Enter to close...'"
    Terminal=true
    Type=Application
    Icon=comfyui
    EOL
    chmod +x "${DESKTOP_FILE_PATH}"

    # 2. Create the desktop launcher for the virtual environment terminal
    VENV_DESKTOP_FILE_PATH="${COMFYUI_DIR}/ComfyUI_Terminal.desktop"
    echo "Creating ComfyUI_Terminal.desktop launcher..."
    cat > "${VENV_DESKTOP_FILE_PATH}" << EOL
    [Desktop Entry]
    Name=ComfyUI Terminal
    Comment=Open a terminal with the ComfyUI virtual environment active
    Exec=zsh -c "cd ${COMFYUI_DIR}/ComfyUI && source ${VENV_DIR}/bin/activate && exec zsh"
    Terminal=true
    Type=Application
    Icon=comfyui-terminal
    EOL
    chmod +x "${VENV_DESKTOP_FILE_PATH}"


    # --- Final Message ---
    echo ""
    echo "--- ComfyUI installation process completed"
    echo "--- ComfyUI installation process completed ---"
    echo "You will now find the following files in this directory:"
    echo " - ComfyUI.desktop"
    echo " - ComfyUI_Terminal.desktop"
    echo ""
    echo "To get started, you can double-click 'ComfyUI.desktop'."
    echo "You may need to grant it permission to run the first time."
    echo "If you want to install more nodes, double-click 'ComfyUI_Terminal.desktop'."

    # Deactivate the environment at the end of the script to not leave it active globally
    deactivate
    deactivate
  3. doggeddalle revised this gist Jul 17, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion install.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/bash

    echo "--- ComfyUI Installation Script for alex ---"
    echo "--- ComfyUI Installation Script ---"
    echo "Targeting NVIDIA RTX 3090, PyTorch with CUDA 12.8, Python 3.12, GCC12."

    # --- Configuration ---
  4. doggeddalle revised this gist Jul 17, 2025. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -256,8 +256,7 @@ fi

    # --- Final Message ---
    echo ""
    echo "--- ComfyUI installation process completed for alex ---"
    echo "You can now launch ComfyUI using your desktop launchers."
    echo "--- ComfyUI installation process completed"

    # Deactivate the environment at the end of the script to not leave it active globally
    deactivate
  5. doggeddalle renamed this gist Jul 17, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. doggeddalle created this gist Jul 17, 2025.
    263 changes: 263 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,263 @@
    #!/bin/bash

    echo "--- ComfyUI Installation Script for alex ---"
    echo "Targeting NVIDIA RTX 3090, PyTorch with CUDA 12.8, Python 3.12, GCC12."

    # --- Configuration ---
    COMFYUI_DIR=$(pwd)
    VENV_DIR="${COMFYUI_DIR}/venv"
    PYTHON_VERSION="3.12"
    COMFYUI_REPO="https://github.com/comfyanonymous/ComfyUI.git"
    SAGEATTENTION_REPO="https://github.com/thu-ml/SageAttention.git"
    XFORMERS_REPO="https://github.com/facebookresearch/xformers.git" # Official xformers repo
    COMFYUI_MANAGER_REPO="https://github.com/Comfy-Org/ComfyUI-Manager.git"

    # Specific PyTorch index URL for stable CUDA 12.8
    PYTORCH_CUDA_INDEX_URL="https://download.pytorch.org/whl/cu128"

    # --- Helper Functions ---

    # Function to check if a Python module is importable within the activated venv
    check_module_import() {
    local module_name="$1"
    # Ensure venv is activated before checking
    if [ -z "${VIRTUAL_ENV}" ]; then
    echo "Error: Virtual environment not activated. Cannot check module import for ${module_name}."
    return 1
    fi
    python -c "import ${module_name}" &> /dev/null
    return $?
    }

    # Function to check if PyTorch is installed and CUDA enabled
    check_pytorch_cuda() {
    if [ -z "${VIRTUAL_ENV}" ]; then
    echo "Error: Virtual environment not activated. Cannot check PyTorch CUDA status."
    return 1
    fi
    python -c "
    import torch
    if torch.cuda.is_available():
    print('PyTorch with CUDA is available.')
    else:
    raise RuntimeError('PyTorch CUDA is not available.')
    " &> /dev/null
    return $?
    }

    # --- Prerequisites Check ---
    echo ""
    echo "Checking for essential build tools..."
    if ! command -v git &> /dev/null; then
    echo "Error: git is not installed. Please install it with 'sudo pacman -S git'."
    exit 1
    fi

    if ! command -v python${PYTHON_VERSION} &> /dev/null; then
    echo "Error: Python ${PYTHON_VERSION} is not installed. Please install it, e.g., 'sudo pacman -S python'."
    exit 1
    fi

    if ! command -v cmake &> /dev/null; then
    echo "Error: cmake is not installed. Please install it with 'sudo pacman -S cmake'."
    exit 1
    fi

    # --- Explicitly setting GCC 12 for compilation ---
    if command -v gcc-12 &> /dev/null && command -v g++-12 &> /dev/null; then
    echo "Found gcc-12 and g++-12. Setting CC and CXX environment variables for builds."
    export CC="gcc-12"
    export CXX="g++-12"
    echo "CC is set to: ${CC}"
    echo "CXX is set to: ${CXX}"
    else
    echo "Error: Could not find gcc-12 or g++-12 in PATH. Please ensure they are installed and accessible."
    echo "You might need to adjust the script to point to the exact path if they are not in standard locations."
    exit 1
    fi

    # --- ComfyUI Cloning ---
    echo ""
    echo "ComfyUI repository setup..."
    if [ ! -d "${COMFYUI_DIR}/ComfyUI" ]; then
    git clone "${COMFYUI_REPO}" "${COMFYUI_DIR}/ComfyUI"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to clone ComfyUI repository."
    exit 1
    fi
    else
    echo "ComfyUI directory already exists. Pulling latest changes."
    (cd "${COMFYUI_DIR}/ComfyUI" && git pull)
    fi

    # --- Virtual Environment Setup ---
    echo ""
    echo "Setting up Python virtual environment at ${VENV_DIR}..."
    if [ ! -d "${VENV_DIR}" ]; then
    python${PYTHON_VERSION} -m venv "${VENV_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to create virtual environment."
    exit 1
    fi
    echo "Virtual environment created."
    else
    echo "Virtual environment already exists."
    fi

    # Activate the virtual environment
    source "${VENV_DIR}/bin/activate"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to activate virtual environment."
    exit 1
    fi
    echo "Virtual environment activated."

    # --- Python Package Installation ---
    echo ""
    echo "Installing/Updating core PyTorch, torchvision, and torchaudio with CUDA 12.8 support..."
    if check_pytorch_cuda; then
    echo "PyTorch with CUDA is already installed. Skipping."
    else
    echo "Attempting to uninstall existing PyTorch installations before clean install..."
    pip uninstall -y torch torchvision torchaudio 2>/dev/null
    pip install torch torchvision torchaudio --extra-index-url "${PYTORCH_CUDA_INDEX_URL}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install PyTorch with CUDA 12.8 support. Check your CUDA setup and network connection."
    deactivate
    exit 1
    fi
    echo "PyTorch, torchvision, and torchaudio installed for CUDA 12.8."
    fi

    echo ""
    echo "Installing ComfyUI's core requirements..."
    pip install -r "${COMFYUI_DIR}/ComfyUI/requirements.txt"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to install ComfyUI requirements. Check the log for specific package errors."
    deactivate
    exit 1
    fi
    echo "ComfyUI requirements installed."

    # --- Triton Installation (Latest) ---
    echo ""
    echo "Installing latest Triton..."
    if check_module_import "triton"; then
    echo "Triton is already installed. Skipping."
    else
    pip install --upgrade triton
    if [ $? -ne 0 ]; then
    echo "Warning: Failed to install Triton directly. This might be due to a missing pre-built wheel."
    echo "Pip will attempt to build from source if possible. Ensure 'cmake' and build-essential packages are present."
    fi
    echo "Triton installation attempted."
    fi

    # --- SageAttention Installation (from source) ---
    echo ""
    echo "SageAttention 2++ setup..."
    SAGEATTENTION_DIR="${COMFYUI_DIR}/SageAttention"
    if [ ! -d "${SAGEATTENTION_DIR}" ]; then
    git clone "${SAGEATTENTION_REPO}" "${SAGEATTENTION_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to clone SageAttention repository."
    deactivate
    exit 1
    fi
    else
    echo "SageAttention directory already exists. Pulling latest changes."
    (cd "${SAGEATTENTION_DIR}" && git pull)
    fi

    echo "Installing SageAttention from source (no build isolation)..."
    if check_module_import "sageattention"; then
    echo "SageAttention is already installed. Skipping build."
    else
    (cd "${SAGEATTENTION_DIR}" && pip install -e . --no-build-isolation)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to build and install SageAttention. Check build logs for details."
    echo "Ensure your CUDA Toolkit is properly installed and accessible, and that GCC 12 is correctly configured via CC/CXX."
    deactivate
    exit 1
    fi
    echo "SageAttention 2++ installed."
    fi

    # --- xFormers Installation (from source) ---
    echo ""
    echo "xFormers setup..."
    XFORMERS_DIR="${COMFYUI_DIR}/xformers"
    if [ ! -d "${XFORMERS_DIR}" ]; then
    git clone "${XFORMERS_REPO}" "${XFORMERS_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to clone xFormers repository."
    deactivate
    exit 1
    fi
    echo "Initializing and updating xFormers submodules..."
    (cd "${XFORMERS_DIR}" && git submodule update --init --recursive)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to update xFormers submodules. This is required for compilation."
    deactivate
    exit 1
    fi
    else
    echo "xFormers directory already exists. Pulling latest changes and updating submodules."
    (cd "${XFORMERS_DIR}" && git pull && git submodule update --init --recursive)
    fi

    echo "Installing xFormers from source (this may take a while, no build isolation)..."
    if check_module_import "xformers"; then
    echo "xFormers is already installed. Skipping build."
    else
    (cd "${XFORMERS_DIR}" && pip install -e . --no-build-isolation)
    if [ $? -ne 0 ]; then
    echo "Error: Failed to build and install xFormers. Check build logs for details."
    echo "Ensure your CUDA Toolkit is properly installed and accessible, and that GCC 12 is correctly configured via CC/CXX."
    deactivate
    exit 1
    fi
    echo "xFormers installed."
    fi

    # --- ComfyUI-Manager Installation ---
    echo ""
    echo "ComfyUI-Manager setup..."
    COMFYUI_MANAGER_DIR="${COMFYUI_DIR}/ComfyUI/custom_nodes/ComfyUI-Manager"

    mkdir -p "${COMFYUI_DIR}/ComfyUI/custom_nodes" # Ensure custom_nodes directory exists

    if [ ! -d "${COMFYUI_MANAGER_DIR}" ]; then
    echo "Cloning ComfyUI-Manager repository..."
    git clone "${COMFYUI_MANAGER_REPO}" "${COMFYUI_MANAGER_DIR}"
    if [ $? -ne 0 ]; then
    echo "Error: Failed to clone ComfyUI-Manager repository."
    echo "You may need to install it manually later or check network connectivity."
    else
    echo "ComfyUI-Manager cloned successfully."
    if [ -f "${COMFYUI_MANAGER_DIR}/requirements.txt" ]; then
    echo "Installing ComfyUI-Manager specific requirements (if any)..."
    pip install -r "${COMFYUI_MANAGER_DIR}/requirements.txt"
    fi
    fi
    else
    echo "ComfyUI-Manager directory already exists. Pulling latest changes."
    (cd "${COMFYUI_MANAGER_DIR}" && git pull)
    if [ $? -ne 0 ]; then
    echo "Warning: Failed to pull latest changes for ComfyUI-Manager."
    else
    echo "ComfyUI-Manager updated."
    if [ -f "${COMFYUI_MANAGER_DIR}/requirements.txt" ]; then
    echo "Re-installing ComfyUI-Manager specific requirements (if any)..."
    pip install -r "${COMFYUI_MANAGER_DIR}/requirements.txt"
    fi
    fi
    fi

    # --- Final Message ---
    echo ""
    echo "--- ComfyUI installation process completed for alex ---"
    echo "You can now launch ComfyUI using your desktop launchers."

    # Deactivate the environment at the end of the script to not leave it active globally
    deactivate