Guideschevron_rightServers

Servers · 7 min read · Updated

How to Host a Discord Bot on a VPS, Always Online

To keep a Discord bot online all the time, put its code on a VPS, keep the bot token in an environment file, and run it as a systemd service so it starts with the server and restarts if it crashes. A bot only connects out to Discord, so it needs no open ports and very little memory: the smallest VPS (6 GB, $37.50 a month) can run several. The steps are the same for Node.js and Python bots.

1. Create the Bot and Copy Its Token

In the Discord Developer Portal, create an application, open Bot, and reset the token to copy it. Treat the token like a password: anyone with it controls your bot. Turn on any privileged intents your bot needs, then invite it to your server from OAuth2 → URL Generator with the bot scope.

2. Prepare the Server

Order a VPS with Ubuntu 24.04, secure it, then create a user for the bot so it never runs as root:

sudo useradd -m -s /bin/bash bot
sudo mkdir -p /home/bot/app && sudo chown bot:bot /home/bot/app

For a Node.js bot, install a current Node.js:

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

For a Python bot, Ubuntu's Python is enough; add the virtual environment package:

sudo apt install -y python3-venv

3. Put the Code on the Server

Clone your repository into /home/bot/app as the bot user, or copy it up with rsync. Then install its dependencies:

sudo -u bot -i
cd ~/app
git clone https://github.com/you/your-bot.git .

npm ci                                     # Node.js
# or, for Python:
python3 -m venv .venv && .venv/bin/pip install -r requirements.txt

A minimal bot to test with, in Node.js (discord.js) and Python (discord.py):

// index.js
import { Client, GatewayIntentBits } from "discord.js";
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once("ready", (c) => console.log(`Logged in as ${c.user.tag}`));
client.login(process.env.DISCORD_TOKEN);
# bot.py
import os, discord
client = discord.Client(intents=discord.Intents.default())

@client.event
async def on_ready():
    print(f"Logged in as {client.user}")

client.run(os.environ["DISCORD_TOKEN"])

4. Keep the Token Out of the Code

# /home/bot/app/.env  (readable only by the bot user)
DISCORD_TOKEN=paste-your-token-here
chmod 600 ~/app/.env
echo ".env" >> ~/app/.gitignore

5. Run It as a Service

As a user with sudo, create /etc/systemd/system/discord-bot.service:

[Unit]
Description=Discord bot
After=network-online.target
Wants=network-online.target

[Service]
User=bot
WorkingDirectory=/home/bot/app
EnvironmentFile=/home/bot/app/.env
ExecStart=/usr/bin/node index.js
# Python: ExecStart=/home/bot/app/.venv/bin/python bot.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now discord-bot
journalctl -u discord-bot -f          # "Logged in as ..." means it is online

Creating a systemd service explains each line. To update the bot, pull the new code as the bot user and run sudo systemctl restart discord-bot.

Running Several Bots

Give each bot its own folder and service file (discord-bot-music, discord-bot-mod and so on). Check memory with systemctl status on each; when the total nears the plan's memory, move to a larger plan from the dashboard. A bot with a database or a web dashboard can share the same server; see running Docker on a VPS if you prefer containers.

Common Questions

  • How much does it cost to keep a Discord bot online all the time?

    Only the server. A small bot uses well under 200 MB of memory, so the smallest VPS runs it, and several bots besides. There is no per-bot fee: it is your server.

  • Does the bot need an open port?

    No. A bot connects out to Discord's gateway, so no incoming port has to be opened. Keep the firewall closed except for SSH, unless the bot also serves a web dashboard.

  • Why does my bot not see messages?

    Reading message text needs the Message Content intent: turn it on under Bot → Privileged Gateway Intents in the Discord Developer Portal, and request it in your code. Bots in 100 or more servers need Discord's approval for it.

  • Python or Node.js?

    Either: discord.js for Node.js and discord.py for Python are both mature. Use the language you know; the hosting steps are the same.

Plans and prices

VPS Plans

KVM servers ready in minutes, with full root access and a fixed IPv4 address. Plenty for a bot or ten.

See all plans
  • Bronze

    3 vCPU · 6 GB RAM · 75 GB NVMe

    2 TB transfer

    $37.50/mo

    Configure inAshburnDallas
  • Silver

    4 vCPU · 8 GB RAM · 100 GB NVMe

    4 TB transfer

    $46.50/mo

    Configure inAshburnDallas
  • Gold

    6 vCPU · 12 GB RAM · 150 GB NVMe

    6 TB transfer

    $60.00/mo

    Configure inAshburnDallas
  • Diamond

    8 vCPU · 16 GB RAM · 200 GB NVMe

    8 TB transfer

    $73.50/mo

    Configure inAshburnDallas

Network port: 10 Gbps in Ashburn, VA, 50 Gbps in Dallas, TX. Pick the location when you configure.

Keep Reading

All guides