Deploy a Discord Bot in 5 Minutes on LayerOne
Move your Python Discord bot off your laptop, keep its token out of source control, and let systemd restart it after a crash or reboot.
A bot running on your laptop disappears whenever the laptop sleeps, changes networks, or installs an update. Move it to a VPS when you want predictable uptime, a stable IP, and logs you can inspect from anywhere. This walkthrough uses Ubuntu 24.04, Python, and systemd.
1. Deploy the server
Choose a plan from the pricing page and deploy Ubuntu 24.04. A small text-only bot can start on Nano; bots that generate images, run local databases, or serve many guilds may need more memory. Add your SSH key during deployment when possible.
Connect over SSH, install current security updates, and add the packages the bot needs:
apt update && apt upgrade -y
apt install -y git python3 python3-venv
2. Give the bot its own account
Do not run application code as root. Create a service account and a directory for the project:
adduser --system --group --home /opt/discord-bot discordbot
mkdir -p /opt/discord-bot/app
chown -R discordbot:discordbot /opt/discord-bot
Clone your repository into /opt/discord-bot/app, or copy the files there with scp or rsync. Then create an isolated Python environment and install the project dependencies:
sudo -u discordbot python3 -m venv /opt/discord-bot/venv
sudo -u discordbot /opt/discord-bot/venv/bin/pip install -r /opt/discord-bot/app/requirements.txt
3. Keep the Discord token out of your repository
Store the token in a root-owned environment file instead of committing it to Git:
install -m 600 /dev/null /etc/discord-bot.env
nano /etc/discord-bot.env
Add a line such as DISCORD_TOKEN=replace-with-your-token. Your Python code can read it with os.environ["DISCORD_TOKEN"]. If a token has ever appeared in a public repository or log, rotate it in the Discord developer portal before continuing.
4. Run the bot with systemd
Create /etc/systemd/system/discord-bot.service and adjust bot.py if your entry point has a different name:
[Unit]
Description=Discord bot
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=discordbot
Group=discordbot
WorkingDirectory=/opt/discord-bot/app
EnvironmentFile=/etc/discord-bot.env
ExecStart=/opt/discord-bot/venv/bin/python bot.py
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Enable the service and confirm that it stays running:
systemctl daemon-reload
systemctl enable --now discord-bot
systemctl status discord-bot
journalctl -u discord-bot -n 100 --no-pager
5. Check networking and logs
Most Discord bots make outbound HTTPS and WebSocket connections and do not need a public application port. Keep only SSH open unless your bot also receives webhooks or serves a dashboard. If it does, put that service behind HTTPS and allow only the port you actually use.
Use journalctl -u discord-bot -f while testing commands. A clean startup, a successful Discord connection, and a response to a test command confirm the deployment. Set up an external check or bot-owner alert so you notice repeated restarts instead of discovering them from users.
6. Update without losing the service
Pull code as the service user, refresh dependencies, and restart only after those steps succeed:
sudo -u discordbot git -C /opt/discord-bot/app pull --ff-only
sudo -u discordbot /opt/discord-bot/venv/bin/pip install -r /opt/discord-bot/app/requirements.txt
systemctl restart discord-bot
Back up any SQLite database, uploaded files, or configuration that is not already in version control. Keep an off-server copy as well as any cluster backup.
Choose the right starting size
Start with memory: a basic command bot is light, while image processing, AI libraries, browser automation, and several bot processes need more. The Discord bot hosting guide maps common workloads to plans, and you can upgrade later from the client portal.