Menu Close

Mosquitto MQTT Broker Unix installation guide

Mosquitto MQTT Installation

This post may contain affiliate links.  If you make a purchase I may get a commission at no cost to you.  Please see the Disclaimer for additional information.

Mosquitto is a MQTT Broker that can be installed in any type of system environment.
Yes, you can have your own Mosquitto MQTT installation on your system!
Also, it’s an Open source Eclipse project that implement MQTT protocol 3.1, 3.1.1 and 5.0, which should cover all of your MQTT needs.

Additionally, If you want to know more about MQTT in general and its application in home automation, just follow this link: MQTT basics for a smart home.

There are several ways to install Mosquitto onto your Raspberry pi. However, the easiest is to use either the combination keyboard mouse screen to access the Terminal or to SSH, using your favorite Putty or MobaXterm, and to run some easy commands.
Don’t forget to log in has root or you need to use sudo in your command.
And, you will need to install an MQTT Client (Pub/Sub) if you want to use / test it.

Mosquitto MQTT Broker Installation

Mosquitto Repository

This MQTT Broker is availlable through an apt-get repository, which makes the installation much easier. As such, the first step is to import the repository package signing key and add it to your apt keys:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key

Once the key is imported, you need to make the repository availlable to apt by going into the source directory:

cd /etc/apt/sources.list.d/

Now that you are in the directory, you can add the repository based on your installed version of debian:

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-buster.list

And as always, you should update apt information to discover the mosquitto package

apt-get update apt-cache search mosquitto

Install Mosquitto on your rapsberry pi

Now that you have set the apt repository, installing the MQTT Broker is quite easy

apt-get install mosquitto

Tadam! You are done with installing Mosquitto!

MQTT Client Installation

Once your Raspberry pi finishes Mosquitto’s installation, you can install Mosquitto MQTT client. Since the repository is already added to apt, installing the client is as simple as:

apt-get install mosquitto-clients

Testing your MQTT environment

Now that you have installed both Mosquitto Broker and client, you can test the environnment by creating a topic, subscribing to it and writting messages to it. First, you need to have 2 SSH windows open, be it Putty or MobaXterm, connected to your Raspberry pi.

In the first window, you can have your client subscribe to a topic, with any name you want, using the command:

mosquitto_sub -d -t test_mqtt

In the second window, you can publish to that topic with your client:

mosquitto_pub -d -t test_mqtt -m "Hello World"

Going back to your first window, you should see the “Hello World” message.
You have successfully tested your Mosquitto installation.

Enjoyed this post?

Follow us on Pinterest for additional awesome ideas!​

Some posts on this blog contain affiliate links.
At no additional charge to you, I receive a small commission whenever a product is purchased through these links. The Dreaming Dad is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com.