
SpamAssassin is an open-source mail filter that identifies spam emails using a wide range of heuristic tests. This guide explains how to install SpamAssassin on Ubuntu 20.04 LTS or 21.04.
Prerequisites
- Deploy an Ubuntu cloud server instance at Vultr.
- Install Postfix mail transfer agent.
- Create a sudo user.
- Verify the server has outbound port 25 open.
1. Install SpamAssassin
- Connect to your server with SSH. 
- Install SpamAssassin. - $ sudo apt-get update $ sudo apt-get install spamassassin spamc -y
- Add a SpamAssassin user and disable the login. - $ sudo adduser spamd --disabled-login
2. Configure SpamAssassin
Assign the spamd user account to SpamAssassin and set the log and default home directories.
- Edit the configuration settings. - $ sudo nano /etc/default/spamassassin
- Find the line: - ENABLED=1- Uncomment it by removing - #and change the value from- 1to- 0.- ENABLED=0
- Find the line: - OPTIONS="--create-prefs --max-children 5 --helper-home-dir"- Change it to include the SpamAssassin user account and log files directory. - OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir /home/spamd/ -s /home/spamd/spamd.log"
- Find the line: - CRON=0- Change the value from - 0to- 1.- CRON=1
- Save and close the file. 
- Make a backup of the SpamAssassin local configuration file. - $ sudo mv /etc/spamassassin/local.cf /etc/spamassassin/local.cf.bk
- Create a new SpamAssassin local configuration file. - $ sudo nano /etc/spamassassin/local.cf
- Paste the information below to the file. - rewrite_header Subject ***** SPAM _SCORE_ ***** report_safe 0 required_score 5.0 use_bayes 1 use_bayes_rules 1 bayes_auto_learn 1 skip_rbl_checks 0 use_razor2 0 use_dcc 0 use_pyzor 0 ifplugin Mail::SpamAssassin::Plugin::Shortcircuit endif
- Save and close the file. 
3. Configure Postfix
- Edit your Postfix configuration file. - $ sudo nano /etc/postfix/master.cf
- Locate these entries. - smtp inet n - y - - smtpd #smtp inet n - y - 1 postscreen #smtpd pass - - y - - smtpd #dnsblog unix - - y - 0 dnsblog #tlsproxy unix - - y - 0 tlsproxy submission inet n - y - - smtpd
- Below the - smtpconfiguration, add a SpamAssassin content filter.- smtp inet n - y - - smtpd -o content_filter=spamassassin spamassassin unix - n n - - pipe user=spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
- Save and close the file. 
- Restart Postfix and enable SpamAssassin to run at system startup. - $ sudo systemctl restart postfix.service $ sudo systemctl enable spamassassin.service $ sudo systemctl start spamassassin.service
More Information
To learn more about SpamAssassin, please see the official website.