How to Set Up a Discord Bot Using Termux in 2026 | Host discord.py 24/7
How to Set Up a Discord Bot Using Termux
Imagine running a custom automation script or community moderator directly from your pocket. You don't need a paid cloud server, an always-on laptop, or complex infrastructure to keep an assistant online — your Android smartphone can handle the job.
This guide walks you through the exact process of building and hosting a script on your mobile device, whether you want a simple utility tool or a termux discord.py bot 24/7 run setup — covering everything from package installation to background process management.
What is a Termux Discord Bot?
A termux discord bot is an automated program, typically written in Python with the discord.py library, that connects to the Discord API using the Android terminal emulator Termux. Termux provides a Linux environment on your phone without requiring root access, so combined with Python it turns your phone into a lightweight, portable server.
Quick Answer: Can Your Phone Really Host a Bot 24/7?
Yes, with caveats. Android aggressively kills background processes to save battery and RAM. To keep your script running without interruptions, you need to disable battery optimization for Termux, run your script inside a persistent terminal session using tmux, and hold a wake lock. Skip these steps and your bot will disconnect whenever your phone enters deep sleep.
Prerequisites and Environment Setup
Before writing any code, prepare your environment. Open Termux and run this initialization sequence to update repositories and install Python:
Essential Installation Commands
pkg update && pkg upgrade
pkg install python git
pip install --upgrade pip
pip install discord.py
If you hit compilation errors during the discord.py install, install build tools first — not a separate "python-dev" package, which no longer exists in Termux (Python's headers already ship inside the main python package). The actual fix is:
pkg install clang make libffi -y
New to Python in Termux generally? See the full Python install guide for background.
Step-by-Step Guide: Building Your First Script
Now that your environment is ready, create a project directory and write a basic Python script.
- Create a new folder:
mkdir my-discord-bot && cd my-discord-bot - Create your script file:
nano bot.py - Paste the template code below, replacing the placeholder token with your actual bot token from the Discord Developer Portal.
Sample Code: Real Use-Case Example
Here's a working utility script that responds to basic commands and checks latency:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name} (ID: {bot.user.id})')
print('------')
@bot.command(name='ping')
async def ping(ctx):
latency = round(bot.latency * 1000)
await ctx.send(f'Pong! Latency is {latency}ms.')
@bot.command(name='status')
async def status(ctx):
await ctx.send('System is online and running locally via Android terminal!')
bot.run('YOUR_BOT_TOKEN_HERE')
Note: intents.message_content requires the "Message Content Intent" toggle to be enabled for your bot in the Discord Developer Portal, or on_message-based commands won't receive text content.
Save the file in nano by pressing Ctrl+O, hitting Enter, and exiting with Ctrl+X.
How to Achieve 24/7 Uptime on Mobile
Keeping your bot online continuously means managing Android's power states. If you simply close the terminal app, the OS terminates the process.
- Acquire a wake lock: Run
termux-wake-lockin your terminal. This prevents the CPU from sleeping. - Use tmux: Install it with
pkg install tmuxand run your script inside a detachable session — see the full guide to installing and using tmux in Termux for the session commands. - Disable battery optimization: Go to Android Settings > Apps > Termux > Battery, and select "Unrestricted" or "Not Optimized."
Comparison: Termux vs. Traditional Cloud Hosting (Heroku, VPS, Replit)
How does mobile hosting compare to traditional cloud services?
| Feature | Termux (Android) | Cloud VPS (DigitalOcean/AWS) | Free Cloud Tiers (Replit/Render) |
|---|---|---|---|
| Cost | Free (uses your existing phone) | Paid ($3-$5+/month) | Free with limitations |
| Uptime | Dependent on phone battery & Wi-Fi | High (data-center reliability) | Sleeps after inactivity on free tiers |
| Setup Complexity | Moderate (Android-specific tweaks) | Moderate (Linux CLI knowledge) | Easy (web-based setup) |
| Hardware Specs | Limited by phone RAM/CPU | Scalable | Very limited on free plans |
Troubleshooting Common Errors
1. RuntimeError: Event loop is closed / Connection Reset
Usually caused by unstable mobile internet switching between Wi-Fi and mobile data. Keep your connection stable where possible, and wrap bot.run() with reconnect/error handling.
2. ModuleNotFoundError: No module named 'discord'
This means the package installed under a different Python environment than the one running your script. Verify with: python3 -m pip install discord.py.
3. Termux Crashes or Stops Randomly
Android's RAM management is likely killing the background process. Confirm you've run termux-wake-lock and excluded Termux from battery restrictions in system settings. If storage-related errors show up alongside the crash, check the Termux storage permission guide.
Expert Tips for Stability
- Keep your phone plugged into a charger if running scripts indefinitely — sustained CPU usage drains batteries quickly.
- Monitor device temperature. Intensive scripts in hot environments can trigger thermal throttling or shutdowns.
- Back up your Python scripts regularly — see how to install Git in Termux to version-control them instead of relying on a single copy on your phone.
Frequently Asked Questions
Can I host a music bot using this setup?
It's not recommended. Audio streaming requires continuous CPU usage and stable bandwidth, which drains your phone's battery rapidly and is prone to frequent disconnects on mobile networks.
Do I need to root my phone?
No. Termux operates entirely within user space without requiring root privileges.
Is my bot token safe inside Termux?
As long as you don't share your device with others or commit your script to a public GitHub repository with the token hardcoded in it, your credentials stay local to your device. Treat a leaked bot token as compromised immediately and regenerate it from the Discord Developer Portal.
Conclusion
Hosting a script from a mobile environment is a genuinely cost-free alternative to cloud servers. By following proper battery configuration, using tmux for persistence, and keeping your dependencies straight, you can maintain reliable uptime right from your pocket.
Ready to take your automation further? Explore more Termux guides on package management, scripting, and mobile development workflows.
Join the conversation