How to Install Mosquitto Broker on Raspberry Pi

This guide explains how to install the Mosquitto Broker for MQTT communication on a Raspberry Pi board.

How to Install Mosquitto Broker on Raspberry Pi

The broker is primarily responsible for receiving all messages, filtering the messages, decide who is interested in it and then publishing the message to all subscribed clients.

mqtt-broker

There are several brokers you can use. In our Home Automation projects we use the Mosquitto Broker installed on a Raspberry Pi.

Prerequisites

Before continuing with this tutorial

If you like home automation and you want to build a complete home automation system, I recommend downloading my home automation course.

After having your Raspberry Pi board prepared with Raspbian OS, you can continue with this tutorial. Let’s install the Mosquitto Broker.

Installing Mosquitto Broker on Raspbian OS

Open a new Raspberry Pi terminal window:

To install the Mosquitto Broker enter these next commands:

[email protected]:~ $ sudo apt update
[email protected]:~ $ sudo apt install -y mosquitto mosquitto-clients

You’ll have to type Y and press Enter to confirm the installation. To make Mosquitto auto start on boot up enter:

[email protected]:~ $ sudo systemctl enable mosquitto.service

Testing Installation

Send the command:

[email protected]:~ $ mosquitto -v

This returns the Mosquitto version that is currently running in your Raspberry Pi. It should be 1.4.X or above.

Note: sometimes the command mosquitto -v prompts a warning message saying “Error: Address already in use“. That warning message means that your Mosquitto Broker is already running, so don’t worry about that.

Raspberry Pi IP Address

To use Mosquitto broker later on your projects, you’ll need your Raspberry Pi IP address. To retrieve your Raspberry Pi IP address, type the next command in your Terminal window:

[email protected]:~ $ hostname -I

In our case, the Raspberry Pi IP address is 192.168.1.144. Save your Raspberry Pi IP address because you’ll need it in future projects.

Testing Mosquitto Broker and MQTT Client

After installing the Mosquitto broker, you should test you installation. You can follow the next tutorial:

Wrapping up

Like home automation? Learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino with my course: Build a Home Automation System for $100.

Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

Updated June 4, 2019



Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

18 thoughts on “How to Install Mosquitto Broker on Raspberry Pi”

  1. One of the best and clear tutorial I ever seen.
    I will be very much appreciated if you upgrade this tutorial to setup Mosquito server running on Raspberry Pi with TLS security connection and CA key(port 8883).
    Everything else is compromise. Specially when the raspberry is open to the Wide WEB.
    There is a lot post about that, but I did`t find good enough to work out of the box.
    Thank you and keep going
    Rosen

    Reply
  2. Hi Rui, I’m getting “1494513510: Error: Address already in use” message al of a sudden, it was all running ok, any help on what’s happening….thank you!

    Reply
  3. Hi Rui,

    Thanks. I found my mistake, it was the mqtt server ip that was not updated right in esp8266 client side code. Thanks for your tutorial (got some time for myself to follow your home automation course book).

    Reply
  4. This doesn’t seem to work with Stretch. Is there an update to this instruction set? “The following packages have unmet dependencies: . . .”

    Reply
    • These instructions work.
      I followed this installation procedure three weeks ago and it worked on a fresh installation with the latest Raspbian lite (Stretch).
      Did you upgrade your OS or followed someone else’s instructions before?

      You might have the Mosquitto software repositories outdated, which results in that exact problem. (You need to remove the repositories)
      Starting with a new installation and only following these instructions should install Mosquitto successfully.
      I hope this helps.

      Reply
  5. I greet you dear “program” friends and thank you for your effort to help. This “introductory” project worked with “RPi3B” and with “Buster” without any problems.
    But I have another problem. The problem with the initiating WiFi on RasPi3B + V. 2017 and Buster. I have 2pcs RPi3B with the installed Buster, WiFi without problems, RPi4B 4GB also without problems, just “with heating”. And in RPi3B +, we don’t even define our country “Slovakia”, but no other. I have already passed the whole internet and tried everything possible.

    Reply
  6. Thank you for your interest. Everything is working. “Mea culpa”. Mosquitto worked on the apparently, perfect.

    Reply
  7. On page 430, what’s the voltage on the breadboard for the DS18B20? The graphic doesn’t show the GND or V+ connection. I’m assuming 5v, since that’s usually what it takes to drive the sensor.

    Reply
  8. Hi Sara, on a Raspberry 4 with 4gb when entering the command ‘sudo systemctl enable mosquitto.service’ when pressing Enter it says’ Failed to enable unit: Unit file mosquitto.service does not exist. What could be the problem? Thank you

    Reply

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.