Sitemap

Setting Up MailCatcher as a Systemd Service on Ubuntu 20.04 with RVM

3 min readMay 7, 2025

--

In the realm of Ruby on Rails development, testing email functionality is a crucial aspect of ensuring a seamless user experience. MailCatcher serves as an invaluable tool in this regard, providing a local SMTP server to intercept and display emails sent during development.

Running MailCatcher as a systemd service offers several advantages:

  • Automatic Startup: MailCatcher launches automatically upon system boot, eliminating the need for manual initiation.
  • Persistent Operation: Operating as a background service ensures MailCatcher remains active without occupying terminal sessions.
  • Centralized Management: Systemd’s centralized service management facilitates easier monitoring and troubleshooting.

By configuring MailCatcher as a systemd service, Rails developers can streamline their development workflow, ensuring consistent and efficient email testing without manual intervention.

Install MailCatcher Gem

gem install mailcatcher

Verify MailCatcher Installation

Run MailCatcher in the foreground to ensure it’s working: ( by default it runs as a daemo).

mailcatcher --foreground --http-ip 0.0.0.0

Determine the location of the MailCatcher binary:

which mailcatcher # /home/coderly/.rvm/gems/ruby-2.6.6/bin/mailcatcher

Assumed paths:

  • Using RVM: /home/coderly/.rvm/gems/ruby-2.6.6/bin/mailcatcher
  • Using system Ruby: /usr/local/bin/mailcatcher

Create Systemd Service

With ruby version manager

I assume you are using RVM (Ruby Version Manager) for simplicity, but these steps can also be applied to other Ruby version managers with minor adjustments.

MailCatcher relies on Ruby installed via RVM, so it’s essential to ensure that the RVM environment is properly loaded when running as a systemd service.

Systemd services operate in a minimal environment and do not automatically load user-specific configurations like RVM. When you run MailCatcher manually, your shell session sources RVM’s initialization scripts (e.g., /home/coderly/.rvm/scripts/rvm), setting up the Ruby environment and PATH. However, Systemd does not execute these scripts, which can lead to MailCatcher failing to find the correct Ruby version or dependencies.

To address this, you can create a wrapper script that explicitly sources RVM before starting MailCatcher. This ensures that the necessary Ruby environment is set up for MailCatcher to run correctly.

Create a wrapper script to load the RVM environment:

sudo vi /usr/local/bin/run-mailcatcher.sh

Add the following content:

#!/bin/bash
source /home/coderly/.rvm/scripts/rvm
/home/coderly/.rvm/gems/ruby-2.6.6/bin/mailcatcher --foreground --http-ip 0.0.0.0 --http-port 1080

Make the scritp executable:

sudo chmod +x /usr/local/bin/run-mailcatcher.sh

Create the Systemd service file:

sudo vi /etc/systemd/system/mailcatcher.service

Add the following content:

[Unit]
Description=MailCatcher Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/run-mailcatcher.sh
Restart=always
User=coderly
Group=coderly

[Install]
WantedBy=multi-user.target

where:

  • coderly: as the user running the application, but these steps can also be adapted for other Ruby version managers with minor adjustments.

Ensure the service file has the correct permissions:

sudo chmod 644 /etc/systemd/system/mailcatcher.service

With native ruby

Create the Systemd service file:

sudo vi /etc/systemd/system/mailcatcher.service

Add the following content to the file:

[Unit]
Description=MailCatcher Service
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/mailcatcher --foreground --http-ip 0.0.0.0 --http-port 1080
Restart=always
User=coderly
Group=coderly

[Install]
WantedBy=multi-user.target

Ensure the service file has the correct permissions:

sudo chmod 644 /etc/systemd/system/mailcatcher.service

Enable and Start Service

Reload systemd to recognize the new service:

sudo systemctl daemon-reload

Enable the service to start on boot:

sudo systemctl enable mailcatcher.service

Start the MailCatcher service:

sudo systemctl start mailcatcher.service

Check the status of the service:

sudo systemctl status mailcatcher.service

Check the log, looking for the active (running)

journalctl -u mailcatcher.service -b

Troubleshooting

Port Conflicts: Ensure no other services are using the SMTP port (1025):

sudo netstat -tulnp | grep 1025

Verify MailCatcher Location: Confirm the path to the MailCatcher binary:

which mailcatcher

Firewall Settings: Allow necessary ports through the firewall:

sudo ufw allow 1025
sudo ufw allow 1080

Conclusion

By following these steps, MailCatcher will run as a systemd service on your Ubuntu server, starting automatically on boot.

--

--

Ly Channa
Ly Channa

Written by Ly Channa

Highly skilled: REST API, OAuth2, OpenIDConnect, SSO, TDD, RubyOnRails, CI/CD, Infrastruct as Code, AWS.

No responses yet