Hello World in Erlang

Our first program will print the classic “hello world” message. Here’s the full source code.

-module(hello_world).
-export([start/0]).

start() ->
    io:format("hello world\n").

To run the program, put the code in hello_world.erl and use erl to compile and run it.

$ erl
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Eshell V12.0  (abort with ^G)
1> c(hello_world).
{ok,hello_world}
2> hello_world:start().
hello world
ok

Sometimes we’ll want to build our programs into binaries. This can be done using escript.

First, create an hello_world escript file:

#!/usr/bin/env escript
%%! -smp enable -sname hello_world

-module(hello_world).
-export([main/1]).

main(_Args) ->
    io:format("hello world\n").

Make the escript executable and run it:

$ chmod +x hello_world
$ ./hello_world
hello world

Now that we can run and build basic Erlang programs, let’s learn more about the language.