How to Install Mosquitto on The Raspberry Pi

Mosquitto is a very lightweight broker and a Raspberry Pi can easily cope with MQTT traffic on clients on a smart home networks..

Mosquitto doesn’t come installed by default so you will need to install it.

Install Steps

First get the repo key

sudo apt-key add mosquitto-repo.gpg.key

Then make the repository available :

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

Then , depending on which version of debian you are using:

sudo wget
sudo wget
sudo wget

Then update apt information:

sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

For windows and linux see this tutorial on my other site.

Starting and Configuring Mosquitto

The install installs mosquitto as a service which starts automatically and tuns on port 1883.

If you need to change the configuration then the configuration file is called mosquitto.conf and is located in the /etc/mosquitto folder.

On the Pi mosquitto can be controlled using the
systemctl command. You can sopt,start and restart the service using.

sudo systemctl stop mosquitto.service
sudo systemctl start mosquitto.service
sudo systemctl restart mosquitto.service

You can find more details, like how to run multiple instances, change port etc on my other site here.

Running Mosquitto Manually

When testing you will find it very useful to run mosquitto manually from the command line.

The first step is to stop mosquitto using:

sudo systemctl stop mosquitto.service

Then you can start it using:

mosquitto -v   #start in verbose mode

the -v option displays verbose logging information other command line options are:

  • -p port_number
  • -c configuration file

To start mosquitto using a test configuration file use

mosquitto -c test.conf

To start mosquitto using port 1884 use

mosquitto -p 1884

Testing the Install

If you followed the install instructions you will have also installed the mosquitto test clients mosquitto_pub and mosquitto_sub.

You can use these clients to test your mosquitto install.

The first step is to subscribe to a topic and then to publish a message to that topic which you should see in the subscribe client.

So first open two command prompts in one type.

mosquitto_sub -h localhost -t test -d

in the second type

mosquitto_pub -h localhost -t test -m message

You should see the message appear in the subscribe window.


Related Tutorials and Resources:

Please Let me Know if you found it Useful
[Total: 7 Average: 4.9]


  1. Hi I have installed MQTT onto RPI 4 & 4g, installed in docker with the allow_ anonymous – listener it states running , however there is an error Address not Available . Is there a solution as i cannot find one trawling the web for a solution brought me to downgrade to eclipse 1.6 image. Is this a solution?
    Hope you can help?

    1. It looks like a port issue and not a mosquitto issue. Check that no other instance of mosquitto is running.

  2. Hi there!
    first of all thanks for the great tutorial. There is a lot of info out there , but most of it is outdated.
    I have the same issue as John has, a fresh install of homebridge and mosquitto that gave me V2.0.9.
    From this point onwards, the MQT communication is not working anymore.
    MQT: Attempting connection…
    MQT: Connect failed, rc -2. Retry in 60 sec

    I have commented my previous (V1) config and left only your proposal, but still no connection:
    allow_anonymous true
    password_file /etc/mosquitto/passwd
    listener 1883

    Any other ideas on how to solve this?
    Thanks beforehand!

    1. Remove the password file line unless you are using passwords. If you use passwords remove the allow_anonymous line.

  3. Firstly, thanks for all this. NB the following is probably date specific – 5th March 2021.

    I’m trying things out on multiple raspberry pi’s with the broker installed on an old Model B running as a nas, then MQTT publishing from a Rpi 400 on the kitchen table (prob not a unique setup). All working fine until I get to the websockets tutorial – the problem being finding examples of a local config file. As far as I can see, mosquitto by default doesn’t have a config file, you have to create your own.

    On the pi nas, after doing update and full-upgrade, sudo apt install mosquitto mosquitto-clients installs mosquitto version 1.5.7 which is a bit old and others say they’re using v 1.6. So, I followed your install instructions above (I’m running buster) to see if I could get v 1.6. However, I now have version 2.0.8, which works fine trying out subscribing and publishing via two terminal windows on the nas. However, my python script (using your instructions) on kitchen table pi (which was working fine) now says ‘socket.error: [Errno 111] Connection refused’ when trying a client.connect

    Presumably, this is a configuration issue on the broker. Currently, I’m trying to figure this out and assume I have to create my own config. Maybe mosquitto v 2 is a bit fussier with permissions?

    Thanks again and any help much appreciated.

    1. Mosquitto 2 default config is more secure a basic config file to make it work as per earlier versions is

      listener 1883
      allow_anonymous true

      just create this simple file and either copy it as mosquitto.conf to /etc/mosquitto/mosquitto.conf
      or use it when manually starting mosquitto as
      mosquitto -c conf_file

  4. This only installs a updated 1.6.x series on Raspberry Pi but 2.0.x on Ubuntu.
    I have problems with getting one broker sending topics to another on the 1.6.x series (random socket errors and difficulty re-connecting) that appears to work find on the 2.0.x series on Ubuntu, but I would really like to use the Pi.

    1. I don’t think the v2 is available for the pi yet as it is a different chipset. I haven’t seen socket errors on pi how are you testing?

      1. I installed mosquitto on a new Raspberry Pi using the method provided here on March 11, 2021, and (without doing anything special) I got version 2.0.9. So evidently v2 is now available on raspbian.

        I also upgraded an existing Raspberry Pi with mosquitto 1.6.12 to 2.0.9 by following the sequence given above:
        sudo apt-get update
        sudo apt-get install mosquitto
        sudo apt-get install mosquitto-clients

Leave a Reply

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