This article is going to cover shortly how to install Mosquitto MQTT broker On Ubuntu 16.04 using apt-get. As for other operating systems and other Linux distros as well, you can find the installation guides on the Mosquitto website.

Mosquitto MQTT Broker
1. Add the Mosquitto-dev PPA to repositories list
To install Mosquitto MQTT Broker On Ubuntu 16.04, we will need to add the Mosquitto PPA repository first. And we can do that simply by running the below command:
1 |
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa |
If you’re behind a proxy, you can try to add -E parameter to the command
1 2 3 |
export http_proxy=http://<proxy>:<port> export https_proxy=http://<proxy>:<port> sudo -E add-apt-repository ppa:mosquitto-dev/mosquitto-ppa |
2. Update Package Repository
The next is to update your package repository
1 |
sudo apt-get update |
3. Install Mosquitto MQTT Broker on Ubuntu 16.04
To install the broker, we can execute the below command:
1 |
sudo apt-get install mosquitto |
Next, we will install mosquitto-clients package which is a Mosquittoclient or Mosquitto CLI, allows us to communicate the Mosquitto broker for different purposes such as: debugging, testing, etc
1 |
sudo apt-get install mosquitto-clients |
4. Basic about Mosquitto MQTT Broker
4.1. Starting the Mosquitto broker
1 |
sudo /etc/init.d/mosquitto start |
4.2. Stopping the broker
1 |
sudo /etc/init.d/mosquitto stop |
4.3. Checking status of the broker
1 |
sudo /etc/init.d/mosquitto status |
4.4. Configuration
The configuration file of the Mosquitto broker is located at: /etc/mosquitto/mosquitto.conf
The default TCP port of the broker is 1883 which is used for the ‘mqtt’ scheme and the port 8883 is used by default for the ‘mqtts’ scheme.
If you want to access the broker from the outside, make sure those ports are not blocked by your firewall.
5. Testing The Mosquitto MQTT Broker
We have finished the installation, and in this section we will verify whether the Mosquitto works or not. We will publish messages and subscribe on the topic: hellomqtt/test.
5.1. Subscribe on the topic: hellomqtt/topic
Open a new terminal (we call it terminal #1) and execute the command:
1 |
mosquitto_sub -h localhost -t "hellomqtt/topic" |
After the above command, the terminal will block and listen for the messages on the topic “hellomqtt/topic”
5.2. Publish on the topic: hellomqtt/topic
Open a new terminal (we call it terminal #2) and publish several messages to the “hellomqtt/topic”
1 |
mosquitto_pub -d -t hellomqtt/topic -m "Hello world" |
The output on the terminal #2 will be similar to:
1 2 3 4 |
Client mosqpub/4917-ubuntu-xen sending CONNECT Client mosqpub/4917-ubuntu-xen received CONNACK Client mosqpub/4917-ubuntu-xen sending PUBLISH (d0, q0, r0, m1, 'hellomqtt/topic', ... (11 bytes)) Client mosqpub/4917-ubuntu-xen sending DISCONNECT |
Back to the terminal #1, we will see the “Hello world” message appeared.
6. Summary
We have learned how to install Mosquitto MQTT Broker on Ubuntu 16.04 and some basic operations and configurations as well. More advanced usages and configuration will come in future posts.
Below are other related posts:
Apache Kafka Connect MQTT Source Tutorial