Table of Contents
What you will read?
Hosting a Telegram bot on your own Ubuntu server gives you full control, better performance, and no dependency on third-party platforms.. The process is simple and optimized for 2025, so even if you’re not an expert in Linux, you’ll be able to follow along easily.
Step 1: Update Your Ubuntu Server
Before setting up the Telegram bot, ensure your Ubuntu server is up to date. This makes sure all required packages are fresh and compatible.
sudo apt update && sudo apt upgrade -y
Step 2: Install Python and Pip
Most Telegram bots are written in Python, so you need Python 3 and Pip (Python’s package manager). Run the following command:
sudo apt install python3 python3-pip -y
Step 3: Get Your Bot Token from BotFather
To connect your code with Telegram, you must create a bot using BotFather inside the Telegram app. After creating it, you’ll receive a bot token that looks like this:
123456789:ABCdefGhIJKlmNoPQRstuVWXyz
Keep this token safe, as you’ll need it in your script.
Step 4: Install the Python Telegram Library
The most popular library for creating bots is python-telegram-bot. Install it with:
pip3 install python-telegram-bot --upgrade
Step 5: Create Your Telegram Bot Script
Now create a simple bot script to test the setup. Open a file with:
nano bot.py
Paste this basic example:
from telegram.ext import Updater, CommandHandler
def start(update, context):
update.message.reply_text("Hello! Your bot is working on Ubuntu server.")
updater = Updater("YOUR_BOT_TOKEN", use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", start))
updater.start_polling()
update.idle()
Replace YOUR_BOT_TOKEN with the token you got from BotFather.
Save and exit (CTRL+O, then CTRL+X).
Step 6: Run the Telegram Bot
Start your bot with:
python3 bot.py
Go to Telegram, open your bot, and type /start. If everything is set up correctly, the bot will reply instantly.
Step 7: Keep the Bot Running in the Background
To keep your bot running even after you close the terminal, use tmux or systemd. The easiest way is with tmux:
sudo apt install tmux -y
tmux new -s mybot
python3 bot.py
Detach with CTRL+B then D. Your bot will continue running in the background.
