Prerequisites for Lavalink v4

Lavalink v4 brings major performance improvements and a modern modular plugin architecture. To host Lavalink, you will need:

Step 1: Install OpenJDK 21

sudo apt update
sudo apt install -y openjdk-21-jre-headless

Verify your installation:

java -version

Step 2: Download Lavalink.jar and Configure application.yml

mkdir -p ~/lavalink && cd ~/lavalink
curl -Lo Lavalink.jar https://github.com/lavalink-devs/Lavalink/releases/latest/download/Lavalink.jar

Create the application.yml configuration file:

server:
  port: 2333
  address: 0.0.0.0

lavalink:
  plugins:
    - dependency: "dev.lavalink.youtube:youtube-plugin:1.11.4"
      repository: "https://maven.lavalink.dev/releases"
  server:
    password: "YourSecureLavalinkPassword"
    playerUpdateInterval: 5
    sources:
      youtube: false # Handled via YouTube plugin in v4
      bandcamp: true
      soundcloud: true
      twitch: true
      vimeo: true
      http: true
      local: false
    filters:
      volume: true
      equalizer: true
      karaoke: true
      timescale: true
      tremolo: true
      vibrato: true
      distortion: true
      rotation: true
      channelMix: true
      lowPass: true
    bufferDurationMs: 400
    frameBufferDurationMs: 5000
    opusEncodingQuality: 10
    resamplingQuality: HIGH
    trackStuckThresholdMs: 10000
    useSeekGhosting: true
    youtubeSearchEnabled: true

metrics:
  prometheus:
    enabled: false

logging:
  file:
    path: ./logs/lavalink.log
  level:
    root: INFO
    lavalink: INFO

Step 3: Run Lavalink with Systemd

Create a persistent system service at /etc/systemd/system/lavalink.service:

[Unit]
Description=Lavalink Audio Node
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/lavalink
ExecStart=/usr/bin/java -Xmx1024M -Xms256M -jar Lavalink.jar
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable --now lavalink

Step 4: Connecting from Your Discord.js Bot

Use client libraries like Lavalink-Client, Poru, or Kazagumo in your bot:

const { LavalinkManager } = require("lavalink-client");

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] });

client.lavalink = new LavalinkManager({
  nodes: [
    {
      authorization: "YourSecureLavalinkPassword",
      host: "127.0.0.1",
      port: 2333,
      secure: false,
    },
  ],
  sendToShard: (guildId, payload) => client.guilds.cache.get(guildId)?.shard?.send(payload),
  client: {
    id: "YOUR_BOT_CLIENT_ID",
    username: "VexaMusicBot",
  },
});