Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

Installing Erlang and Using the Shell

Install Erlang/OTP on Windows, macOS or Linux, start the erl shell, evaluate expressions, use history, and learn the shell commands you will use every day.

To follow this course on your own machine you need Erlang/OTP, the language plus its standard libraries. This lesson shows how to install it, start the interactive shell and use it as a calculator and experiment bench. The shell is where Erlang programmers spend a lot of their time: you try an idea there before it becomes a module.

Installing Erlang/OTP

Pick the route that matches your system. The commands are read-only examples; run them in your terminal, not here.

Terminal
# macOS (Homebrew)
brew install erlang

# Debian / Ubuntu
sudo apt install erlang

# Fedora
sudo dnf install erlang

# Windows (winget)
winget install Erlang.ErlangOTP

# Any system: asdf or mise version managers
asdf plugin add erlang
asdf install erlang latest

Windows users can also download the installer from the official Erlang website. Whatever method you use, check the result:

Terminal
erl -version

It prints something like Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 16.0. The number is the runtime version (ERTS), which differs from the OTP release number (for example OTP 28). Both matter, but for learning, any recent OTP release works.

If erl is not found on Windows, add the bin folder of the Erlang installation to your PATH, then open a new terminal.

Starting the shell

Type erl and press Enter. You get a banner and a prompt:

TEXT
Erlang/OTP 28 [erts-16.0] [64-bit] [smp:8:8] [jit]

Eshell V16.0 (press Ctrl+G for help)
1>

The 1> is the prompt, and the number counts your commands. Every expression you type must end with a period followed by Enter. The period is part of the syntax, just like a semicolon in other languages.

ERLANG
1> 2 + 3.
5
2> 10 / 4.
2.5
3> 10 div 4.
2
4> 10 rem 4.
2

The shell prints the value of each expression. / always gives a float, while div and rem are integer division and remainder.

Variables in the shell

The shell remembers variable bindings between commands. A variable starts with a capital letter:

ERLANG
1> Width = 8.
8
2> Height = 5.
5
3> Width * Height.
40

Erlang variables can be bound only once. Trying Width = 9. now would fail with a badmatch error, because Width is already 8. The next lessons explain why; for now, know that the shell offers f(Width). to forget one variable and f(). to forget all of them.

ERLANG
4> Width = 9.
** exception error: no match of right hand side value 9
5> f(Width).
ok
6> Width = 9.
9

Handy shell commands

Command What it does
b(). Show all current variable bindings
f(). / f(X). Forget all / one binding
h(). Show command history
v(N). Value of the expression on line N (v(-1). is the previous result)
c(Module). Compile and load a module in the current directory
l(Module). Load (or reload) an already compiled module
q(). Quit politely (init:stop())
Ctrl+C, then a Abort the shell immediately
Ctrl+G The job control menu

You can press the Up arrow to recall earlier lines, and Tab completes module and function names, for example lists:re followed by Tab suggests reverse, reverse/2 and others.

Calling library functions

A function call takes the form module:function(Arguments). Here are a few from the standard library:

ERLANG
1> lists:reverse([1, 2, 3]).
[3,2,1]
2> string:uppercase("erlang").
"ERLANG"
3> lists:sum([10, 20, 30]).
60
4> math:sqrt(16).
4.0
5> length([a, b, c]).
3

length/1 is a built-in function (BIF) that needs no module prefix. The /1 notation means "the function named length that takes one argument". This name/arity pair is how Erlang identifies functions: length/1 and length/2 would be unrelated functions.

Getting help from the shell

ERLANG
1> h(lists, reverse).

prints the documentation of lists:reverse/1 in recent releases. m(lists). lists every function a loaded module exports, and lists:module_info(exports). returns them as data.

Evaluating in a script

You can also run a one-liner without opening the shell:

Terminal
erl -noshell -eval 'io:format("~p~n", [lists:seq(1, 5)]), halt().'

-noshell hides the prompt, -eval runs the expression, and halt() stops the VM. Without halt() the program would stay open. The ~p~n part is a format string: ~p pretty-prints a term and ~n prints a newline.

A shell stand-in

The shell is a read-eval-print loop (REPL). To see the idea in action, here is a JavaScript stand-in that evaluates small arithmetic lines and keeps variables, with the same single-binding rule as Erlang. This is not Erlang; it only imitates the behavior you just read about.

JS Stand-in: a tiny shell with single assignment (JavaScript, not Erlang)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last two lines show the rule: rebinding to a different value fails, while "binding" to the same value succeeds, because = is really a match, not an assignment. The variables lesson explains this properly.

JS Stand-in: command history and v(N) (JavaScript, not Erlang)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must every expression typed into the Erlang shell end with?

    An expression ends with a period followed by Enter. Without it the shell waits for more input.
  2. 2. What does the notation length/1 mean?

    name/arity identifies a function. length/1 and length/2 would be different functions.
  3. 3. How do you forget a variable named Width in the shell?

    f(Name). forgets one binding and f(). forgets all of them.
  4. 4. What does erl -noshell -eval '...' do?

    -noshell removes the interactive prompt and -eval runs the given expression. Add halt() so the VM stops.

Summary

  • Install Erlang/OTP with your package manager or the Windows installer, then check with erl -version.
  • erl opens the shell; every expression ends with a period.
  • Variables begin with a capital letter and can be bound once; f() forgets them in the shell.
  • Functions are written module:function(Args) and identified by name and arity.
  • Useful commands: b(), f(), c(Module), h(Module, Function) and q().

Next: Your First Module