Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

Run LLaMA Models on Android Locally using Termux + llama.cpp (TinyLLaMA & LLaMA 7B)

Yes, you can run local LLMs on your Android phone — completely offline — using llama.cpp in Termux!
This guide walks you step by step through compiling llama.cpp, downloading quantized .gguf models, running TinyLLaMA or LLaMA 2 7B, and even setting up a simple Chat UI.


Table of Contents

  1. What You’ll Need
  2. Installing Termux and Required Packages
  3. Cloning and Building llama.cpp
  4. Downloading TinyLLaMA and LLaMA 2 7B Quantized Models
  5. Running the Model in CLI
  6. Setting Up a Basic Web Chat UI
  7. Final Thoughts & Tips

What You’ll Need

  • A relatively recent Android phone
    • Minimum 4 GB RAM (8 GB or more for LLaMA 2 7B)
    • Minimum 4-core CPU (Snapdragon 7xx or 8xx preferred)
  • Termux (Download from F-Droid, not Play Store)
  • At least:
    • ~500MB for TinyLLaMA
    • ~4–5GB for LLaMA 7B quantized

Installing Termux and Required Packages

Open Termux and run the following:

pkg update && pkg upgrade -y
pkg install git cmake clang build-essential wget python -y

Make sure you have storage access enabled:

termux-setup-storage

This allows file access from ~/storage for downloaded models.


Cloning and Building llama.cpp

# Clone the llama.cpp repo
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp

# Compile the main binary
make

This builds the ./main program that runs GGUF models locally.


Downloading Models

1. TinyLLaMA - Great for Phones (Only ~500MB)

TinyLLaMA is super lightweight and works well on mid-range phones.

Download command:

mkdir -p models/tinyllama
cd models/tinyllama

# Download the quantized GGUF model
wget https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf

cd ../../

2. LLaMA 2 7B - Requires 8+ GB RAM (Use Q4_0 or Q5_0)

If your phone is powerful (Snapdragon 8 Gen 1+, or 12GB RAM), try this:

mkdir -p models/llama2
cd models/llama2

# Download a 7B quantized model (Q4_0 = decent quality, smaller size)
wget https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGUF/resolve/main/llama-2-7b-chat.Q4_0.gguf

cd ../../

Running the Model

You’re now ready to run the model using the CLI!

Run TinyLLaMA:

./main -m models/tinyllama/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf -p "Hello! Who are you?"

Run LLaMA 2 7B (if phone can handle it):

./main -m models/llama2/llama-2-7b-chat.Q4_0.gguf -p "What is the capital of France?"

Run in Interactive Chat Mode

./main -m models/tinyllama/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf -i

You can now type prompts interactively.


Setting Up a Basic Chat UI (Flask-based)

You can build a simple web-based chat UI running locally on your Android phone.

1. Install Flask and dependencies

pip install flask flask-cors

2. Create a Python Script: chat_server.py

nano chat_server.py

Paste the following:

from flask import Flask, request, jsonify
import subprocess

app = Flask(__name__)

@app.route('/chat', methods=['POST'])
def chat():
    prompt = request.json.get("prompt", "")
    if not prompt:
        return jsonify({"error": "No prompt given."}), 400

    # Call llama.cpp with the prompt
    result = subprocess.run(
        ["./main", "-m", "models/tinyllama/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf", "-p", prompt],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True
    )
    
    return jsonify({"response": result.stdout})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)

3. Run the server

python chat_server.py

Now go to your browser and POST to:

http://localhost:7860/chat

Or use an app like Postman to test it.


Optional: Create an HTML Chat Interface

Make a simple chat page (chat.html) like:

<!DOCTYPE html>
<html>
<head>
    <title>TinyLLaMA Chat</title>
</head>
<body>
    <h2>Chat with TinyLLaMA</h2>
    <textarea id="input" rows="4" cols="50"></textarea><br>
    <button onclick="send()">Send</button>
    <pre id="response"></pre>

    <script>
        function send() {
            fetch("http://localhost:7860/chat", {
                method: "POST",
                headers: {"Content-Type": "application/json"},
                body: JSON.stringify({prompt: document.getElementById("input").value})
            })
            .then(res => res.json())
            .then(data => {
                document.getElementById("response").innerText = data.response;
            });
        }
    </script>
</body>
</html>

Open it in a browser and chat locally!


Final Tips

  • Use TinyLLaMA for best performance on most phones.
  • Use -n 256 to control number of output tokens (example: -n 256 -p "Explain gravity").
  • Monitor RAM usage — LLaMA 7B needs around 4–6 GB RAM for Q4.
  • You can create wrapper scripts or integrate with Android web UIs using Termux:API if you want!

Conclusion

Running local LLMs on Android is now a reality with llama.cpp, especially with small models like TinyLLaMA. For larger models like LLaMA 7B, high-end phones or tablets work well — and with Flask, you can even build your own mobile ChatGPT clone completely offline.

About

Run LLaMA Models on Android Locally using Termux + llama.cpp (TinyLLaMA & LLaMA 7B)

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors