October 21, 2021
How to install MQTT on Ubuntu 20.04 LTS

How To Install Mosquitto MQTT On Linux Ubuntu 20.04 LTS

On this article we will learn how to install the latest version of Mosquitto MQTT (version 2.0.11) on Linux Ubuntu 20.04 LTS operating system.

Introduction

The Message Queuing Telemetry Transport (MQTT) is a lightweight machine-to-machine messaging protocol, designed to provide lightweight publish/subscribe communication between Internet of Things devices. The protocol usually runs over TCP/IP. It is designed for connections with remote locations where the network bandwidth is limited. Mosquitto is an open source message broker (or server) that implements MQTT protocols. Mosquitto has good community support, documentation, and ease of installation it has become one of the most popular MQTT brokers. On this article we are going to install Mosquitto on Ubuntu 20.04 LTS operating system.



Mosquitto MQTT Installation on Ubuntu 20.04 LTS

We are going to install Mosquitto MQTT server by compling the source and testing it by sending sub/pub message with a simple step.

Prerequisite

  • Ubuntu 20.04 LTS Operating system with sufficient disk space
  • root or an ordinary user access who is having sudo privilege

A. Installing Mosquitto MQTT Server

1. Update system and get MQTT dependencies

In this first stage, we will update the system and install the libraries required by MQTT, we will execute some command line : sudo apt update and sudo apt -y install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc.

ramans@otodiginet:~$ sudo apt update
 [sudo] password for ramans: 
 Hit:1 http://mirror.telkomuniversity.ac.id/ubuntu focal InRelease
 Get:2 http://mirror.telkomuniversity.ac.id/ubuntu focal-updates InRelease [114 kB]                               
 Hit:3 https://deb.nodesource.com/node_14.x focal InRelease                                                       
 Hit:4 https://dl.yarnpkg.com/debian stable InRelease                                                             
 Get:5 http://mirror.telkomuniversity.ac.id/ubuntu focal-backports InRelease [101 kB]      
Update system
Update system
ramans@otodiginet:~$ sudo apt -y install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 build-essential is already the newest version (12.8ubuntu1.1).
 build-essential set to manually installed.
 The following additional packages will be installed:
   libc-ares2 libssl1.1
MQTT Depedencies library
MQTT Depedencies library

2. Add mosquitto user and grant with sudo privilege

At this stage, we will create a user with the name mosquitto which is used to execute the MQTT application. For this purpose we just submit command line : sudo adduser mosquitto and sudo usermod -aG sudo mosquitto.

ramans@otodiginet:~$ sudo adduser mosquitto
 Adding user mosquitto' ... Adding new groupmosquitto' (1001) …
 Adding new user mosquitto' (1001) with groupmosquitto' …
 Creating home directory /home/mosquitto' ... Copying files from/etc/skel' …
 New password: 
 Retype new password: 
 passwd: password updated successfully
 Changing the user information for mosquitto
 Enter the new value, or press ENTER for the default
     Full Name []: 
     Room Number []: 
     Work Phone []: 
     Home Phone []: 
     Other []: 
 Is the information correct? [Y/n] Y
Adding new user, called as mosquitto
Adding new user, called as mosquitto



ramans@otodiginet:~$ sudo usermod -aG sudo mosquitto
 ramans@otodiginet:~$ sudo su - mosquitto
 To run a command as administrator (user "root"), use "sudo ".
 See "man sudo_root" for details.
 mosquitto@otodiginet:~$ 
Granting mosquitto user with sudo privilege
Granting mosquitto user with sudo privilege

3. Download Mosquitto source code and upack it

By using user mosquitto, we will download the MQTT source file version 2.0.11 (latest) and then extract it and compile the MQTT package application. The command line that will be used are : wget https://mosquitto.org/files/source/mosquitto-2.0.11.tar.gz and tar zxvf mosquitto-2.0.11.tar.gz.

mosquitto@otodiginet:~$ wget https://mosquitto.org/files/source/mosquitto-2.0.11.tar.gz
 --2021-06-27 01:43:36--  https://mosquitto.org/files/source/mosquitto-2.0.11.tar.gz
 Resolving mosquitto.org (mosquitto.org)… 85.119.83.194, 2001:ba8:1f1:f271::2
 Connecting to mosquitto.org (mosquitto.org)|85.119.83.194|:443… connected.
 HTTP request sent, awaiting response… 200 OK
 Length: 760325 (743K) [application/octet-stream]
 Saving to: ‘mosquitto-2.0.11.tar.gz’
 mosquitto-2.0.11.tar.gz        100%[====================================================>] 742.50K   683KB/s    in 1.1s    
 2021-06-27 01:43:39 (683 KB/s) - ‘mosquitto-2.0.11.tar.gz’ saved [760325/760325]
Download MQTT Source code
Download MQTT Source code
mosquitto@otodiginet:~$ tar zxvf mosquitto-2.0.11.tar.gz 
 mosquitto-2.0.11/
 mosquitto-2.0.11/pskfile.example
 mosquitto-2.0.11/security/
 mosquitto-2.0.11/security/mosquitto.apparmor
 mosquitto-2.0.11/service/
 mosquitto-2.0.11/service/upstart/
 mosquitto-2.0.11/service/upstart/mosquitto.conf
Unpacking Mosquitto MQTT package librries
Unpacking Mosquitto MQTT package librries

4. Compiling and install MQTT server

After extracting the MQTT v2.0.11 package, we will go into the directory and then compile and install it, just hit the command line: make && sudo make install.

mosquitto@otodiginet:~/mosquitto-2.0.11$ make && sudo make install
 set -e; for d in lib apps client plugins src; do make -C ${d}; done
 make[1]: Entering directory '/home/mosquitto/mosquitto-2.0.11/lib'
 make -C cpp
 make[2]: Entering directory '/home/mosquitto/mosquitto-2.0.11/lib/cpp'
 make[2]: Nothing to be done for 'all'.
 make[2]: Leaving directory '/home/mosquitto/mosquitto-2.0.11/lib/cpp'
 make[1]: Leaving directory '/home/mosquitto/mosquitto-2.0.11/lib'
 make[1]: Entering directory '/home/mosquitto/mosquitto-2.0.11/apps'
 set -e; for d in db_dump mosquitto_ctrl mosquitto_passwd; do make -C ${d}; done
Make and install MQTT
Make and install MQTT

B. Configuring MQTT Server

1. Creating a user and setting permission

After installation was done, then we have to configure MQTT server to be operating smoothly. The first step is creating a new user. We will create a user with the name mqtt-spy, this user is a system user for running mosquitto server which will be used to make a connection from the client. On this step we will be prompted to make a new password. This step is done by submitting command line : sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy. After user was created then we have to add permissions to this mosquitto user to all relevant directories.

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy
 Password: 
 Reenter password: 
 mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo mkdir /var/lib/mosquitto/
 mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo chown -R mosquitto:mosquitto /var/lib/mosquitto/
Creating new user for MQTT
Creating new user for MQTT

2. Create configuration file for Mosquitto MQTT Server

The next step is to create a configuration file for Mosquitto MQTT Server, for this step we will be using vi editor. We will create a new file named as /etc/mosquitto/mosquitto.conf, content of this file is as follow :

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo vi /etc/mosquitto/mosquitto.conf
 persistence true
 persistence_location /var/lib/mosquitto/
 persistence_file mosquitto.db
 log_dest syslog
 log_dest stdout
 log_dest topic
 log_type error
 log_type warning
 log_type notice
 log_type information
 connection_messages true
 log_timestamp true
 allow_anonymous true
 password_file /etc/mosquitto/pwfile
 ~
mosquitto.conf file
mosquitto.conf file

then run sudo ldconfig command line.

3. Creating systemd unit file for MQTT

The next step is to create a systemd unit file for MQTT server application, for this purpose we will use vi editor. We will create a new file and named it as /etc/systemd/system/mosquitto.service.

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo vi /etc/systemd/system/mosquitto.service
 [Unit]
 Description=Insite MQTT Broker
 [Service]
 ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
 Restart=always
 [Install]
 WantedBy=multi-user.target
 ~                                                                                                             
mosquitto.service file
mosquitto.service file

4. Start and Check MQTT Service

After all set, then we will try to start MQTT Service and checking it. We will use the command line : sudo systemctl start mosquitto.service for starting and sudo systemctl start mosquitto.service for checking MQTT service.

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo systemctl start mosquitto.service
 mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo systemctl enable mosquitto.service
 Created symlink /etc/systemd/system/multi-user.target.wants/mosquitto.service → /etc/systemd/system/mosquitto.service.
 mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo systemctl status mosquitto.service
 ● mosquitto.service - Insite MQTT Broker
      Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
      Active: active (running) since Sun 2021-06-27 02:02:34 PDT; 19s ago
    Main PID: 35674 (mosquitto)
       Tasks: 1 (limit: 9448)
      Memory: 992.0K
      CGroup: /system.slice/mosquitto.service
              └─35674 /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
 Jun 27 02:02:34 otodiginet systemd[1]: Started Insite MQTT Broker.
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: mosquitto version 2.0.11 starting
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Config loaded from /etc/mosquitto/mosquitto.conf.
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Starting in local only mode. Connections will only b>
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Create a configuration file which defines a listener>
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: For more details see https://mosquitto.org/documenta>
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Opening ipv4 listen socket on port 1883.
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Opening ipv6 listen socket on port 1883.
 Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: mosquitto version 2.0.11 running
MQTT Service
MQTT Service

C. Testing

At this point we have installed and configured Mosquitto MQTT broker server as will, the next step is to test it. On this article, we have create a simple testing which is running on server’s console, where there is a server and a client to use the server service.

  1. Server
    On the server we will create a topic by using command line : mosquitto_sub -v -t 'otodiginet/test' -u mqtt-spy -P <password>
  2. Client
    Sending message with the same topic as server has. Submitting command line : mosquitto_pub -t 'otodiginet/test' -u mqtt-spy -P <password>

Mosquitto MQTT testing example
Server, client and message between them

Conclusion

Mosquitto MQTT installation was completed done. This is only an example about how to install MQTT server application and configuring it on Ubuntu 20.04 LTS operating system. I hope it should be a new step for learning IoT.

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *