Install Mosquitto MQTT Broker on Raspberry Pi

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

Install Mosquitto MQTT Broker on Raspberry Pi

Updated 15 December 2021

You can also run Mosquitto MQTT broker in the cloud. Running the MQTT Mosquitto Broker in the cloud allows you to connect several ESP32/ESP8266 boards and other IoT devices from anywhere using different networks as long as they have an Internet connection. Check the tutorial below:

What is an MQTT Broker?

MQTT stands for Message Queuing Telemetry Transport. MQTT is a simple messaging protocol, designed for constrained devices with low bandwidth. So, it’s the perfect solution to exchange data between multiple IoT devices.

MQTT communication works as a publish and subscribe system. Devices publish messages on a specific topic. All devices that are subscribed to that topic receive the message.

The MQTT broker is responsible for receiving all messages, filtering the messages, deciding who is interested in them, and then publishing the message to all subscribed clients.


There are several brokers you can use. In home automation projects, we use the Mosquitto Broker installed on a Raspberry Pi.

You can also install the Mosquitto broker on your PC (which is not as convenient as using a Raspberry Pi board, because you have to keep your computer running all the time to keep the MQTT connection between your devices).

For a more detailed explanation about MQTT communication check out this article: What is MQTT and How It Works


Before continuing with this tutorial

You can use the preceding links or go directly to to find all the parts for your projects at the best price!

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

Installing Mosquitto Broker on Raspberry Pi OS

After having your Raspberry Pi board prepared with Raspberry Pi OS, you can continue with this tutorial. 

Let’s install the Mosquitto Broker.

1) Open a new Raspberry Pi terminal window. If you’re running your Raspberry Pi headless, check this tutorial to learn how to establish an SSH connection between your computer and the Raspberry Pi.

2) Run the following command to upgrade and update your system:

[email protected]:~ $ sudo apt update && sudo apt upgrade

3) Press Y and Enter. It will take some time to update and upgrade (in my case, it took approximately 10 minutes).

4) To install the Mosquitto Broker enter these next commands:

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

5) To make Mosquitto auto start when the Raspberry Pi boots, you need to run the following command (this means that the Mosquitto broker will automatically start when the Raspberry Pi starts):

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

6) Now, test the installation by running the following command:

[email protected]:~ $ mosquitto -v

This returns the Mosquitto version that is currently running in your Raspberry Pi. It will be 2.0.11 or above.

Installing Mosquitto MQTT broker Raspberry Pi

It will prompt the following message: “Starting in local only mode. Connections will only be possible from clients running on this machine. Create a configuration file which defines a listener to allow remote access.”

This is only applicable for Mosquitto version 2. More information about this topic on the Mosquitto documentation.

To enable remote access so that we can communicate with other IoT devices, we need to edit/create a configuration file.

7) Run the following command to open the mosquitto.conf file.

[email protected]:~$ sudo nano /etc/mosquitto/mosquitto.conf

8) Move to the end of the file using the arrow keys and paste the following two lines:

listener 1883
allow_anonymous true
Mosquitto configuration file to allow remote connections and anonymous users

9) Then, press CTRL-X to exit and save the file. Press Y and Enter.
10) Reboot your Raspberry Pi with the following command for the changes to take effect.

[email protected]:~$ sudo reboot

Raspberry Pi IP Address

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

[email protected]:~ $ hostname -I

In our case, the Raspberry Pi IP address is 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 your installation. You can follow the next tutorial:

Wrapping Up

An MQTT broker is essential if you want to use the MQTT protocol in IoT projects. The MQTT broker receives all MQTT messages and forwards them to all subscribed clients. In this tutorial, you’ve learned how to install the Mosquitto broker on a Raspberry Pi.

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 to my blog.

Updated December 15, 2021

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

44 thoughts on “Install Mosquitto MQTT 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

  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!

  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).

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

    • 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.

  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.

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

  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.

  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

  9. Hi, I need some help in building a salt water station that will measure temperature, electrical conductivity and dissolved oxygen. These three sensors will be hooked up on a tentacle t3 and on a raspberry pi 4b. I have scripts to read the data but I want somehow to have real time access to these data through a pc or a smartphone and not in the same network. Any ideas? Im newbie in Iot

  10. What if my devices isn’t on the same network as my Raspberry Pi? How can I use my Raspberry Public IP address as a server?

      • Certainly! If your Pi is reachable from outside of your local network, you can use the public IP address of your router. In the router’s settings make sure to route port numer 1883 to the appropriate address of the MQTT boker. Also make sure that your broker is password protected with a complex password becuause your firewall now has a hole in it.

  11. Hi Rui,
    Even though it says,
    8) Move to the end of the file using the arrow keys and paste the following two lines:
    The file opened is blank and has no lines as shown in your screenshot. Any mistakes I have done before?
    Also which shortcut keys that I have to use when copying ‘listener 1883’ from this website and to paste it to file?

  12. Well darn. I did a clean installation of Raspberry OS and then followed the instructions for installing mosquito exactly. Edited the mosquitto.conf and pasted in:
    listener 1883
    allow_anonymous true

    If I do mosquito -v after a reboot I still get:
    1639720453: mosquitto version 2.0.11 starting
    1639720453: Using default config.
    1639720453: Starting in local only mode. Connections will only be possible from clients running on this machine.
    1639720453: Create a configuration file which defines a listener to allow remote access.
    1639720453: For more details see
    1639720453: Opening ipv4 listen socket on port 1883.
    1639720453: Opening ipv6 listen socket on port 1883.
    1639720453: mosquitto version 2.0.11 running

    I thought perhaps the listen socket was good news, but when I run the esp32 project I built from your tutorial it still shows
    connecting to MQTT
    disconnecting from MQTT

    I think for some reason Mosquitto is not reading that config file.

    • Hi.
      Are you sure the changes made to the mosquitto.conf file were saved?
      After making the changes, you need to reboot your Raspberry Pi.

      • Sadly, yes, and I rebooted. In fact I unplugged everything last night because we had a storm lost power. Here’s a grab of the contents of /etc/mosquitto/mosquitto.conf:

        Place your local configuration in /etc/mosquitto/conf.d/


        A full description of the configuration file is at


        pid_file /run/mosquitto/

        persistence true
        persistence_location /var/lib/mosquitto/

        log_dest file /var/log/mosquitto/mosquitto.log

        include_dir /etc/mosquitto/conf.d
        listener 1883
        allow_anonymous true

        The only odd thing I’ve noticed so far is that when I opened the mosquitto.conf file it still had my original changes in it. I didn’t reformat the mini-sd card, but the raspberry pi imager installation claimed all the original data would be lost. Apparently, that’s not entirely true.

  13. Well, I have MQTT working, though I’m not entirely sure why. Looking around in stack overflow I reread this post:

    And decided to try: $ mosquitto -v -c /etc/mosquitto/mosquitto.conf
    which yielded:
    1639763140: Error: Unable to write pid file.
    Which sort of confirms my concern that mosquitto is not usually reading the /etc/mosquitto/mosquitto.conf file.

    So I tried: sudo mosquitto -v -c /etc/mosquitto/mosquitto.conf
    Which doesn’t yield any messages at all. I checked to see if the tutorial app can actually connect to MQTT and it does! Yay. sort of. Of course, this means Mosquitto is not started as a usable service when the system boots. Perhaps you folks could figure out how to make that happen. I’d be happy about all this if I thought I knew what I was learning something, but I’m basically just trying stuff.

      • It isn’t working the way your tutorials and the fixes I found on the web said it should. That’s a problem because I understand your tutorials (which is the entire point of doing them) but I don’t understand this random forced solution. But yes, it’s working. I did the MQTT testing tutorial as well, and that worked properly.

        • Hi Bill.
          Thanks for the clarification.
          That fix is because, with version 2.0, by default, Mosquitto only allows connections from the local host for security reasons.
          Those lines we add to the configuration file allow us to connect any device on the local network to the broker.
          If you want to learn more about that subject, you can check the Mosquitto 2.0 vs 1.0 documentation:
          It is explained in greater detail there.
          I hope this helps.

  14. I’ve got the same issue as Bill Babcock, but I can’t seem to get sorted out on the pid file.

    I’m using a RPi4 with Raspian Buster

    From the terminal:
    [email protected]:~ $ mosquitto -v -c /etc/mosquitto/mosquitto.conf
    1641239054: Loading config file /etc/mosquitto/conf.d/custom.conf

    1641239054: Error: Unable to write pid file.

    If I run it without the config file, it starts fine:

    [email protected]:~ $ mosquitto
    1641239158: mosquitto version 2.0.12 starting
    1641239158: Using default config.
    1641239158: Starting in local only mode. Connections will only be possible from clients running on this machine.
    1641239158: Create a configuration file which defines a listener to allow remote access.
    1641239158: For more details see
    1641239158: Opening ipv4 listen socket on port 1883.
    1641239158: Opening ipv6 listen socket on port 1883.
    1641239158: mosquitto version 2.0.12 running

    1641239161: New connection from ::1:46566 on port 1883.

    Just wanted to post this here to see if anyone else is having the same issue and were able to resolve it. Even as sudo I can’t cd into the directory with the pid files.

    In my config file I just have the standard
    listener 1883
    allow_anonymous true

    Any tips advice would love to hear it.

  15. can I install MQTT broker on pi zero 2 W ?
    I try it without succes, I first run the apt update && apt upgrade command, but after download over 1GB of date, 7 minutes or so, it restart or something because there was no signal on hdmi out, the on board led seems to indicate some work, but after 1 hour there was still no signal on hdmi.
    So my quastion remains, is pi zero 2 w good for this article, or do I need a pi3 or 4 ?

      • Hi Sara
        I succesed to install MQTT. I had problems when I give upgrade command, allways freez, sd coruption etc. When I did just update command before mqrr install, was ok.

  16. I just installed Ubuntu on a PC that will be dedicated for running Node-red.
    I tried a few of the tutorials and find I cannot get my ESP8266 to send to Node-Red.

    my wish-list would be a simple ‘Hello-World’ with serial.print lines as to the success/failure of each step.

    In node-red, I get connecting next to my nodes, but nothing on the dashboard.

    I am using Mosquitto 1.6.9
    it seems that is the version for Ubuntu
    there is no configuration file and it appears (I am a noob so I don’t really know) that V2 of mosquitto defaulted to “this computer only” and you had to tell it to accept from other sources. i assume that means 1.6.9 did not need to have the mosquitto.config file.

    this tutorial:
    yields :
    Connecting to MQTT…
    Disconnected from MQTT.
    Connecting to MQTT…
    Disconnected from MQT

    this tutorial :
    yields :
    Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
    Attempting MQTT connection…failed, rc=-2 try again in 5 seconds
    Attempting MQTT connection…failed, rc=-2 try again in 5 seconds

    when I start Node-Red in Ubuntu, I get

    [mqtt-broker:7a5ee377caccc1ec] connection failed to broker : mqtt”//localhost:1883
    ( I hand typed as I am on my may computer reading from the screen of my dedicated PC)

    it seems I am having problems with network or linking or the broker…..
    can you point me towards the basics ?

      • [email protected]:~$ mosquitto
        [ 49.566528]~DLT~ 2068~INFO ~FIFO /tmp/dlt cannot be opened. Retrying later…
        1650669434: mosquitto version 1.6.9 starting
        1650669434: Using default config.
        1650669434: Opening ipv4 listen socket on port 1883.
        1650669434: Error: Address already in use
        [ 59.582398]~DLT~ 2068~WARNING ~Lost log messages in user buffer when exiting: 6


        [email protected]:~$ node-red
        22 Apr 19:18:05 – [info]

        Welcome to Node-RED

        22 Apr 19:18:05 – [info] Node-RED version: v2.2.2
        22 Apr 19:18:05 – [info] Node.js version: v14.19.1
        22 Apr 19:18:05 – [info] Linux 5.13.0-40-generic x64 LE
        22 Apr 19:18:05 – [info] Loading palette nodes
        22 Apr 19:18:07 – [info] Dashboard version 3.1.6 started at /ui
        22 Apr 19:18:08 – [warn] rpi-gpio : Raspberry Pi specific node set inactive
        22 Apr 19:18:08 – [info] Settings file : /home/iot/.node-red/settings.js
        22 Apr 19:18:08 – [info] Context store : ‘default’ [module=memory]
        22 Apr 19:18:08 – [info] User directory : /home/iot/.node-red
        22 Apr 19:18:08 – [warn] Projects disabled : editorTheme.projects.enabled=false
        22 Apr 19:18:08 – [info] Flows file : /home/iot/.node-red/flows.json
        22 Apr 19:18:08 – [info] Server now running at
        22 Apr 19:18:08 – [info] Starting flows
        22 Apr 19:18:08 – [info] Started flows
        22 Apr 19:18:08 – [info] [mqtt-broker:7a5ee377caccc1ec] Connection failed to broker: mqtt://localhost:1883
        22 Apr 19:18:23 – [info] [mqtt-broker:7a5ee377caccc1ec] Connection failed to broker: mqtt://localhost:1883

  17. Very helpful page, thank you.
    Finally I managed to access my RasPI remotely by adding two lines to the mosquitto.conf file.


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.