Your First Erlang Program (in Style).
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!
- 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
|
|
Fire up Tmux:
1
|
|
Install Kerl, a tool which makes building and switching Erlang versions easy.
1 2 |
|
Let’s add some good configuration options for our Erlang installation
1
|
|
And add
1
|
|
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
.
Besides the eye candy, compilation finishes under 5 minutes on a 20 core Digital Ocean Droplet. Whoop!
To start compilation of Erlang:
1
|
|
After compilation we need to install the build and activate it:
1 2 |
|
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
|
|
Start up Emacs emacs
. It will complain that it can’t find projmake-mode. Let’s fix that:
1
|
|
Exit emacs:
1
|
|
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
|
|
Add these line and save the file
1 2 3 |
|
Ok, now we can really start writing our “Hello World” program and put those 20 cores and 64GB RAM to good use.
1
|
|
And type or paste:
1 2 3 4 5 |
|
And save with [CTRL]-x [CTRL]-s
.
Whoops. We made an error as projmake-mode shows:
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
|
|
Load the hello
module with:
1
|
|
And run you first Erlang function….
1
|
|
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.