Blog
Tutorial

How to host a Discord bot on a VPS with Ubuntu

Set up a Discord bot on Ubuntu with Node.js or Python, systemd, token protections, firewall rules, and a method for measuring resource use.

To host a Discord bot on Ubuntu, rent a KVM VPS, install the bot's runtime and dependencies, keep its token out of the source code, then run it as a systemd service.

The process is yours to maintain: patch the server, manage access, read logs, and fix the bot.

Run the bot on a VPS you control

This walkthrough covers a clean Ubuntu setup for Node.js or Python, a restricted service account, token handling, firewall access, and systemd. Ekire rents KVM virtual servers with full hardware virtualization rather than containers. You control the operating system and process; you also handle its upkeep.

  • Install security updates and the runtime your bot requires.
  • Keep the bot's source, dependencies, and secret configuration in their own locations.
  • Use SSH keys, restrict inbound access, and inspect logs when the process exits.

Choose a VPS size from the bot's workload

There is no single VPS size for every bot: libraries, event volume, caching, and any database or other processes beside the bot all affect resource use. Measure the bot under its expected workload, then account for Ubuntu and co-hosted processes.

  1. Run the bot with representative activity and observe its CPU and memory use.
  2. Include the operating system and any database or additional bot in your measurements.
  3. Compare your measured needs with the server details on Compute. See Pricing for billing details.

Prepare Ubuntu and deploy either runtime

Start with a clean Ubuntu server and connect over SSH. The commands below create a non-root service account and a directory for the bot; put your source code in that directory using your chosen deployment method.

  1. Update Ubuntu and create the service account

    Run: sudo apt update && sudo apt upgrade -y sudo adduser --system --group --home /srv/discord-bot discordbot sudo install -d -o discordbot -g discordbot /srv/discord-bot/app

  2. Install Node.js and the bot's packages

    Install the Node.js version required by your bot. Ubuntu's package version can differ from the version a project supports, so check the official Node.js installation guidance (opens in a new tab) before choosing an installation method. With Node.js and npm installed, place the project files in /srv/discord-bot/app, then run: cd /srv/discord-bot/app sudo -u discordbot npm ci --omit=devFor a bot whose entry point is index.js, test it manually before creating a service: sudo -u discordbot bash -c 'set -a; . /etc/discord-bot.env; set +a; cd /srv/discord-bot/app; exec node index.js'

  3. Install Python dependencies in a virtual environment

    Install Python and its virtual-environment support, then create the environment as the service account. The Python venv documentation (opens in a new tab) explains how the environment isolates installed packages. sudo apt install python3 python3-venv python3-pip git clone YOUR_REPOSITORY_URL /srv/discord-bot/app sudo chown -R discordbot:discordbot /srv/discord-bot/app sudo -u discordbot python3 -m venv /srv/discord-bot/app/.venv sudo -u discordbot /srv/discord-bot/app/.venv/bin/pip install -r /srv/discord-bot/app/requirements.txtReplace the repository URL with your source location. For a bot whose entry point is bot.py, test it manually: sudo -u discordbot bash -c 'set -a; . /etc/discord-bot.env; set +a; cd /srv/discord-bot/app; exec .venv/bin/python bot.py'

Keep the token out of source control

Never hard-code the Discord token or commit it to a repository. This example uses an environment file, but that file is only one access control: anyone who can read it can use the token, so limit access to root and the bot's service account.

  1. Create a restricted environment file

    Create the file so root owns it and the bot account's group can read it, then edit it without putting the token in a shell command: sudo install -o root -g discordbot -m 0640 /dev/null /etc/discord-bot.env sudoedit /etc/discord-bot.envAdd a line in the form DISCORD_TOKEN=your_token_value. Keep the file out of the source directory.

  2. Set up SSH keys and the server firewall

    Save an SSH public key to your account and use it to access the server; follow the SSH key documentation (opens in a new tab). Ekire's server firewall denies inbound traffic by default, including SSH, until you add an allow rule. Allow your SSH access before closing your current session, and see the firewall documentation (opens in a new tab) for the control panel steps.

  3. Rotate an exposed token

    If the token reaches a repository, log, or other place you do not control, reset it through Discord's bot settings, replace the environment-file value, then restart the service. Removing the exposed copy does not invalidate it.

Run the bot with systemd and check its limits

Create one unit file for the runtime you use. Both examples load the same environment file, run under the restricted account, and restart after an unsuccessful exit. The systemd service documentation (opens in a new tab) describes the unit settings.

Node.js unit

Save as /etc/systemd/system/discord-bot.service: [Unit] Description=Discord bot After=network.target [Service] Type=simple User=discordbot Group=discordbot WorkingDirectory=/srv/discord-bot/app EnvironmentFile=/etc/discord-bot.env ExecStart=/usr/bin/node /srv/discord-bot/app/index.js Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.targetCheck the Node.js path with command -v node and use the path it returns in ExecStart.

Python unit

Use the same unit path and settings, replacing ExecStart with the virtual-environment interpreter and your entry point: ExecStart=/srv/discord-bot/app/.venv/bin/python /srv/discord-bot/app/bot.py

  1. Enable the service at boot

    Reload systemd and enable and start the unit: sudo systemctl daemon-reload sudo systemctl enable --now discord-bot

  2. Inspect status and logs

    Check the current service state and read recent logs. Follow new log entries while investigating an exit: sudo systemctl status discord-bot sudo journalctl -u discord-bot -n 100 --no-pager sudo journalctl -u discord-bot -f

systemd can restart a process, but it cannot prevent a host outage or repair a bug that makes the bot exit repeatedly. Ekire's service commitments are described on the SLA page. A process restart is not a host-availability guarantee.

A server never costs more than its monthly price in a billing month; destroying a server stops its billing. A stopped server keeps billing, and destroying it ends the meter. See Pricing for billing details.

Set firewall rules and plan for multiple bots

A Discord bot generally needs outbound connectivity to Discord, while inbound access should be limited to SSH and any service you have explicitly chosen to expose. Discord's Gateway documentation (opens in a new tab) describes the bot connection. Do not open inbound ports just because a bot runs on a server.

  • Allow inbound SSH from the addresses and port you use to administer the server.
  • Leave other inbound access closed unless the bot needs a specific listener or callback.
  • For each additional bot, use its own service account, directory, environment file, and systemd unit.
  • Measure the combined resource use of all bots and co-hosted processes before choosing a server.

A server never costs more than its monthly price in a billing month; destroying a server stops its billing. Check Pricing for billing details.

Frequently asked questions

How do I host a Discord bot on a VPS?

Install the bot's runtime and packages on Ubuntu, keep its token in a restricted environment file or secret store, then run it as a systemd service under a non-root account.

How does billing work?

A server never costs more than its monthly price in a billing month; destroying a server stops its billing. See Pricing for the billing details.

What VPS specs do I need for a Discord bot?

There is no universal size. Measure CPU and memory use under the workload you expect, include the operating system and other processes, then compare with the server details on Compute.

How do I keep my Discord bot running?

Use a systemd unit with a restart policy and enable it at boot. That can restart an exited process, but it cannot fix a repeating bug or prevent a host outage; see Ekire's SLA for service commitments.

Can I host multiple Discord bots on one VPS?

Yes, if the server has capacity for their combined workload. Give each bot a separate service account, directory, environment file, and systemd unit, then measure resource use with them running together.

Compare server detailsSee pricingCheck server details and billing information before deployment.