How to Set Up a Local Flask App in Termux (2026)
Want to turn your Android phone into a portable development server? With Termux, you get a real Linux-like terminal on your device, and with Flask, you get a lightweight Python web framework that can serve pages and JSON APIs from that terminal. Together, they let you build, run, and test a working web app entirely from your pocket — no laptop required.
This guide covers the full process: preparing Termux correctly, installing Python and Flask the safe way (inside a virtual environment), writing a working app, running it locally, accessing it from other devices on your Wi-Fi, keeping it alive in the background, and locking it down before you expose it to anyone else.
Quick summary: Update Termux packages, install Python, create a virtual environment, install Flask inside it, write your app.py, then run python app.py. Visit http://127.0.0.1:5000 on your phone, or your device's local IP address (like http://192.168.1.15:5000) from another device on the same network.
What Is a Termux Flask Setup?
A "Termux Flask app" simply means running the Flask Python web framework inside the Termux terminal app on Android. Termux gives you a Linux-style package manager and shell, while Flask handles routing, requests, and responses. Because Android is built on the Linux kernel, Termux can run Python scripts, bind to local network ports, and serve web pages entirely offline — no rooting required.
Before You Start: Prepare Termux Properly
A few setup steps get skipped in most tutorials, and they're exactly where beginners get stuck.
1. Install Termux From F-Droid, Not Play Store
The Play Store build of Termux was deprecated and no longer receives package updates, which causes installation failures for newer Python and Flask versions. Install it from F-Droid or GitHub instead. If you haven't installed it yet, our guide to installing Termux on Android walks through it step by step.
2. Grant Storage Access (If You'll Save Files Outside Termux)
If your Flask app will read or write files outside its own folder — templates, uploads, a SQLite database you want to back up — run:
termux-setup-storage
Approve the Android permission prompt. If you run into permission errors later, see our Termux storage permission fix guide or the full Termux storage access guide.
3. Update Your Packages
pkg update && pkg upgrade -y
If Termux asks about replacing configuration files, press Enter to keep the default. For a deeper look at what this command does and how to keep packages current, check our package update and upgrade guide. If you ever see a "package not found" error at this stage, our unable to locate package fix covers the common causes.
Install Python and Flask (the Safe Way)
Install Python
pkg install python -y
python --version
pip --version
Need more detail on Python itself in Termux? See our full Python installation guide.
Create a Project Folder and Virtual Environment
Installing Flask globally works, but it's not a good habit: global installs get messy fast, and version conflicts between projects are hard to debug. Isolate each project instead:
mkdir flask_app && cd flask_app
python -m venv venv
source venv/bin/activate
Your terminal prompt should now show (venv) at the start of the line. Every pip install you run from here only affects this project. When you're done working, type deactivate to leave the environment.
Install Flask
pip install --upgrade pip
pip install flask
This should take well under a minute on most connections.
Write Your First Flask App
Create the app file:
pkg install nano -y
nano app.py
(Prefer a more capable editor? Compare your options in our Nano vs Vim in Termux guide.)
Paste in a simple app that serves a homepage and a JSON status endpoint:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def home():
return "<h1>Hello from Termux Flask!</h1><p>Your mobile server is running.</p>"
@app.route('/api/status')
def status():
return jsonify({
"status": "online",
"environment": "Termux Android",
"framework": "Flask"
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Save with Ctrl + O, Enter, then exit with Ctrl + X.
host='0.0.0.0' vs '127.0.0.1': What's the Difference?
| Setting | What it means | Use it when |
|---|---|---|
127.0.0.1 (localhost) | Only your own device can reach the server | You're only testing on the same phone |
0.0.0.0 | The server listens on every network interface, so other devices on the same Wi-Fi can reach it via your phone's IP | You want to open the app from a laptop, tablet, or another phone on the same network |
Binding to 0.0.0.0 is what makes LAN access possible, but it also means anyone else on that network can reach the app — keep that in mind on shared or public Wi-Fi.
Run the App and Access It
python app.py
You'll see output similar to:
* Serving Flask app 'app'
* Debug mode: on
* Running on http://127.0.0.1:5000
* Running on http://192.168.1.15:5000 (Press CTRL+C to quit)
Access on the Same Device
Open your phone's browser and go to http://127.0.0.1:5000 or http://localhost:5000.
Access from Another Device on the Same Wi-Fi
Use the local IP address Termux printed (for example http://192.168.1.15:5000) from a laptop or another phone connected to the same network. If you don't see an IP listed, find it manually:
ip addr show wlan0
Look for the "inet" line under your Wi-Fi interface.
Keeping Your Server Running in the Background
Close the Termux window and your server stops — that's normal, not a bug. To keep it alive:
| Method | Best for | Limitation |
|---|---|---|
python app.py & | Quick, short testing sessions | Ends when Termux's process is killed by Android; no easy way to reattach |
tmux | Persistent sessions you can detach from and resume later | Slight learning curve for new users |
screen | Similar persistence to tmux, simpler command set | Fewer features than tmux for multi-pane work |
For most people, tmux is worth the small learning curve. Our full tmux installation and usage guide covers creating, detaching, and reattaching sessions so your Flask server survives even after you close the Termux app.
Also disable Android's battery optimization for Termux in your phone's app settings — otherwise the system may kill the background process to save power, regardless of which method you use.
Troubleshooting Common Errors
ModuleNotFoundError: No module named 'flask'
Cause: Flask isn't installed in the Python environment you're currently running, or your virtual environment isn't activated.
Fix: Confirm (venv) appears in your prompt, then run pip install flask again inside it.
OSError: [Errno 98] Address already in use
Cause: A previous instance of your app is still bound to port 5000.
Fix: Find and stop the specific process rather than killing all Python processes on your system:
pkill -f app.py
Or run the new instance on a different port: app.run(host='0.0.0.0', port=5050).
Permission Denied or Cannot Bind to Host
Cause: You tried to bind to a privileged port (below 1024, like port 80) without root access.
Fix: Stick to unprivileged ports such as 5000, 8000, or 8080, which work fine without root on Android.
Optional: Expose Your App Beyond Your Wi-Fi
Local and LAN access covers most development needs, but if you want to share a running demo with someone outside your network, a tunneling tool forwards a public URL to your phone without router configuration.
| Tool | Setup effort | Good for |
|---|---|---|
| ngrok | Very low — one command after signup | Quick, temporary demo links |
| Cloudflare Tunnel | Slightly more setup, but no session time limits on the free tier for basic use | Longer-running or repeatable shares |
For persistent private access to your own devices instead of public sharing, an alternative is running an SSH connection between machines — see our guide to using SSH in Termux.
Whichever tool you choose, treat it the same way you'd treat any server exposed to the internet: see the security checklist below before you share a public link.
Going Further: Production-Style Serving
Flask's built-in app.run() server is meant for development, not for handling real traffic or running unattended for long periods. The official Flask documentation recommends a proper WSGI server for anything beyond local testing. In Termux, Gunicorn is a common lightweight choice:
pip install gunicorn
gunicorn -w 2 -b 0.0.0.0:5000 app:app
This runs two worker processes and is more stable than the development server if your app needs to stay up for a while. It's optional for casual local testing, but worth knowing before you rely on your phone as a "real" server.
Security Checklist Before You Expose Your App
- Never leave
debug=Trueset if your app is reachable outside your own device — Flask's debugger can allow arbitrary code execution if someone else can reach it. - Only bind to
0.0.0.0when you actually need LAN or remote access; use127.0.0.1otherwise. - Change the default port if you're exposing the app publicly, and don't reuse it across unrelated projects.
- Prefer a private tunnel or SSH connection over public exposure when the app isn't meant for strangers.
Flask vs. Other Local Server Options in Termux
| Tool | Language | Learning Curve | Best For |
|---|---|---|---|
| Flask | Python | Low–Medium | APIs, prototypes, small dynamic sites |
| Node.js (Express) | JavaScript | Medium | Real-time apps, full-stack JS work — see our Node.js and npm setup guide |
| PHP built-in server | PHP | Low | Legacy apps, quick PHP testing — see our PHP install guide |
| Static file hosting | Any | Very Low | Serving plain HTML/CSS — see our hosting a local website in Termux guide |
Frequently Asked Questions
Can I access my Termux Flask app from the internet, not just my Wi-Fi?
Not by default — it's only reachable on your local network unless you add a tunneling tool like ngrok or Cloudflare Tunnel, as covered above.
Does running Flask in Termux need root access?
No. Termux runs in a sandboxed user space, and standard package installs and unprivileged ports (5000, 8080, etc.) work fine on unrooted devices.
Can I connect a database to my Flask app in Termux?
Yes. SQLite works out of the box with Python's built-in sqlite3 module or SQLAlchemy, with no separate server needed. If your project needs a full client-server database instead, see our MySQL/MariaDB installation guide for Termux.
How do I run a task automatically, like restarting my server on a schedule?
For scheduled or recurring tasks alongside your app, see our guide to cron jobs in Termux.
Conclusion
You now have a working Flask server running locally in Termux, reachable from your own device and from others on your Wi-Fi, with a clear path to keeping it alive in the background, exposing it safely when needed, and hardening it before anyone else touches it. That's a real, working mobile development setup — not just a toy demo.

Join the conversation