Erlang Tutorial
Learn Erlang step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Erlang? Meet Erlang: a functional language built at Ericsson for telephone switches, with lightweight processes, fault tolerance and the BEAM virtual machine.
- 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.
- Your First Module: Writing and Compiling Write your first Erlang module, export functions, compile it with erlc or c/1, run it from the shell and understand attributes, arity and export rules.
- Projects with rebar3 Use rebar3, the standard Erlang build tool: create an application, understand the project layout, compile, run the shell, add dependencies and build releases.
Language Basics
- Numbers and Atoms Learn Erlang's basic data types: arbitrary-size integers, floats, number bases, atoms, booleans and the type-checking functions every beginner needs.
- Variables and Single Assignment Understand why Erlang variables bind only once, how the = sign is a pattern match rather than assignment, and how to use underscore and scope safely.
- Operators, Comparison and Booleans Learn Erlang's comparison, boolean and bitwise operators, the difference between == and =:=, short-circuit andalso/orelse, and how terms are ordered.
- Printing Output and Working with Strings Print with io:format and its control sequences, understand why Erlang strings are lists of integers, and use the string module, binaries and iolists.
Functions and Pattern Matching
- Functions and Clauses Learn how Erlang functions work: clauses and heads, arity, the last expression as return value, multiple clauses, private helpers and function references.
- Pattern Matching in Erlang Master Erlang pattern matching: match tuples, lists, maps and strings in function heads, case and receive, bind variables and use the common ok/error idiom.
- case, if and Guards Choose between branches in Erlang with case, if and guard sequences. Learn which functions are allowed in guards, how guards fail safely and when to use each form.
- Recursion and Tail Calls Replace loops with recursion in Erlang: base cases, list recursion, accumulators, tail-call optimization, and why tail recursion keeps long-running servers alive.
- Funs and Higher-Order Functions Use anonymous functions (funs) in Erlang, capture variables in closures, pass funs to lists:map, filter and foldl, return funs and write named funs.
Data Structures
- Lists in Erlang Work with Erlang lists: linked-list structure, head and tail, cons, append, the lists module, sorting, searching, zipping, proper lists and performance traps.
- List Comprehensions Build lists concisely with Erlang list comprehensions: generators, filters, multiple generators, pattern generators, binary comprehensions and map comprehensions.
- Tuples and Records Use Erlang tuples for fixed-size groups, tagged tuples for results, and records for named fields with defaults, updates, pattern matching and header files.
- Maps Use Erlang maps as key-value data: create, read, update with => and :=, pattern match, iterate, use the maps module and choose between maps, records and proplists.
- Binaries and Bit Syntax Build and parse binary data in Erlang with bit syntax: segments, sizes, types, endianness, pattern matching network packets, bitstrings and binary functions.
Processes and Message Passing
- Processes and spawn Learn how Erlang processes work: spawn, self, process identifiers, isolation, cheap creation, and why millions of processes can run on one machine.
- Sending and receiving messages Learn Erlang message passing: the ! operator, mailboxes, receive with pattern matching, replying with the sender's pid, and request-response patterns.
- Selective receive and timeouts Learn how Erlang receive picks messages out of order, how the after clause adds timeouts, how make_ref prevents mixups, and how to avoid mailbox scans.
- Process loops and state Learn how Erlang processes keep state without variables that change: tail-recursive receive loops, counters, servers, hot code loading, and clean shutdown.
- Registered processes and the process dictionary Give Erlang processes names with register/2 and whereis/1, send to names, learn the limits of the name registry, and why the process dictionary is best avoided.
Fault Tolerance and OTP
- Links, monitors and exit signals Understand how Erlang processes detect each other's failure: spawn_link, exit signals, trap_exit, monitors and DOWN messages, and the let it crash philosophy.
- gen_server: the OTP generic server Learn the OTP gen_server behaviour: init, handle_call, handle_cast, handle_info, terminate, start_link, timeouts and a complete key-value server example.
- Supervisors and supervision trees Learn OTP supervisors: child specs, one_for_one, one_for_all and rest_for_one strategies, restart intensity, temporary and transient children, and supervision trees.
- OTP applications and releases Package Erlang code as an OTP application: the .app resource file, the application behaviour callback, environment variables, dependencies, and building releases.
Distribution and Ports
- Distributed Erlang: nodes and clusters Connect Erlang nodes into a cluster: node names, magic cookies, net_adm:ping, sending to remote pids, rpc calls, global names, monitor_node and security.
- Ports, NIFs and TCP sockets Talk to the outside world from Erlang: open_port for external programs, NIFs and port drivers, gen_tcp servers and clients, active and passive sockets.
Tools and Testing
- rebar3: building Erlang projects Use rebar3, the standard Erlang build tool: create apps and releases, rebar.config, dependencies from Hex, profiles, compile, shell, eunit, dialyzer and releases.
- Testing Erlang: EUnit, Common Test and PropEr Write Erlang tests with EUnit assertions and fixtures, run integration suites with Common Test, and find edge cases with property-based testing using PropEr.
- Debugging, tracing and the Observer Inspect running Erlang systems: io:format and logger, process_info, the Observer GUI, dbg tracing, the recon library, crash dumps and finding a mailbox leak.
- Types, specs and Dialyzer Document Erlang functions with -spec and -type, define custom and opaque types, run Dialyzer to find discrepancies, and understand success typing.
Projects and Practice
- Project: a chat room with processes Build a chat room in Erlang from processes and messages: a room server, join and leave, broadcast, monitoring clients, and a runnable JavaScript model of the design.
- Project: a supervised key-value store Build a key-value store as a real OTP application: a gen_server with TTL expiry, ETS-backed reads, a supervisor, tests and a runnable JavaScript model.
- Practice: concurrency and OTP exercises Twelve graded Erlang exercises on processes, message passing, receive timeouts, links, gen_server and supervisors, with hints, solutions and a runnable model.