Skip to main content
← All guides

Hosting a Discord bot 24/7 (discord.js, discord.py)

Here's how to run your bot on an Ubuntu server so it keeps working even when your PC is off. We assume your bot code is already finished and runs fine on your PC.

1. Move your bot code to the server

Move your bot folder to the server using Uploading and downloading files, or if it's on GitHub, pull it directly on the server.

git clone https://github.com/myid/mybot.git
cd mybot

🔒 Never write your bot token directly in your code or upload it to GitHub. If the token leaks, anyone can control your bot. We recommend keeping it separately in a .env file on the server.

Node.js bot (discord.js)

Install Node.js

The Node.js in Ubuntu's default repositories is an old version, so the latest discord.js may not work with it. Install it from the official distribution.

curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
node -v

Install packages, then run with pm2

pm2 turns your bot back on automatically if it stops, and starts it again even after the server reboots.

npm install
sudo npm install -g pm2
pm2 start index.js --name my-bot     # Change this to match your start file's name
pm2 save
pm2 startup                          # Copy the command it prints and run that too

Common commands:

pm2 logs my-bot      # View live logs (to exit: Ctrl + C)
pm2 restart my-bot   # Restart after changing your code
pm2 status           # Check status

Python bot (discord.py)

Create a virtual environment and install packages

sudo apt install -y python3-venv
python3 -m venv venv
./venv/bin/pip install -r requirements.txt

Keep it running with systemd

Create a service file.

sudo nano /etc/systemd/system/my-bot.service

Change the paths in WorkingDirectory and ExecStart to where your bot folder actually is (check with the pwd command), and User to your username.

[Unit]
Description=My Discord Bot
After=network-online.target

[Service]
User=username
WorkingDirectory=/home/username/mybot
ExecStart=/home/username/mybot/venv/bin/python bot.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now my-bot
sudo systemctl status my-bot          # Success if it shows active (running)
journalctl -u my-bot -f               # View live logs

My bot is offline

  • Check the logs first (pm2 logs or journalctl -u my-bot). Token errors and missing packages are the most common causes.
  • A bot isn't a service that accepts connections from outside, so you don't need to open any firewall ports for it.
  • If the server itself isn't responding, see Can't connect to your server.

If your problem isn't solved, open a ticket on our Discord.