Posted on
Artificial Intelligence

Artificial Intelligence Image Generation on Linux

Author
  • User
    linuxbash
    Posts by this author
    Posts by this author

Artificial Intelligence Image Generation on Linux (The Bash Way)

What if the next poster, icon set, or concept art for your project could be generated in seconds—locally, privately, and scriptable from Bash? With today’s open models and tools, Linux is one of the best platforms for running AI image generation on your own hardware. This guide shows you why that’s powerful, how to set it up, and gives you actionable steps to generate your first images and automate workflows.

Why generate images on Linux?

  • Control and privacy: Run models locally—no cloud fees, no data leaving your machine.

  • Speed and cost: Once set up, iteration is fast and cheap compared to API calls.

  • Automation: Linux + Bash excels at reproducible pipelines, batch jobs, and cron.

  • Open ecosystem: Community-driven tools like Stable Diffusion WebUI, ComfyUI, and InvokeAI run great on Linux.


1) Prep your system: check hardware and install essentials

First, check what GPU you have and how much VRAM is available. More VRAM helps, but you can also run many workflows on CPU or low-VRAM GPUs.

lspci | grep -E 'NVIDIA|AMD|Intel'

NVIDIA:

nvidia-smi

AMD (ROCm-capable):

/opt/rocm/bin/rocm-smi  # if ROCm installed

Then, install core packages. Pick the command for your distro:

  • Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y python3 python3-venv python3-pip git git-lfs ffmpeg wget curl jq build-essential cmake
  • Fedora/RHEL (dnf):
sudo dnf install -y python3 python3-pip python3-virtualenv git git-lfs ffmpeg wget curl jq cmake @development-tools
  • openSUSE (zypper):
sudo zypper refresh
sudo zypper install -y python3 python3-pip python3-virtualenv git git-lfs ffmpeg wget curl jq cmake
sudo zypper install -t pattern devel_basis

Notes:

  • Install proper GPU drivers from your distro/vendor. Exact steps vary; consult NVIDIA/AMD/Intel docs.

  • git-lfs is required for large model files (Hugging Face, etc.).

Initialize Git LFS:

git lfs install

2) Choose your workflow tool (UI vs. node-graph vs. CLI)

Below are three popular, well-supported options. You can install more than one.

Option A: AUTOMATIC1111 Stable Diffusion WebUI (friendly GUI + API)

  • Pros: Easy to use; huge plugin ecosystem; REST API available.

  • Cons: Monolithic but very capable.

Install and run:

git clone https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
cd stable-diffusion-webui
bash webui.sh --xformers --api

Tips:

  • If you have issues with xformers (especially on non-NVIDIA), drop --xformers.

  • For low VRAM GPUs, try: bash webui.sh --medvram --api or --lowvram.

  • The UI runs on http://127.0.0.1:7860 by default. The --api flag enables a REST endpoint.

Model placement for A1111:

mkdir -p models/Stable-diffusion
# Put .safetensors/.ckpt files here

Option B: ComfyUI (node-based, reproducible workflows)

  • Pros: Visual node graphs; great for complex pipelines and automation.

  • Cons: Slightly steeper learning curve.

Install:

git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip

# Choose ONE of the torch installs that matches your setup:
# NVIDIA CUDA (example for CUDA 12.1):
pip install --index-url https://download.pytorch.org/whl/cu121 torch torchvision

# OR CPU-only (works everywhere, slower):
# pip install --index-url https://download.pytorch.org/whl/cpu torch torchvision

pip install -r requirements.txt
python main.py --listen 0.0.0.0 --port 8188

Model placement for ComfyUI:

mkdir -p models/checkpoints
# Put SD/SDXL .safetensors checkpoints in models/checkpoints

Open http://127.0.0.1:8188.

Option C: InvokeAI (streamlined installer, UI + CLI)

  • Pros: Nice onboarding, good CLI, image-to-image/inpainting features.

  • Cons: Slightly different workflow than A1111.

Install:

python3 -m venv ~/invokeai-venv
source ~/invokeai-venv/bin/activate
pip install --upgrade pip
pip install "invokeai>=4"
invokeai-configure
invokeai-web

Open the provided local URL.


3) Get models (the right way)

Many high-quality models require accepting licenses on Hugging Face. Use the CLI to authenticate and download:

python3 -m venv ~/hf-venv
source ~/hf-venv/bin/activate
pip install --upgrade pip
pip install huggingface_hub

huggingface-cli login
# Follow browser prompt or paste token

Example: download SDXL Turbo (fast prototyping):

huggingface-cli download --repo-type model stabilityai/sdxl-turbo --local-dir ./sdxl-turbo

Then place the checkpoint files where your tool expects them:

  • A1111:
cp ./sdxl-turbo/*.safetensors stable-diffusion-webui/models/Stable-diffusion/
  • ComfyUI:
cp ./sdxl-turbo/*.safetensors ComfyUI/models/checkpoints/

Alternatively, some repositories can be pulled with Git LFS:

git clone https://huggingface.co/runwayml/stable-diffusion-v1-5

Always check each model’s license and usage restrictions.


4) Generate your first image (GUI and pure Bash options)

A) Click-to-generate in the browser

  • A1111: Open http://127.0.0.1:7860, enter a prompt like: “a cozy cabin under the northern lights, cinematic lighting, 4k, high detail”

  • Choose Steps ~20–30, Sampler (e.g., DPM++ 2M), Resolution 768×512, then Generate.

  • ComfyUI: Load an example workflow (ComfyUI includes demo nodes). Wire prompt → sampler → VAE decode → SaveImage, then Queue Prompt.

B) Bash: call the A1111 REST API with curl

Start A1111 with --api flag, then:

curl -s -X POST 'http://127.0.0.1:7860/sdapi/v1/txt2img' \
  -H 'Content-Type: application/json' \
  -d '{
    "prompt": "a cozy cabin under the northern lights, cinematic lighting, 4k, high detail",
    "steps": 22,
    "width": 768,
    "height": 512,
    "seed": 12345
  }' \
| jq -r '.images[0]' | base64 -d > out.png

Now out.png is your generated image—fully scriptable and automatable.

C) Minimal Python script (Diffusers) that runs on CPU or GPU

Great when you want a tiny reproducible script (works inside any venv):

python3 -m venv ~/diffusers-venv
source ~/diffusers-venv/bin/activate
pip install --upgrade pip

# Choose ONE Torch install:
# CPU-only:
pip install --index-url https://download.pytorch.org/whl/cpu torch torchvision

# OR NVIDIA CUDA 12.1 example:
# pip install --index-url https://download.pytorch.org/whl/cu121 torch torchvision

pip install diffusers transformers accelerate safetensors
python - << 'PY'
from diffusers import AutoPipelineForText2Image
import torch

pipe = AutoPipelineForText2Image.from_pretrained(
    "stabilityai/sdxl-turbo",
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    variant="fp16" if torch.cuda.is_available() else None
)
if torch.cuda.is_available():
    pipe = pipe.to("cuda")

image = pipe(
    prompt="studio photo of a modern chair, soft shadows, product shot, 8k",
    num_inference_steps=4,  # SDXL Turbo is fast with very few steps
    guidance_scale=0.0
).images[0]

image.save("chair.png")
print("Saved chair.png")
PY

5) Optimize and automate (real-world tips)

  • Low VRAM survival kit:

    • A1111 flags: --medvram or --lowvram. For some AMD setups: --precision full --no-half.
    • Diffusers: enable attention slicing:
    pipe.enable_attention_slicing()
    
  • Install xformers (NVIDIA) for speedups in A1111/Comfy:

    • A1111 with --xformers tries to handle it automatically in many cases.
    • Manual (example CUDA 12.1):
    pip install xformers==0.0.23.post1 --index-url https://download.pytorch.org/whl/cu121
    
  • Batch generation with Bash and seeds:

    for s in 1 2 3 4 5; do
    curl -s -X POST 'http://127.0.0.1:7860/sdapi/v1/txt2img' \
      -H 'Content-Type: application/json' \
      -d "{
        \"prompt\": \"isometric pixel-art village, warm lighting, highly detailed\",
        \"steps\": 30,
        \"width\": 768,
        \"height\": 768,
        \"seed\": $s
      }" \
    | jq -r '.images[0]' | base64 -d > "village_$s.png"
    done
    
  • Reproducibility:

    • Pin versions in requirements.txt or a conda/uv environment.
    • Keep models and workflows in Git (LFS enabled) for team collaboration.

Common troubleshooting

  • Torch/CUDA mismatch: Ensure your Torch wheel matches your installed CUDA (e.g., cu118, cu121). Check with python -c "import torch; print(torch.version.cuda)".

  • ROCm/AMD: Follow AMD ROCm docs for your distro and GPU generation. Many tools also work CPU-only if ROCm is not available.

  • Fedora ffmpeg: If dnf install ffmpeg fails, enable RPM Fusion (per Fedora docs) and try again.


Conclusion and next steps (CTA)

You now have the building blocks to run AI image generation on Linux—from set up to your first image and beyond. Pick a tool (A1111, ComfyUI, or InvokeAI), download a model you’re comfortable licensing, and start creating. Then:

  • Automate with Bash and the A1111 API for batch jobs.

  • Explore ComfyUI for complex, shareable node workflows.

  • Experiment with specialized models (photoreal, anime, product shots, SDXL refiners).

When you’re ready, script your own pipeline: prompt lists, seeds, and outputs organized by timestamp—fully reproducible. Your terminal is now a creative studio.