How to Install Mosquitto MQTT Broker on Ubuntu and Raspberry Pi

How to Install Mosquitto MQTT Broker on Ubuntu and Raspberry Pi

How to Install Mosquitto MQTT Broker on Ubuntu and Raspberry Pi

Introduction

Before IoT devices can exchange messages using MQTT, they need an MQTT broker. The broker acts as the central communication hub, receiving messages from publishers and delivering them to subscribers.

One of the most popular MQTT brokers available today is Eclipse Mosquitto. It is lightweight, open-source, easy to install, and widely used in IoT projects ranging from smart homes and environmental monitoring systems to industrial automation.

In this tutorial, you’ll learn how to install Mosquitto MQTT Broker on Ubuntu and Raspberry Pi, verify the installation, and test MQTT communication using the built-in command-line tools.

By the end of this guide, you’ll have a fully functional MQTT broker ready to connect ESP32 devices, Node-RED dashboards, and other MQTT clients.

What is Mosquitto?

Mosquitto is an open-source MQTT broker developed by the Eclipse Foundation.

It supports:

  • MQTT v3.1
  • MQTT v3.1.1
  • MQTT v5.0

Mosquitto is popular because it:

  • Requires minimal system resources
  • Runs efficiently on Raspberry Pi
  • Supports Linux, Windows, and macOS
  • Supports TLS encryption
  • Supports authentication and access control
  • Is free and open source

Why Use Mosquitto?

Mosquitto is one of the most widely used MQTT brokers in IoT development because it is reliable, lightweight, and simple to configure.

Common use cases include:

  • Smart home automation
  • ESP32 and Arduino projects
  • Node-RED dashboards
  • Industrial monitoring systems
  • Environmental data collection
  • Agricultural IoT applications

Because it requires very little processing power and memory, Mosquitto performs exceptionally well on Raspberry Pi devices.

Prerequisites

Before starting, ensure you have:

  • Ubuntu 20.04 or later
  • Raspberry Pi OS
  • Internet connection
  • Terminal access
  • User account with sudo privileges

Update Your System

Before installing Mosquitto, update your package list and installed packages.

sudo apt update

sudo apt upgrade -y

This ensures your system is running the latest software versions.

Install Mosquitto MQTT Broker

Install Mosquitto and its client utilities using the package manager.

sudo apt install mosquitto mosquitto-clients -y

The installation includes:

Mosquitto Broker

The MQTT server responsible for receiving and routing messages between MQTT clients.

Mosquitto Clients

Command-line tools used for testing MQTT communication.

Useful tools include:

  • mosquitto_pub
  • mosquitto_sub

Enable Mosquitto Service

Configure Mosquitto to start automatically during system boot.

sudo systemctl enable mosquitto

Start the service.

sudo systemctl start mosquitto

Verify the service status.

sudo systemctl status mosquitto

Expected output:

Active: active (running)

If you see this message, the broker is running successfully.

Verify Mosquitto Installation

Check the installed Mosquitto version.

mosquitto -h

Example output:

mosquitto version 2.x

This confirms that Mosquitto is installed correctly.

Test MQTT Communication

Let’s verify that the broker can receive and deliver messages.

Open Terminal 1 and subscribe to a topic.

mosquitto_sub -h localhost -t test/topic

Open Terminal 2 and publish a message.

mosquitto_pub -h localhost -t test/topic -m "Hello MQTT"

If everything is working correctly, Terminal 1 should display:

Hello MQTT

Congratulations! Your MQTT broker is successfully routing messages.

How to Install Mosquitto MQTT Broker on Ubuntu and Raspberry Pi

Understanding the Test

The MQTT communication test involves three components.

Publisher

mosquitto_pub

Sends messages to the broker.

Broker

Mosquitto

Receives and routes messages.

Subscriber

mosquitto_sub

Receives messages from subscribed topics.

This demonstrates the publish-subscribe architecture that makes MQTT efficient for IoT applications.

Allow Remote MQTT Connections (Development Setup)

For local testing and learning purposes, you may want devices on your network to connect to the broker.

Open the Mosquitto configuration file.

sudo nano /etc/mosquitto/mosquitto.conf

Add the following lines:

listener 1883
allow_anonymous true

Save the file and restart Mosquitto.

sudo systemctl restart mosquitto

Important

The allow_anonymous true setting should only be used in local development environments.

For production deployments, enable authentication and TLS encryption.

Continue with: MQTT Security with TLS

Find Your MQTT Broker IP Address

To allow other devices to connect, determine your system’s IP address.

hostname -I

Example output:

192.168.1.100

Other devices on the network can connect using:

192.168.1.100:1883

Port 1883 is the default MQTT port.

Check Open Port

Verify that Mosquitto is listening on port 1883.

sudo netstat -tulpn | grep 1883

Expected output:

tcp 0 0 0.0.0.0:1883

This confirms the broker is accepting connections.

Basic Mosquitto Configuration

The main configuration file is located at:

/etc/mosquitto/mosquitto.conf

Additional configuration files are stored in:

/etc/mosquitto/conf.d/

View the default configuration:

sudo nano /etc/mosquitto/mosquitto.conf

Most beginner projects can use the default configuration without modification.

Restart Mosquitto After Configuration Changes

Whenever configuration changes are made, restart the broker.

sudo systemctl restart mosquitto

Verify status:

sudo systemctl status mosquitto

Common Installation Issues

Service Not Starting

Check Mosquitto logs.

journalctl -u mosquitto

Port Already in Use

Verify if another application is using port 1883.

sudo lsof -i :1883

Firewall Blocking Connections

Allow MQTT traffic through the firewall.

sudo ufw allow 1883

Reload firewall rules.

sudo ufw reload

Installing Mosquitto on Raspberry Pi

If you’re using Raspberry Pi OS, the installation process is identical to Ubuntu.

Run:

sudo apt update

sudo apt upgrade -y

sudo apt install mosquitto mosquitto-clients -y

Then enable and start the service:

sudo systemctl enable mosquitto

sudo systemctl start mosquitto

Your Raspberry Pi is now ready to run Mosquitto.

For a complete Raspberry Pi MQTT server setup, including static IP configuration, ESP32 integration, Node-RED connectivity, and broker optimization, continue with:

Raspberry Pi MQTT Broker Setup Using Mosquitto

Conclusion

Mosquitto is one of the easiest MQTT brokers to install and an excellent choice for beginners learning MQTT. In just a few minutes, you can set up a lightweight broker capable of handling communication between IoT devices, dashboards, and automation systems.

With Mosquitto running successfully, you’re now ready to connect ESP32 devices, build real-time dashboards with Node-RED, and create complete IoT solutions.

Frequently Asked Questions

What is Mosquitto MQTT Broker?

Mosquitto is a lightweight open-source MQTT broker that enables communication between MQTT publishers and subscribers.

Is Mosquitto free?

Yes. Mosquitto is completely free and open source under the Eclipse Foundation.

Which port does Mosquitto use?

Mosquitto uses port 1883 for unencrypted MQTT communication and port 8883 for MQTT over TLS.

Can Mosquitto run on Raspberry Pi?

Yes. Raspberry Pi is one of the most popular and cost-effective platforms for hosting a Mosquitto MQTT broker.

How do I check if Mosquitto is running?

Use:

sudo systemctl status mosquitto

Can ESP32 connect directly to Mosquitto?

Yes. ESP32 devices can connect directly to a Mosquitto MQTT broker using libraries such as PubSubClient. Once connected, they can publish sensor data and subscribe to control commands.

What is the difference between Mosquitto and MQTT?

MQTT is the communication protocol, while Mosquitto is software that implements an MQTT broker.

Related MQTT Tutorials:

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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