Self-hosted Discord bot: discord.py + 24/7 on VPS

calendar_month May 08, 2026 schedule 8 min read visibility 14 views
person
Valebyte Team
Self-hosted Discord bot: discord.py + 24/7 on VPS
For stable operation of a Discord bot on 1000 servers, a VPS with 1 GB RAM, 1 vCPU, and Ubuntu 22.04 is sufficient — such a configuration, starting from $4-5 per month, provides 99.9% uptime and allows you to run discord.py or discord.js scripts around the clock.

Moving a bot from free platforms like Replit or Heroku to your own server is a critical step for any serious project. Free tiers often limit CPU time, "sleep" processes during inactivity, and impose strict limits on RAM. Your own discord bot vps provides full control over the environment, a guaranteed dedicated IP address, and the ability to scale resources as your audience grows.

System Requirements and Choosing discord bot hosting

Choosing the right hardware directly depends on your application's functionality. A simple moderator bot that processes text commands consumes minimal resources. However, if you plan to run a music bot self host with audio stream processing or a bot with AI elements, CPU and RAM requirements increase significantly.

Estimated Specifications for Different Bot Types

Below is a table with recommended VPS specifications depending on the load and the number of servers (guilds) the bot is on.

Bot Type Server Count CPU (vCPU) RAM (GB) Disk (NVMe)
Small (Utility/Mod) 1 - 100 1 Core (Shared) 512 MB - 1 GB 10 GB
Medium (Economy/Games) 100 - 1000 1-2 Cores 2 GB 20 GB
Large (Music/Global) 1000+ 2-4 Cores 4 GB+ 40 GB
Music Bot (Lavalink) Any 2 Cores (High freq) 2 GB+ 15 GB

When choosing discord bot hosting, pay attention to the data center location. Discord uses servers worldwide, but to reduce latency (ping) when processing commands and especially when transmitting voice, it is better to choose Europe or the USA. For users in Eastern Europe, locations in the Netherlands or Germany are optimal.

Why 1GB RAM is the Golden Standard?

The discord.py vps library version 2.0+ is optimized for memory usage, but Member Cache can quickly "eat up" available resources. On 1000 servers with privileged intents enabled (member and message intents), a bot can consume from 600 to 800 MB of RAM. By leaving a margin up to 1 GB, you protect the process from the Linux Out-Of-Memory (OOM) killer.

Server Preparation and Installing the discord.py vps Environment

For our work, we will use Ubuntu 22.04 LTS as the most stable and well-documented OS for servers. First, you need to update the packages and install Python. Unlike developing a Telegram bot on aiogram, Discord bots are more sensitive to the version of the aiohttp library that comes bundled with discord.py.

Installing Necessary Dependencies

Run the following commands in your VPS terminal:

sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-venv git build-essential -y

Creating a virtual environment is a mandatory practice. This isolates your bot's dependencies from system libraries, preventing version conflicts.

mkdir my_bot && cd my_bot
python3 -m venv venv
source venv/bin/activate
pip install discord.py python-dotenv

For secure storage of tokens and access keys, we recommend using environment variables. You can store them in a .env file or use Vaultwarden to manage your development team's secrets.

Looking for a reliable server for your projects?

VPS from $10/mo and dedicated servers from $9/mo with NVMe, DDoS protection, and 24/7 support.

View offers →

Automation and Running discord bot 24 7 via systemd

Simply running the script via python main.py is not enough. If the SSH session closes or the server reboots, the bot will turn off. To ensure discord bot 24 7 operation, you need to create a system service in the systemd process manager.

Creating the Service File

Create the configuration file:

sudo nano /etc/systemd/system/discordbot.service

Insert the following content, replacing the paths with your own:

[Unit]
Description=Discord Bot Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/my_bot
ExecStart=/root/my_bot/venv/bin/python /root/my_bot/main.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

The Restart=always parameter ensures that if the bot crashes due to a code error or Discord API issues, the system will automatically bring it back up after 5 seconds. This is critical for maintaining high uptime.

Managing the Service

After saving the file, run the commands to activate it:

sudo systemctl daemon-reload
sudo systemctl enable discordbot
sudo systemctl start discordbot
sudo systemctl status discordbot

To monitor errors in real-time, use journalctl -u discordbot -f. If you want advanced bug tracking without paid subscriptions, check out self-hosted Sentry, which can also be deployed on a separate VPS.

Creating music bot self host: Lavalink and Resource Optimization

Music bots are the most resource-intensive. Direct audio transmission via Python (FFmpeg) heavily loads the CPU and often leads to audio "stuttering." The professional solution is using Lavalink. This is a separate node written in Java that handles all the decoding and streaming work.

Setting up Lavalink on VPS

Lavalink requires Java 17 or higher. Install it:

sudo apt install openjdk-17-jre-headless -y

Download the latest version of Lavalink.jar and create an application.yml file. Specify the password and ports in the configuration. Remember that Lavalink can consume a lot of RAM, so for a music bot self host, a VPS with at least 2 GB of RAM is recommended.

Example of a basic application.yml config:

server:
  port: 2333
  address: 0.0.0.0
lavalink:
  server:
    password: "youshallnotpass"
    sources:
      youtube: true
      bandcamp: true
      soundcloud: true
      twitch: true
      vimeo: true
      http: true
      local: false

In your Python bot code, you will need the wavelink or disnake-lavalink library to interact with this node. This separation allows you to move the "heavy" part of the bot to a separate server if the main discord bot vps can no longer handle the load.

Migration from Replit and Heroku to a Full VPS

If you are used to Replit, moving to a VPS might seem complicated due to the lack of a built-in IDE and automatic deployment. However, the benefits outweigh the drawbacks: no disk read/write limits and dedicated 100% CPU resources. When migrating from Heroku, it's important to remember that on a VPS, the file system is persistent. You no longer need external cloud storage for simple JSON databases or SQLite.

Step-by-Step Migration Plan

  1. Data Export: Download your database (e.g., database.db) and configuration file.
  2. Environment Setup: Install the same major version of Python that was used previously.
  3. Dependency Installation: Use pip freeze > requirements.txt on the old platform and pip install -r requirements.txt on the new server.
  4. DB Setup: If you used PostgreSQL on Heroku, you can deploy your own container or install the service directly on the VPS.

For storing your bot's documentation or internal project Wiki, self-hosted Outline or BookStack is a great fit, allowing your development team to always have access to up-to-date instructions.

Databases: When is SQLite No Longer Enough?

For small bots, SQLite is the perfect choice. It's a file that doesn't require database server configuration. But as soon as your discord bot vps starts serving more than 500 servers, problems with write locks (database is locked) arise.

At this point, it's worth switching to PostgreSQL. It supports concurrent queries and is much more reliable under high loads. Setting up PostgreSQL on Ubuntu:

sudo apt install postgresql postgresql-contrib -y
sudo -i -u postgres psql

For your bot's analytics and dashboards, you can use self-hosted solutions like Umami to track command popularity without violating user privacy.

Security of Your discord bot vps

Open SSH access and standard ports make your server a target for brute-force attacks. Since the bot has access to tokens and potentially user data, security must be a priority.

  • SSH via Keys: Disable password login in /etc/ssh/sshd_config.
  • Firewall Setup (UFW): Allow only necessary ports (SSH, port for Lavalink if it's external).
  • Fail2Ban: Install the utility to automatically block IP addresses attempting to brute-force passwords.
sudo ufw allow 22/tcp
sudo ufw allow 2333/tcp # For Lavalink
sudo ufw enable

If your bot collects applications or works as technical support, integration with Chatwoot will allow you to aggregate messages from Discord and other messengers into a single CRM system on your own server.

Scaling: Sharding and Microservices

Discord imposes a limit: one WebSocket shard can serve up to 2500 servers. If your bot grows further, you must implement Sharding. The discord.py vps library supports AutoShardedClient, which automatically splits the load into multiple threads.

Upon reaching the limit of 5000-10000 servers, you will need to consider a distributed architecture. In this case, the bot is divided into:

  • Gateway Cluster: Handles only the connection to Discord.
  • Worker Nodes: Process the business logic of commands.
  • Message Broker (Redis/RabbitMQ): Passes messages between the cluster and workers.

This scheme allows you to horizontally scale discord bot hosting, adding new VPS instances as needed without stopping the main service.

Conclusions

To run a reliable Discord bot, it is optimal to use a VPS with 1-2 GB RAM and Ubuntu OS, setting up automatic restarts via systemd to ensure 24/7 uptime. Moving from free cloud platforms to a self-hosted solution not only saves budget during scaling but also gives full control over the data and performance of your application.

Ready to choose a server?

VPS and dedicated servers in 72+ countries with instant activation and full root access.

Start Now →

Share this post:

support_agent
Valebyte Support
Usually replies within minutes
Hi there!
Send us a message and we'll reply as soon as possible.