Skip to main content

How to run Stable Diffusion on Termux on Android phone

Smartphone Termux tutorial Stable Diffusion AI Android Termux
Table of Contents

๐Ÿ‡น๐Ÿ‡ผ ไธญๆ–‡็‰ˆ

Stable Diffusion is an open-source deep-learning model. It can generate (paint) images conditioned on text descriptions.

I tried to run Stable Diffusion on my Android phone and it worked. No root required.

But why…? Because why not. A developer, Raoul Straczowski, on Github ran Stable Diffusion on his Raspberry Pi 4 and it could generate images up to 400x400 pixels. I think it should work on flagship Android phones, too. Therefore I tried many ways and it finally worked. The result is not bad.

1. Environment

  • Device: Sony Xperia 5 II (Android 13)
  • CPU: Qualcomm Snapdragon 865 (Adreno 650)
  • RAM: 8GB
  • Storage: 30GB+
  • High speed network for downloading large files.

2. Setup Linux environment

  1. Install Termux

  2. Install proot Debian. We only need rootfs, there is no need to add a new user and install desktop environment.

  3. Log into Proot Debian

proot-distro login debian --shared-tmp
  1. Install Git, Vim, Python, Pip
apt update
apt install git git-lfs vim python3 python3-pip
  1. Install Anaconda if you need it.

3. Install Stable Diffusion

There are many versions of Stable Diffusion. I follow Raoul Straczowski’s instructions and choose Stable Diffusion v1.5 made by runwayml. It could run headlessly in terminal.

  1. Clone runwayml’s repository and download all models (around 30GB)
git clone
cd stable-diffusion-v1-5
git lfs pull
  1. Then install python dependences
pip install --upgrade diffusers transformers accelerate ftfy xformers

4. Creating a script for generating images

Because my phone only has 8GB RAM, the maximum image size that it could generate is 320x320 pixels. Beyond that Termux will crash immediately.

  1. Disable background process in Developer Settings. I also enable “Stay awake while charging”.

  2. Termux will crash when generating large images due to the limitation of Android OS, so we have to increase the value of max_phantom_processes. Please refer to Fix Singal 9 error to install ADB tools on PC, and execute following commands to set value of max_phantom_processes to 2147483647.

adb shell "/system/bin/device_config set_sync_disabled_for_tests persistent; /system/bin/device_config put activity_manager max_phantom_processes 2147483647"
  1. Create a Python script.
cd ~
  1. Type these
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler

# Use local models
model_id = "./stable-diffusion-v1-5"

# Use Euler sampling
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")

# Enable low memory usage, set image size to 320x320 and steps to 50.
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, low_cpu_mem_usage=True, height=320, width=320, num_inference_steps=50)

# Bypass NSFW filter
pipe.safety_checker = lambda images, clip_input: (images, False)

# Prompts
prompt = "A victorian woman stands on grass field."

# Start generating the image. Use CPU only
image = pipe(prompt).images[0]

# Save the image"result.png")
  1. Let’s go!
  1. Wait for 20 minutes

  2. We can see Stable Diffusion consumes almost all memory while computing

  3. To view the generated image, move it to the internal storage

mv result.png /sdcard
  1. Here is the result. Not bad, huh?

5. Running Stable Diffusion with Adreno GPU

We can run Stable Diffusion with Adreno GPU to accelerate the generation of images.

This is very experimental. Virglrenderer may not work on non-Adreno devices which do not support Vulkan.

  1. Install virglrenderer on Termux. Then start virgl server.
MESA_NO_ERROR=1 MESA_GL_VERSION_OVERRIDE=4.0 GALLIUM_DRIVER=zink virgl_test_server --use-egl-surfaceless
  1. Log into Proot. Run Python script with virgl
GALLIUM_DRIVER=virpipe python
  1. The time of image generation will reduce a little bit.

  2. The result.

6. What if we run Stable Diffusion on chroot?

Note: You need root permission to do this.

Since Stable Diffusion requires a lot of RAM, why not create a big swapfile on chroot? So Termux will not crash due to out of memory.

  1. Install chroot Ubuntu in Termux.

  2. Install Python and Stable Diffusion (similar to Ubuntu) or just copy files from proot-distro (the data is located at /data/data/com.termux/files/usr/var/lib/proot-distro/installed-rootfs/ubuntu/root).

  3. Log into chroot Ubuntu. Create a 8GB swap file on chroot, so we will have 8 + 4 + 8 = 20GB RAM on our phone. If we don’t do this, the phone will reboot when it is out of memory.

dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress
chmod 0600 /swapfile
mkswap /swapfile
swapon /swapfile
  1. In the following test, I increase the size of image to 512x512 but Termux still crash while computing, so I enter chroot via ADB shell.

  2. Wait for 1 hour, and here is the result. Her face is like saying “Why did you spend so much time on doing this?”



Run Windows exe on Android through Proot Exagear on Termux
Smartphone Termux tutorial Termux Windows Android
An experiment of running postmarketOS on Android with Termux Proot
Smartphone Termux tutorial postmarketOS Termux
How to play Minecraft Java Edition on Android using Termux proot
Smartphone Termux tutorial Raspberry Pi Minecraft Termux

Leave a comment

Choose the comment system you like. Giscus for codes, Disqus for anonymous comments.

Please login to your Github account to leave a comment. You can post codes here for discussion. For images, please upload to Imgur and copy links. Your comments would be visible to everyone on Github Discussions.

This comment system is provided by Disqus, you may see forcing Disqus ADs on this page. Sometimes your comment may need to be reviewed, resulting in delayed display. Thank you for your understanding.