---
title: Run a Discord.js Bot on Ubuntu 20.04
url: https://docs.vultr.com/run-a-discord-js-bot-on-ubuntu-20-04
description: Learn how to set up and run a Discord.js bot on Ubuntu 20.04 with step-by-step instructions for installation, configuration, and deployment for 24/7 operation.
publish_date: 2021-01-28T00:00:00Z
last_updated: 2026-04-15T00:00:00.077250Z
header_image: https://docs.vultr.com/public/doc-assets/832/f9d3ab92d61d11d7.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

A Discord bot can execute commands and perform advanced Discord functions. This tutorial explains how to run a Discord.js Bot on Ubuntu by creating a simple bot with Discord.js on a Vultr Ubuntu 20.04 server.

## Create a Discord Application

A Discord bot needs an application to use as a Discord account.

1. Go to the [Discord Developer Portal](https://discord.com/developers/applications) and click the **New Application** button.

    ![Discord Token](https://docs.vultr.com/public/doc-assets/832/57555375d58d3455.png)

1. Give your Application a name and click **Create**.
1. Add an icon and description if desired.
1. Click **Bot** in the Settings menu.
1. Click the **Add Bot** button.
1. Click the **Yes, do it!** button.
1. Click the link titled "Click to Reveal Token".
1. Make a note of this token.

## Install Vultr Server

1. Deploy a new [Vultr Ubuntu instance](https://console.vultr.com/). Use the following settings:

   * Server Type: Cloud Compute Server
   * Server Location: The location closest to you, or closest to the majority of the bot's users.
   * Server OS: Ubuntu 20.04, 64-bit.
   * Server Size: Choose a size according to your expected user volume. You can start with a small instance and upgrade later with the **Change Plan** option in the server control panel. You do not need to reinstall when upgrading plans.

1. Click the **View Console** icon in the top right corner, or SSH to the server.
1. Log in as root using the password on the server information page.
1. Update the apt database.

        # sudo apt-get update

## Install Dependencies

1. Install Node, which is required for Discord.js

        # sudo apt-get install nodejs

1. Install the Node Package Manager, which is used to install the Discord.js library.

        # sudo apt-get install npm

1. Install Node Version Manager (nvm), which is used to update Node.js. For example:

        # curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

    **Note:** Use the command for the latest version, which is found here: <https://github.com/nvm-sh/nvm#installing-and-updating>

1. Reboot the server.

1. Reconnect to the server console.
1. Verify the nvm installation:

        # command -v nvm

    It should return:

        nvm

    If it returns nothing, or an error, run the following:

        # export NVM_DIR="$HOME/.nvm"
        # [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
        # [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

1. After you've installed nvm, update Node.js to the latest version:

        # nvm install node
        # nvm use node

## Create the Project

1. Switch to the root user's home directory.

        # cd /root

1. Create a project folder.

        # mkdir tutorial-bot

1. Switch to the folder.

        # cd tutorial-bot

1. Initialize your Node.js project.

        # npm init

1. Answer the questions. Leave the defaults unless you need to change them.
1. Check your `package.json` file.

        # cat package.json

    It should look like:

        {
          "name": "tutorial-bot",
          "version": "1.0.0",
          "description": "Tutorial Bot",
          "main": "index.js",
          "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1"
          },
          "author": "",
          "license": "ISC"
        }

## Install Libraries

Libraries make writing code easier. For this guide, you need the Discord.js library. As you add capabilities to your bot, look for other useful libraries on the [npm website](https://www.npmjs.com/).

Install the Discord.js library.

    # npm install discord.js --save

The `--save` flag adds the library as a dependency in your `package.json` file.


## Write the Bot Code

1. Create an `index.js` file in the project folder.

        # nano index.js

1. Add the following to `index.js`, which creates a new Discord client.

        const Discord = require('discord.js');
        const client = new Discord.Client();

1. Add a code block to watch for `!hello` and respond with `Hello World!`.

        client.on('message', message => {
            if (message.content === '!hello') {
                message.channel.send('Hello World!');
            }
        });

1. Add a code block to log `Ready!` in the console when client connects to Discord.

        client.once('ready', () => {
           console.log('Ready!');
         }

1. Add your Discord Application token, which allows the bot access.

        client.login('YOUR-TOKEN-HERE')

Your finished file should look like this:

    const Discord = require('discord.js');
    const client = new Discord.Client();

    client.on('message', message => {
        if (message.content === '!hello') {
            message.channel.send('Hello World!');
        }
    })

    client.once('ready', () => {
        console.log('Ready!');
    })

    client.login('YOUR-TOKEN-HERE')

## Install a Node.js Process Manager

1. Install the PM2 process manager to manage your bot.

        # npm install pm2 -g

1. Navigate to the project folder.

        # cd /root/tutorial-bot

1. Start the bot with pm2.

        # pm2 start index.js

1. Verify the bot is online.

        # pm2 logs

    You should see:

        0|index | Ready!

    To exit the log, press: :key_control: + :key_c:

## Invite the Bot to a Server

1. Go to the [Discord Developer Portal](https://discord.com/developers/applications).
1. Navigate to the application's **OAuth2** tab
1. In the **Scopes** section, select **bot**.
1. In the **Bot Permissions** section, select **Administrator**. Note: This can create invite links that only give the bot certain permissions.

    ![OAuth2](https://docs.vultr.com/public/doc-assets/832/1be76f52317f570f.png)

1. Open the invite link in a new tab.
1. Select the server to add the bot to.
1. Approve the permissions and complete the CAPTCHA.

    The bot is now active and able to respond to your command.

    ![Bot Joined](https://docs.vultr.com/public/doc-assets/832/3b9c0bbb3b5873b8.png)

    ![Hello World](https://docs.vultr.com/public/doc-assets/832/7b641c9eed67821e.png)

## Next Steps

After you develop your bot further, go to your server's project folder restart the bot.

    # pm2 restart index.js

The [Discord.js website](https://discord.js.org) has more documentation needed to develop advanced JavaScript bots.
