Elixir Tutorial
Learn Elixir step by step: 48 lessons in 8 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Elixir? Meet Elixir, a functional language that runs on the Erlang VM (the BEAM). Learn where it came from, what makes it good at concurrency, and where teams use it.
- Installing Elixir and Using IEx Install Erlang/OTP and Elixir on Windows, macOS or Linux, start the IEx shell, run .exs scripts and check versions with mix --version. Includes a REPL stand-in.
- Your First Elixir Program Write your first Elixir program: print with IO.puts and IO.inspect, add comments, tell .ex from .exs, and see how modules and functions fit together.
- Basic Types: Numbers, Atoms and Booleans Learn Elixir's basic types: big integers, floats, arithmetic with / div and rem, atoms, true, false and nil, and the simple truthiness rule behind if.
- Strings and Binaries Work with Elixir strings: UTF-8 binaries, interpolation, heredocs, the String module, the difference between bytes and graphemes, and charlists vs strings.
Core Language
- Pattern Matching: The = Operator Understand Elixir's match operator: destructure tuples, lists and maps, ignore parts with _, use the pin operator ^, and read a MatchError. Includes a JS matcher.
- Immutability and Rebinding Understand why Elixir data never changes in place, how rebinding differs from mutation, and how structural sharing keeps new copies cheap.
- Operators and Comparison Learn Elixir's arithmetic, comparison, boolean and string operators, the difference between and and &&, ranges, in, and how any two terms are ordered.
- Tuples and Lists Learn how Elixir tuples and linked lists work, how to use head and tail, what ++ and -- cost, and when keyword lists are the right choice.
- Maps and Structs Learn Elixir maps and structs: building them, reading with dot and bracket syntax, update syntax, nested data, defstruct and @enforce_keys.
- Conditionals: if, unless, cond and case Choose between branches in Elixir with if, unless, cond, case and with. Learn truthiness, patterns in case, guards and how with chains steps.
- Functions and Modules Learn how to define Elixir functions inside modules with def and defp, use anonymous functions and the capture operator, arity, defaults and attributes.
- Multiple Clauses and Guards Learn how Elixir picks a function clause by pattern matching, how guards refine a match, which expressions are allowed in guards, defguard and clause-order traps.
- The Pipe Operator Learn the Elixir pipe operator |>: how it passes values as the first argument, how to design pipeline-friendly functions, use then and tap, and avoid common traps.
- Recursion and Tail Calls Learn recursion in Elixir: base and recursive cases, accumulators, body vs tail recursion, tail-call optimization on the BEAM and when to use Enum instead.
Enumerables and Data Processing
- The Enum Module Learn the Elixir Enum module: map, filter, reduce, sort, group_by, zip, chunk_every, frequencies and more, how eager evaluation works and when to use Stream.
- Comprehensions with for Learn Elixir for comprehensions: generators, filters, multiple generators, pattern matching, :into, :reduce, :uniq and bitstring generators, with clear examples.
- Lazy Processing with Stream Learn Elixir Stream: lazy vs eager evaluation, Stream.map, take, iterate, unfold and cycle, streaming files line by line, and a JavaScript generator stand-in.
- Ranges, Sorting and Custom Ordering Learn Elixir ranges with steps, Enum.sort and sort_by, sorting by several keys, descending order, comparing dates with Date.compare and how term ordering works.
- Sigils and Regular Expressions Learn Elixir sigils such as ~r, ~s, ~w and ~D, the Regex module, named captures, replacing text, modifiers and how to write your own custom sigil.
Protocols, Errors and Tooling
- Error Handling: Tuples, with and Exceptions Learn how Elixir handles errors with {:ok, value} and {:error, reason} tuples, the with expression, raise and rescue, try/catch, custom exceptions and let it crash.
- Protocols and Behaviours Learn Elixir polymorphism: defprotocol and defimpl for data types, behaviours with @callback and @impl for modules, and the built-in Enumerable and Inspect protocols.
- Metaprogramming and Macros Understand Elixir metaprogramming: the AST, quote and unquote, writing a simple defmacro, macro hygiene, __using__, and why you should avoid macros most of the time.
- Typespecs, Dialyzer and Documentation Learn Elixir @type and @spec typespecs, @moduledoc and @doc documentation, doctests, ExDoc, Dialyzer basics and how the compiler's new set-theoretic types fit in.
- Mix: Creating and Managing Projects Learn how Mix creates Elixir projects, what mix.exs contains, how dependencies and Hex work, and how environments and releases fit in.
Concurrency and OTP
- Processes and Message Passing Learn how Elixir processes work: spawn, send, receive, mailboxes and self(). Includes a JavaScript actor stand-in you can run in the browser.
- Links, Monitors and Let It Crash Learn how Elixir processes find out about each other's failures with spawn_link, Process.monitor and trap_exit, and why 'let it crash' works.
- Keeping State in a Process Learn how Elixir keeps changing state with immutable data: recursive receive loops, request/response messages, Agent and Task, with JS stand-ins.
- GenServer Learn GenServer in Elixir: init, handle_call, handle_cast, handle_info, naming, timeouts and state design, with a JavaScript stand-in to experiment.
- Supervisors and Supervision Trees Learn how Elixir supervisors restart crashed processes: child specs, one_for_one, one_for_all, rest_for_one, max restarts and DynamicSupervisor.
- Tasks and Parallel Work Run work in parallel with Elixir's Task module: Task.async and await, timeouts, Task.Supervisor, and async_stream with bounded concurrency and back-pressure.
- ETS, Registry and Application State Learn when to keep data in ETS tables, how to find processes with Registry, what persistent_term is for, and when a plain process is still the better choice.
Real-World Elixir
- Testing with ExUnit Write Elixir tests with ExUnit: assert and refute, describe and setup, doctests, async tests, mocks through behaviours, and an intro to property testing.
- Files, JSON and Serialization Read and write files with Elixir's File and Path modules, stream large files line by line, encode JSON with Jason, and serialize terms with term_to_binary.
- Ecto: Databases and Changesets Learn Ecto, Elixir's database toolkit: repos, schemas, changesets for validation, composable queries, migrations and Ecto.Multi transactions.
- Phoenix: Web Applications Meet Phoenix, the Elixir web framework: how a request flows through the endpoint, router, plugs and controllers, HEEx templates and contexts.
- Phoenix LiveView Learn how Phoenix LiveView builds interactive pages with server state over a websocket: mount, assigns, events, forms, PubSub updates and testing.
- Releases, Config and Deployment Learn how to configure and ship an Elixir app: config files, runtime.exs, mix release, clustering basics and observability with Telemetry.
- Performance and Common Pitfalls Avoid slow Elixir code: list append costs, binaries, N+1 queries in Ecto, unbounded mailboxes, large messages, and how to measure with Benchee.
- Security Basics Learn the main security traps in Elixir and Phoenix: atom exhaustion, unsafe deserialization, SQL fragments, CSRF, XSS and keeping secrets out of code.
Projects
- Project: Todo List with Pure Functions Build a todo list in Elixir with structs, Enum, pattern matching and formatted output, then run the same logic as a JavaScript stand-in in your browser.
- Project: Bank Account Process Build a bank account as an Elixir process: state in a GenServer, call and cast messages, error tuples for bad requests, plus a runnable JavaScript actor model.
- Project: Word Frequency Counter Build a word frequency counter in Elixir: stream a file line by line, split words with a regex, count with Enum.frequencies and sort the top results.
- Project: Chat Room with Processes Build a chat room from Elixir processes: a room process keeps subscribers, broadcasts messages and uses monitors to drop users who crash or leave.
- Project: Job Queue with Supervision Build a background job queue in Elixir: a queue process with back-pressure, workers that retry failed jobs and a supervisor that restarts crashes.
Practice and Review
- Practice Exercises with Solutions Practice Elixir with exercises on pattern matching, recursion, Enum, structs and processes. Try each one yourself, then compare with the solution.
- Elixir Interview Questions Prepare for an Elixir interview: clear answers on immutability, the BEAM, processes, OTP, pattern matching and tricky code snippets, with examples.
- Elixir Cheat Sheet A one-page Elixir cheat sheet: types, operators, pattern matching, Enum, strings, processes, OTP behaviours and the most useful Mix commands.