Your First Erlang Program (in Style).

written in Erlang

Always wanted to learn Erlang? Let’s create your first Erlang “Hello World” program in style!

In this HOWTO I’ll show you how to setup a bleeding edge Erlang development VPS and how to run you first Erlang program.

Main ingredient: Cores

Erlang’s main strength is it’s concurrency support. It likes cores, so for our ‘Hello World’ program we obviously need cores. Lot’s! Not 4, not 8, 20!

Create an account on Digital Ocean if you don’t have one yet (love them) and we’re going to boot up their biggest instance. It’s a steal at less than 1 dollar per hour. Just make sure you destroy it when done.

64GB and 20 cores will make our Hello World so snappy!

Fullscreen%2029/03/14%2021:36

  • Pick a datacenter location near you.
  • Select the latest version of Ubuntu: 13.10 x64.
  • Create the Droplet.
  • And ssh to your Droplet with the credentials received from Digital Ocean: ssh root@your_ip_address.

Bleeding Edge Erlang

We’re going to compile Erlang from it’s github repository master branch, At the time of writing it’s a few commits after R17 release candidate 2 which comes with a Hipe LLVM backend, maps and named funs. If that doesn’t make any sense, no worries, just remember it’s the fastest Erlang yet. And fast is good.

Install the required Ubuntu packages:

1
$ apt-get install tmux build-essential emacs24 git-core libncurses5-dev libssl-dev autconf htop

Fire up Tmux:

1
$ tmux

Install Kerl, a tool which makes building and switching Erlang versions easy.

1
2
$ curl -O https://raw.github.com/spawngrid/kerl/master/kerl
$ chmod a+x kerl

Let’s add some good configuration options for our Erlang installation

1
$ emacs .kerlrc

And add

1
KERL_CONFIGURE_OPTIONS="--disable-debug --without-javac --enable-shared-zlib --enable-dynamic-ssl-lib --enable-hipe --enable-smp-support --enable-threads --enable-kernel-poll"

And because we can, we forge our Erlang installation on 20 cores. Muahahaha.

To see those cores sweat for you on compilation, create another tmux window CTRL-b c and run htop.

Fullscreen%2029/03/14%2021:52

Besides the eye candy, compilation finishes under 5 minutes on a 20 core Digital Ocean Droplet. Whoop!

To start compilation of Erlang:

1
$ export MAKEFLAGS=-j20 && ./kerl build git git://github.com/erlang/otp.git master erlang_llvm

After compilation we need to install the build and activate it:

1
2
$ kerl install erlang_llvm erlang_llvm
$ . ~/erlang_llvm/activate

Great! We are now ready for our Pièce de résistance.

Just say: Hello!

Real Erlang hacker use Emacs, so let’s setup Emacs for Erlang development.

Fetch a good base config for Emacs:

1
$ curl -o https://gist.githubusercontent.com/wardbekker/22a1bd79eb93af85fde0/raw/41eb32cbe1bc231c21fba8ec090e537805de2c58/.emacs

Start up Emacs emacs. It will complain that it can’t find projmake-mode. Let’s fix that:

1
[ESC]-x package-install [Enter] projmake-mode

Exit emacs:

1
[CTRL]-x [CTRL]-c

Start up Emacs again emacs. Great! We can finally start writing our “Hello World” program. Oh, not, wait. First, we create a projmake file. The file is needed by Projmake-mode, a Flymake inspired mode that compiles your program on every save and shows build errors and warnings inline. Really useful!

1
[CRTL]-x f projmake [Enter]

Add these line and save the file

1
2
3
(projmake
 :name  "Hello"
 :shell "erlc +native hello.erl")

Ok, now we can really start writing our “Hello World” program and put those 20 cores and 64GB RAM to good use.

1
[CTRL]-x f hello.erl [Enter]

And type or paste:

1
2
3
4
5
-module(hello).
-export([just_say/0]).

just_say() ->
    io_format("hello~n", []).

And save with [CTRL]-x [CTRL]-s.

Whoops. We made an error as projmake-mode shows:

Fullscreen%2029/03/14%2021:24

Replace io_format with io:format and save again. That fixes our error!.

Let’s run our program. Fire up the Erlang shell with:

1
[ESC]-x erlang-shell

Load the hello module with:

1
1> m(hello). [Enter]

And run you first Erlang function….

1
2> hello:just_say(). [Enter]

Bliss!

Congratulations. You now have a powerful Erlang development environment in your hands.

Check out A beginners guide to Erlang to continue your Erlang binge.


Comments