Mirosław Pragłowski bio photo

Mirosław Pragłowski

.NET/Ruby/... dev

Email Twitter Facebook LinkedIn Instagram Github Stackoverflow Pinterest Foursquare

Installing Erlang & rebar3

On OSX just use brew.

brew install rebar3

It will install rebar3 with Erlang & OTP.

After install:

$ which rebar3
/usr/local/bin/rebar3
$ rebar3 version
rebar 3.1.0 on Erlang/OTP 18 Erts 7.3

Creating application

$ rebar3 new app helloerlang
===> Writing helloerlang/src/helloerlang_app.erl
===> Writing helloerlang/src/helloerlang_sup.erl
===> Writing helloerlang/src/helloerlang.app.src
===> Writing helloerlang/rebar.config
===> Writing helloerlang/.gitignore
===> Writing helloerlang/LICENSE
===> Writing helloerlang/README.md

After successful creation of new application you could compile it:

$ rebar3 compile
===> Verifying dependencies...
===> Compiling helloerlang

Compilation should create output _build folder:

$ tree _build
_build/
└── default
    └── lib
        └── helloerlang
            ├── ebin
            │   ├── helloerlang.app
            │   ├── helloerlang_app.beam
            │   └── helloerlang_sup.beam
            ├── include -> ../../../../include
            ├── priv -> ../../../../priv
            └── src -> ../../../../src

Adding dependencies

Edit yout rebar.config file to add required dependency. In my case it will be a cowboy web server.

{erl_opts, [debug_info]}.
{deps, [
        cowboy
       ]}.

Or you could use alternative source:

{erl_opts, [debug_info]}.
{deps, [
        {cowboy, {git, "git://github.com/ninenines/cowboy.git", {tag, "2.0.0-pre.3"}}}
       ]}.

Then you could add cowboy to your project’s application’s .app.src file.

Next run rebar3 compile to fetch dependencies:

$ rebar3 compile
===> Verifying dependencies...
===> Fetching cowboy ({pkg,<<"cowboy">>,<<"1.0.4">>})
===> Fetching cowlib ({pkg,<<"cowlib">>,<<"1.0.2">>})
===> Fetching ranch ({pkg,<<"ranch">>,<<"1.2.1">>})
===> Compiling cowlib
===> Compiling ranch
===> Compiling cowboy
===> Compiling helloerlang

Now your outut folder will contain all dependencies (also the ones required by the one you have added to your project):

$ tree _build/
_build/
└── default
    └── lib
        ├── cowboy
        │   ├── ... all cowboy files here
        ├── cowlib
        │   ├── ... all cowlib files here
        ├── helloerlang
        │   ├── ebin
        │   │   ├── helloerlang.app
        │   │   ├── helloerlang_app.beam
        │   │   └── helloerlang_sup.beam
        │   ├── include -> ../../../../include
        │   ├── priv -> ../../../../priv
        │   └── src -> ../../../../src
        └── ranch
           └── ... all ranch files here

Remember to remove rebar.lock if you want to change version of dependencies used.

Simplest web application

To setup cowboy you need to start HTTP server and define routues to be handled by it. Edit the start method in src/hellowrlang_app.erl file:

start(_StartType, _StartArgs) ->
    Dispatch = cowboy_router:compile([{'_', routes() }]),
    ok = case cowboy:start_http(
                helloerlang_http_listener, 100,
                [{port, 4000}],
                [{env, [{dispatch, Dispatch}]}]) of
             {ok, _} -> ok;
             {error, {already_started, _}} -> ok;
             {error, _} = Error -> Error
         end,
    helloerlang_sup:start_link().

This will setup cowboy to listen on port 4000. All you need more is to define routes() method:

routes() ->
    [
        {"/", cowboy_static, {priv_file, helloerlang, "index.html"}}
    ].

This setup will serve static index.html file localed in priv folder of your application.

Setup a release

To run the application you need to define a release. Edit the rebar.config file and add release setup:

{relx, [{release, { local, "0.1.0" },
         [helloerlang]},

        {dev_mode, true},
        {extended_start_script, true}]
}.

Starting your application

To start your application locally use:

rebar3 run

And you will see:

$ rebar3 run
===> Verifying dependencies...
===> Compiling helloerlang
===> Starting relx build process ...
===> Resolving OTP Applications from directories:
          /Users/mpraglowski/erlang/helloerlang/_build/default/lib
          /usr/local/Cellar/erlang/18.3/lib/erlang/lib
          /Users/mpraglowski/erlang/helloerlang/_build/default/rel
===> Resolved local-0.1.0
===> Dev mode enabled, release will be symlinked
===> release successfully created!
... here quite a lot of text ...
=PROGRESS REPORT==== 12-May-2016::02:08:50 ===
         application: sasl
          started_at: helloerlang@mpmacbookair
Eshell V7.3 (abort with ^G)
(helloerlang@mpmacbookair)1>

Your application is started. Go to http://localhost:4000 to see if it working.