How to Host a Discord Music Bot on a VPS
Run your own Discord music bot on a VPS: install ffmpeg and the voice dependencies, stream audio reliably, and keep it online 24/7 with PM2.

Music bots are the most demanding Discord bots to self-host: they need audio encoding, extra native libraries, and more CPU and bandwidth than a typical command bot. This guide gets a music bot running reliably on a VPS. For the general basics first, see how to host a Discord bot on a VPS 24/7.
What makes a music bot different
- ffmpeg transcodes audio streams — it's mandatory.
- Opus and encryption libraries (
@discordjs/opusoropusscript, pluslibsodium/tweetnacl) handle Discord voice. - A source extractor like
yt-dlppulls audio from links. - More resources — encoding audio uses real CPU, and streaming uses steady bandwidth.
Because of the licensing and reliability headaches of scraping streaming sites, many people run bots that play from direct audio URLs, self-hosted files, or platforms that explicitly allow it. Respect each source's terms of service.
Step 1: Install system dependencies
sudo apt update
sudo apt install -y ffmpeg python3 build-essential git
ffmpeg -version
For a Node.js bot, install Node too:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
Install yt-dlp if your bot uses it:
sudo curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -o /usr/local/bin/yt-dlp
sudo chmod a+rx /usr/local/bin/yt-dlp

Step 2: Install the bot's voice libraries
For a discord.js music bot:
cd ~/music-bot
npm install discord.js @discordjs/voice @discordjs/opus libsodium-wrappers ffmpeg-static play-dl dotenv
@discordjs/voice is the voice engine, @discordjs/opus encodes audio, and libsodium-wrappers handles the required encryption. ffmpeg-static bundles ffmpeg if you'd rather not rely on the system copy.
Step 3: Store the token and start
Keep the token in .env and start the bot to test playback in a voice channel:
echo "DISCORD_TOKEN=your-bot-token-here" > .env
chmod 600 .env
node index.js
Your bot needs the Voice permissions (Connect, Speak) in the server, and if it reads commands from message text, the Message Content intent.
Step 4: Keep it online with PM2
Music bots crash more than most (network hiccups, stream errors), so auto-restart matters:
sudo npm install -g pm2
pm2 start index.js --name music-bot --max-memory-restart 500M
pm2 save
pm2 startup
The full PM2 workflow is in how to keep a Discord bot online with PM2. A --max-memory-restart guard is especially useful here, since audio buffers can leak.
Step 5: Size the server
Audio encoding is CPU work, and every listener adds outbound bandwidth. A single-guild bot runs on 1–2 vCPU and 1 GB RAM; a bot serving many servers simultaneously wants more cores and headroom. Read Game Server Specs Explained: CPU, RAM and Bandwidth — the same CPU-and-bandwidth logic applies to audio streaming.
Troubleshooting audio
- No sound: confirm ffmpeg is installed and on PATH (
ffmpeg -version), and that the encryption library loaded. - Choppy playback: usually CPU starvation or a slow source — check
pm2 monitand your network. - Bot joins but leaves instantly: missing Connect/Speak permission, or an unhandled voice-connection error.
Prefer a container? See how to deploy a Discord bot with Docker — just remember to install ffmpeg in the image.
Reducing lag and choppy audio
Choppy playback almost always traces to CPU starvation or a slow source. To keep audio smooth:
- Give the bot real CPU — audio encoding is continuous work, especially across multiple guilds.
- Prefer sources that stream reliably, and buffer ahead where your library allows.
- Cap concurrent streams if one small VPS serves many servers at once.
Building a track queue
Most music bots implement a per-guild queue so users can stack tracks. Conceptually:
- A
playcommand resolves a source URL and pushes it onto the guild's queue. - An audio player plays the head of the queue and fires an "idle" event when a track ends.
- On idle, shift the queue and play the next track, or leave the channel after a timeout.
@discordjs/voice exposes exactly these player events, so the logic stays small.
Troubleshooting
- Bot joins but is silent: ffmpeg is missing or not on PATH, or the encryption library failed to load.
- Instant disconnect: missing Connect/Speak permission, or an unhandled voice-connection error — add error handlers.
- Memory creeps up: audio buffers can leak; run under PM2 with
--max-memory-restart.
FAQ
Why does my music bot need ffmpeg?
ffmpeg transcodes the source audio into the Opus format Discord voice requires. Without it, the bot connects but plays nothing.
How much CPU does a music bot use?
Audio encoding is real CPU work. One stream is light, but a bot playing in many servers at once can saturate a small VPS — scale cores accordingly.
Is it legal to stream from YouTube?
Scraping some streaming platforms violates their terms of service. Play from sources that permit it (direct URLs, self-hosted files, licensed platforms) and check each source's terms.
Why does the bot keep crashing?
Music bots hit network and stream errors often. Run under PM2 with auto-restart and a memory limit, and add error handling around your voice connection.
Give your music bot the CPU and bandwidth it needs on Discord bot hosting from Nxeon — NVMe VPS, full root access, and generous bandwidth for smooth streaming.