...
How to host a discord bot on ubuntu vps
Learn how to host a discord bot on ubuntu vps!

This article provides a guide demonstrating how to host a Discord bot on Ubuntu VPS.

Running your Discord bot on a VPS (Virtual Private Server) gives you full control, better uptime, and the ability to keep your bot online 24/7 without relying on your local computer.

This guide walks you through the steps to host a Discord bot on Ubuntu VPS using Node.js or Python, securing the server, and keeping the bot running permanently with PM2 or systemd.

Requirements

Before you are able to host a Discord bot on Ubuntu VPS, you’ll need:

Popular VPS providers include:

Launch 100% ssd ubuntu vps from $3. 19/mo!


Compare Ubuntu VPS Plans

KVM-SSD-1
KVM-SSD-8
KVM-SSD-16
KVM-SSD-32
CPU
1 Core
2 Cores
4 Cores
8 Cores
Memory
1 GB
8 GB
16 GB
32 GB
Storage
16 GB NVMe
128 GB NVMe
256 GB NVMe
512 GB NVMe
Bandwidth
1 TB
4 TB
8 TB
16 TB
Network
1 Gbps
1 Gbps
1 Gbps
1 Gbps
Delivery Time
⏱️ Instant
⏱️ Instant
⏱️ Instant
⏱️ Instant
Location
US/FR
US/FR
US/FR
US/FR
Price
$7.58*
$39.50*
$79.40*
$151.22*
KVM-SSD-1
CPU: 1 Core
Memory: 2 GB
Storage: 16 GB NVMe
1 TB
KVM-SSD-8
CPU: 2 Cores
Memory: 8 GB
Storage: 128 GB NVMe
4 TB
KVM-SSD-16
CPU: 4 Cores
Memory: 16 GB
Storage: 256 GB NVMe
8 TB
KVM-SSD-32
CPU: 8 Cores
Memory: 32 GB
Storage: 512 GB NVMe
16 TB

How to Host a Discord Bot on Ubuntu VPS

To host a Discord bot on Ubuntu VPS, follow the steps below:

  1. Connect to Your Ubuntu VPS

    Use SSH to connect to the server from your local computer.

    ssh root@YOUR_SERVER_IP
    

    Example:

    ssh root@192.168.1.10
    

    If this is your first login, confirm the fingerprint by typing:

    yes
    
  2. Update the Server

    Always update package lists and installed packages first.

    apt update && apt upgrade -y
    

    Optional but recommended:

    apt install curl wget git unzip -y
    
  3. Create a Non-Root User (Recommended)

    Running applications as root is unsafe.

    Create a new user:

    adduser discordbot
    

    Add the user to the sudo group:

    usermod -aG sudo discordbot
    

    Switch to the new user:

    su - discordbot
    
  4. Install Node.js or Python

    Choose the section based on your bot language.

    1. Install Node.js (Discord.js Bots)

      Install Node.js LTS:

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

      Verify installation:

      node -v
      npm -v
      
    2. Install Python (discord.py Bots)

      Install Python and pip:

      sudo apt install python3 python3-pip python3-venv -y
      

      Verify installation:

      python3 --version
      pip3 --version
      
  5. Upload Your Discord Bot Files

    There are several ways to upload your bot.

    1. Git Clone (Recommended)

      If your bot is on GitHub:

      git clone https://github.com/USERNAME/BOT-REPO.git
      

      Enter the folder:

      cd BOT-REPO
      
    2. SCP Upload

      From your local computer:

      scp -r ./mybot discordbot@YOUR_SERVER_IP:/home/discordbot/
      
    3. SFTP Client

      Use an SFTP client like:

      Upload files into:

      /home/discordbot/
      
  6. Install Dependencies

    • Node.js Bots

      Inside your bot directory:

      npm install
      
    • Python Bots

      Create a virtual environment:

      python3 -m venv venv
      

      Activate it:

      source venv/bin/activate
      

      Install dependencies:

      pip install -r requirements.txt
      
  7. Configure Environment Variables

    Never hardcode your Discord token directly into source code.

    Create a .env file:

    nano .env
    

    Example:

    DISCORD_TOKEN=your_bot_token_here
    

    Save and exit:

    • CTRL + X
    • Y
    • ENTER
  8. Test the Bot

    • Node.js

      node index.js
      

      Or:

      npm start
      
    • Python

      python3 bot.py
      

      If everything is correct, your bot should appear online in Discord.

      Stop the bot with:

      CTRL + C
      
  9. Keep the Bot Running 24/7

    If you close the terminal, the bot will stop unless you use a process manager.

    The best options are:

    1. Using PM2 (Recommended for Beginners)

      Install PM2 globally:

      sudo npm install -g pm2
      

      Start a Node.js Bot

      pm2 start index.js --name discord-bot
      

      Start a Python Bot

      pm2 start bot.py --interpreter python3 --name discord-bot
      

      View Running Processes

      pm2 list
      

      View Logs

      pm2 logs discord-bot
      

      Restart Bot

      pm2 restart discord-bot
      

      Stop Bot

      pm2 stop discord-bot
      

      Enable Auto-Start on Reboot

      pm2 startup
      

      Copy and run the command PM2 provides.

      Then save processes:

      pm2 save
      

      Your bot will now automatically restart after VPS reboots.

    2. Using systemd (Advanced)

      Create a service file:

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

      Example service configuration:

      [Unit]
      Description=Discord Bot
      After=network.target
      
      [Service]
      Type=simple
      User=discordbot
      WorkingDirectory=/home/discordbot/mybot
      ExecStart=/usr/bin/node index.js
      Restart=always
      
      [Install]
      WantedBy=multi-user.target
      

      Reload systemd:

      sudo systemctl daemon-reload
      

      Enable service:

      sudo systemctl enable discordbot
      

      Start service:

      sudo systemctl start discordbot
      

      Check status:

      sudo systemctl status discordbot
      
  10. Secure Your VPS

    Security is important when hosting public bots.

    Enable Firewall

    Install UFW:

    sudo apt install ufw -y
    

    Allow SSH:

    sudo ufw allow OpenSSH
    

    Enable firewall:

    sudo ufw enable
    

    Check status:

    sudo ufw status
    

    Disable Root SSH Login

    Edit SSH configuration:

    sudo nano /etc/ssh/sshd_config
    

    Find:

    PermitRootLogin yes
    

    Change to:

    PermitRootLogin no
    

    Restart SSH:

    sudo systemctl restart ssh
    

    Use SSH Keys Instead of Passwords

    Generate SSH keys locally:

    ssh-keygen
    

    Copy key to server:

    ssh-copy-id discordbot@YOUR_SERVER_IP
    
  11. Optional Enhancements

    Install Fail2Ban

    Protect against brute-force attacks:

    sudo apt install fail2ban -y
    

    Enable and start:

    sudo systemctl enable fail2ban
    sudo systemctl start fail2ban
    

    Monitor Resource Usage

    Install htop:

    sudo apt install htop -y
    

    Run:

    htop
    

    Auto-Deploy with GitHub

    You can automate deployments using:

    • GitHub Actions
    • Webhooks
    • CI/CD pipelines

    Example deployment command:

    git pull origin main
    npm install
    pm2 restart discord-bot
    

Troubleshooting

Bot Offline

Check logs:

pm2 logs

Or:

journalctl -u discordbot -f

Permission Errors

Fix ownership:

sudo chown -R discordbot:discordbot /home/discordbot/

Missing Dependencies

Reinstall dependencies:

npm install

Or:

pip install -r requirements.txt

Port Already in Use

Find process:

sudo lsof -i :PORT

Kill process:

kill -9 PID

Final Thoughts

Hosting your Discord bot on an Ubuntu VPS gives you:

  • 24/7 uptime
  • Better performance
  • Full control
  • Improved reliability
  • Easier scaling

For most users, the ideal setup is:

  • Ubuntu 22.04
  • Node.js LTS or Python 3
  • PM2 process manager
  • UFW firewall
  • SSH key authentication

Once configured properly, your Discord bot can run continuously with minimal maintenance.
Launch 100% ssd ubuntu vps from $3. 19/mo!

Conclusion

You now know how to host a Discord bot on Ubuntu VPS.

Happy bot hosting!

Avatar of editorial staff

Editorial Staff

Rad Web Hosting is a leading provider of web hosting, Cloud VPS, and Dedicated Servers in Dallas, TX.
lg