Erlang in 2019

Mustafa
6 min readFeb 18, 2019

--

History

Erlang, or as it is also commonly referred to ‘Erlang/OTP’, is an open-source general-purpose programming language and runtime environment which was originally a proprietary language developed in 1986 for Ericsson, a networking and telecommunications company. The ‘OTP’ part of Erlang refers to Open Telecom Platform which is a the collection of libraries, tools, and middleware which are written in the Erlang language. Because Erlang was developed to work well for a large scale telecommunications company, it has many favorable characteristics. It is particularly well suited to be used for applications which are distributed, fault tolerant, reliable, soft real-time, and concurrent systems.

How to install ERLANG on Windows

If you want an Editor for Erlang, you can also download Visual Studio Code (VSC) from https://code.visualstudio.com/download and then the Erlang for VSC from https://marketplace.visualstudio.com/items?itemName=pgourlain.erlang

Which will allow for easier editing. You will need to configure the erlang extension in VSC by updating the directory location of erl and the default package manager rebar3 if applicable.

How to install ERLANG on Linux

1 — Adding the Repository

$ wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb$ sudo dpkg -i erlang-solutions_1.0_all.deb

2 — Install Erlang on Ubuntu

to install Erlang packages and its dependencies

$ sudo apt-get update
$ sudo apt-get install erlang

or install the complete package at once.

$ sudo apt-get update
$ sudo apt-get install esl-erlang

reference

We will demonstrate on how to create a hello world app on Linux

1- Let’s start with hello world program on erlang. First create file helloworld.erl with

$ vi helloworld.erl

and then we write this code block “remove the %”

% hello world program
-module(helloworld).
-export([start/0]).
start() ->
io:fwrite("Hello World!\n").

Now compile the hello world program using below command.

$ erlc helloworld.erl

The above command will create a file helloworld.beam. And now the program is ready to run.

$ erl -noshell -s helloworld start -s init stopHello World!

Alternatively, if you would to write your program in the VSC editor, simply create a .erl file named hello and write the following:

HOW Erlang

Package manager

What is the Package manager? A package manager or package management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer’s operating system in a consistent manner. A package manager deals with packages, distributions of software and data in archive files. Packages contain metadata, such as the software’s name, description of its purpose, version number, vendor, checksum, and a list of dependencies necessary for the software to run properly. Upon installation, metadata is stored in a local package database. Package managers typically maintain a database of software dependencies and version information to prevent software mismatches and missing prerequisites. They work closely with software repositories, binary repository managers, and app stores.

So what Erlang uses as a package manager?

Rebar3… This is the name of the main package manager for Erlang.

Rebar3 is a build tool and package management tool for Erlang. Creating and publishing your Erlang packages is simple thanks to Rebar3 with its plugin to Hex. Let’s make a simple “hello world” package, and feel free to play along at home!

Download Rebar3: http://www.rebar3.org/

or Use the terminal to download it

curl -O https://s3.amazonaws.com/rebar3/rebar3

Use chmod to make it executable, then add it to your environment variable PATH.

chmod +x rebar3
export PATH=$PATH:your-current-directory

Build an Erlang app using Rebar3 package manager.

Start with the command rebar3 new to generate a new project from the built-in template called app. In this example we are creating a project called myapp. Other available templates are: release, lib, plugin, escript, cmake.

$ rebar3 new app myapp
===> Writing myapp/src/myapp_app.erl
===> Writing myapp/src/myapp_sup.erl
===> Writing myapp/src/myapp.app.src
===> Writing myapp/rebar.config
===> Writing myapp/.gitignore
===> Writing myapp/LICENSE
===> Writing myapp/README.md

Code for your package is placed within the src directory.

$ cd myapp
$ tree
.
├── LICENSE
├── README.md
├── rebar.config
└── src
├── myapp.app.src
├── myapp_app.erl
└── myapp_sup.erl

The convention is to have one .app.src file to define your app as an OTP application, since Rebar3 only handles OTP structured projects. Looks familiar? The file is also Erlang. Check out the full reference to see what it can contain.

$ cat src/myapp.app.src 
{application, 'myapp',
[{description, "An OTP application"},
{vsn, "0.1.0"},
{registered, []},
{mod, {'myapp_app', []}},
{applications,
[kernel,
stdlib
]},
{env,[]},
{modules, []}
]}.

The code inside of src/myapp_app.erl is pretty bare bones. It just makes sure that you can start and stop your Erlang app:

$ cat src/myapp_app.erl 
-module('myapp_app').
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
'myapp_sup':start_link().
stop(_State) ->
ok.

Rebar3 uses a file called rebar.config to specify addition metadata, such as dependencies. There are lots of fields rebar.config can contain. To see them all check out the full sample.

$ cat rebar.config 
{erl_opts, [debug_info]}.
{deps, []}.

Now let’s use Rebar3 to start an Erlang shell with your app and the dependencies in the path. Run application:start(myapp). to verify that your app is correctly loaded.

$ rebar3 shell
===> Verifying dependencies...
===> Compiling myapp
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:8:8] [async-threads:0] [hipe] [kernel-poll:false]
Eshell V5.10.4 (abort with ^G)
1> application:start(myapp).
ok
2> application:stop(myapp).
ok
3>
=INFO REPORT==== 17-Feb-2019::16:14:10 ===
application: myapp
exited: stopped
type: temporary

Erlang Packages

We need to install a plugin called rebar3_hex in order to use fetch and install Erlang packages from Hex.pm, the Erlang/Elixir package manager. Simply add the following line to your rebar.config file. (You need Erlang version OTP 17.4 and above)

{plugins, [rebar3_hex]}.

Then run the commend: rebar3 update to enable the plugin.

$ rebar3 update
===> Fetching jsx ({pkg,<<"jsx">>,<<"2.6.1">>})
===> Fetching ssl_verify_hostname ({pkg,<<"ssl_verify_hostname">>,
<<"1.0.5">>})
===> Fetching rebar3_hex ({pkg,<<"rebar3_hex">>,<<"0.6.0">>})
===> Compiling ssl_verify_hostname
===> Compiling jsx
===> Compiling rebar3_hex
===> Updating package index...

If you want to avoid this step every time you create a new Erlang app, add the entry to a global rebar.config and place it at:

~/.config/rebar3/rebar.config

Finding Erlang packages

The search command lets you find remote Erlang packages published on Hex.pm. You can use regular expression characters in your query:

$ rebar3 hex search cowboy
cloudi_service_http_cowboy
cowboy

Installing packages

Rebar3 can download and install the Erlang packages and any necessary dependencies. Add the application names to the deps entry in your rebar.config file, then run the command: rebar3 compile. In this example we are trying to use two Erlang packages called cowboy and meck.

{deps, [cowboy, meck]}.$ rebar3 compile
===> Verifying dependencies...
===> Fetching ranch ({pkg,<<"ranch">>,<<"1.0.0">>})
===> Fetching meck ({pkg,<<"meck">>,<<"0.8.2">>})
===> Fetching cowlib ({pkg,<<"cowlib">>,<<"1.0.1">>})
===> Fetching cowboy ({pkg,<<"cowboy">>,<<"1.0.0">>})
===> Compiling cowlib
===> Compiling ranch
===> Compiling meck
===> Compiling cowboy
===> Compiling myapp

Want to install a specific version of an Erlang package? Write the application name and the version in a tuple. You can browse the available versions of a package at Hex home page.

{deps, [{cowboy, “1.0.2”}, {meck, "0.8.3"}]}.

Listing Installed packages

The rebar3 deps command shows your locally installed packages:

$ rebar3 deps
cowboy (locked package 1.0.0)
meck (locked package 0.8.2)

Uninstalling packages

To uninstall a package, you have to remove it from the rebar.config file first, then use the command: rebar3 unlock. Here we removed the package meck from our list.

$ rebar3 unlock
$ rebar3 deps
cowboy (locked package 1.0.0)

--

--