Mainflux can be fetched from the official Mainflux GitHub repository:
go get github.com/mainflux/mainflux cd $GOPATH/src/github.com/mainflux/mainflux
Make sure that you have Protocol Buffers compiler (
Go Protobuf installation instructions are here.
Go Protobuf uses C bindings, so you will need to install C++ protobuf as a prerequisite.
Protocol Buffers for Go with Gadgets to generate faster marshaling and unmarshaling Go code. Protocol Buffers for Go with Gadgets instalation instructions can be found (here)(https://github.com/gogo/protobuf).
Build All Services#
GNU Make tool to build all Mainflux services:
Build artefacts will be put in the
N.B. All Mainflux services are built as a statically linked binaries. This way they can be portable (transferred to any platform just by placing them there and running them) as they contain all needed libraries and do not relay on shared system libraries. This helps creating FROM scratch dockers.
Build Individual Microservice#
Individual microservices can be built with:
will build the HTTP Adapter microservice.
Dockers can be built with:
or individually with:
N.B. Mainflux creates
FROM scratchdocker containers which are compact and small in size.
users-dbcontainers are built from a vanilla PostgreSQL docker image downloaded from docker hub which does not persist the data when these containers are rebuilt. Thus, rebuilding of all docker containers with
make dockersor rebuilding the
users-dbcontainers separately with
make docker_users-dbrespectively, will cause data loss. All your users, things, channels and connections between them will be lost! As we use this setup only for development, we don't guarantee any permanent data persistence. If you need to retain the data between the container rebuilds you can attach volume to the
users-dbcontainers. Check the official docs on how to use volumes here and here.
Building Docker images for development#
In order to speed up build process, you can use commands such as:
or individually with
make dockers and
make dockers_dev are similar. The main difference is that building images in the development mode is done on the local machine, rather than an intermediate image, which makes building images much faster. Before running this command, corresponding binary needs to be built in order to make changes visible. This can be done using
make <service_name> command. Commands
make dockers_dev and
make docker_dev_<service_name> should be used only for development to speed up the process of image building. For deployment images, commands from section above should be used.
The MQTT Microservice in Mainflux is special, as it is currently the only microservice written in NodeJS. It is not compiled, but node modules need to be downloaded in order to start the service:
cd mqtt npm install
Note that there is a shorthand for doing these commands with
After that, the MQTT Adapter can be started from top directory (as it needs to find
*.proto files) with:
If you've made any changes to
.proto files, you should call
protoc command prior to compiling individual microservices.
To do this by hand, execute:
protoc --gofast_out=plugins=grpc:. *.proto
A shorthand to do this via
make tool is:
N.B. This must be done once at the beginning in order to generate protobuf Go structures needed for the build. However, if you don't change any of
.protofiles, this step is not mandatory, since all generated files are included in the repo (those are files with
Cross-compiling for ARM#
Mainflux can be compiled for ARM platform and run on Raspberry Pi or other similar IoT gateways, by following the instructions here or here as well as information
found here. The environment variables
GOARM=7 must be set for the compilation.
Cross-compilation for ARM with Mainflux make:
GOOS=linux GOARCH=arm GOARM=7 make
To run all of the tests you can execute:
Dockertest is used for the tests, so to run them, you will need the Docker daemon/service running.
Installing Go binaries is simple: just move them from
$GOBIN (do not fortget to add
$GOBIN to your
You can execute:
which will do this copying of the binaries.
N.B. Only Go binaries will be installed this way. The MQTT adapter is a NodeJS script and will stay in the
Mainflux uses NATS as it's central message bus. For development purposes (when not run via Docker), it expects that NATS is installed on the local system.
To do this execute:
go get github.com/nats-io/gnatsd
This will install
gnatsd binary that can be simply run by executing:
Mainflux uses PostgreSQL to store metadata (
channels entities alongside with authorization tokens).
It expects that PostgreSQL DB is installed, set up and running on the local system.
Information how to set-up (prepare) PostgreSQL database can be found here, and it is done by executing following commands:
# Create `users` and `things` databases sudo -u postgres createdb users sudo -u postgres createdb things # Set-up Postgres roles sudo su - postgres psql -U postgres postgres=# CREATE ROLE mainflux WITH LOGIN ENCRYPTED PASSWORD 'mainflux'; postgres=# ALTER USER mainflux WITH LOGIN ENCRYPTED PASSWORD 'mainflux';
Running of the Mainflux microservices can be tricky, as there is a lot of them and each demand configuration in the form of environment variables.
The whole system (set of microservices) can be run with one command:
which will properly configure and run all microservices.
Please assure that MQTT microservice has
node_modules installed, as explained in MQTT Microservice chapter.
make runactually calls helper script
scripts/run.sh, so you can inspect this script for the details.