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: 4 Average: 4.8]


  1. 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?

Leave a Reply

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