Install a NATS server on Debian/Ubuntu

Amjad Abujamous - Nov 4 - - Dev Community

Introduction

This tutorial teaches you how to install a NATS server directly on Debian-based linux (not via Docker).

Overview

The installation consists of multiple steps each of which will be explained in detail. Those steps are as follows:

  1. Create a user with name nats who belongs to the group nats.
  2. Download the latest NATS release build.
  3. Unzip the folder and copy it to /usr/bin.
  4. Add NATS to systemd.
  5. Run the NATS server and confirm it is working.
  6. Enable it, so it can run at startup.

Step 1: Create the user nats

In order to add a user, we will use the useradd command, like so.

sudo useradd -s /bin/bash -d /home/nats/ -m -G sudo nats
Enter fullscreen mode Exit fullscreen mode

To add a password for the user (not really needed, just a standard practice):

sudo passwd nats
Enter fullscreen mode Exit fullscreen mode

Step 2: Download NATS

As per the documentation, one can download NATS by using the following shell command.

curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@v2.10.22 | sh
Enter fullscreen mode Exit fullscreen mode

Note that you may want to change the version of NATS if there are newer releases.

Step 3: Add NATS to /usr/bin

This step is straightforward. First, open the file explorer and unzip NATS.

NATS downloaded and unzipped

Now copy the unzipped folder to /usr/bin

sudo cp nats-server/nats-server-v2.10.22-linux-amd64/nats-server /usr/bin
Enter fullscreen mode Exit fullscreen mode

Step 4: Add NATS to systemd

This one is a little tricky, but this guide simplifies it for you.

  1. Create /etc/nats-server.conf. No need to add content to it, since it is only there for customization and overriding the default behavior.

  2. Create /etc/systemd/system/nats.service and add the following content to it.

[Unit]
Description=NATS Server
After=network-online.target ntp.service

[Service]
PrivateTmp=true
Type=simple
ExecStart=/usr/bin/nats-server -c /etc/nats-server.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s SIGINT $MAINPID
User=nats
Group=nats
# The nats-server uses SIGUSR2 to trigger using Lame Duck Mode (LDM) shutdown
KillSignal=SIGUSR2
# You might want to adjust TimeoutStopSec too.

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode
  1. Save and exit.

Step 5: Run the NATS server

This one is easy. We reload the daemon and then run NATS.

sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode
sudo systemctl start nats
Enter fullscreen mode Exit fullscreen mode

To confirm that it is running, run systemctl status nats, and you should get that it is active.

nats is running

Step 6: Enable NATS

To enable NATS which would run it at startup, simply run the command below:

sudo systemctl enable nats
Enter fullscreen mode Exit fullscreen mode

Conclusion

This simple tutorial ensures that the installation of NATS on Debian-based linux is smooth and easy. Let me know below if you have any questions or inquiries about it.

Happy building!

. . . . . . . . . . . . . . .