Elixir Get Started (IEx)
To run Elixir on your own computer you need two things: Erlang/OTP (the BEAM virtual machine and its standard libraries) and Elixir itself. Most installers bring both.
Installing
| System | Command |
|---|---|
| macOS (Homebrew) | brew install elixir |
| Ubuntu / Debian | sudo apt install elixir (often an older version) |
| Windows | the installer from elixir-lang.org, or choco install elixir |
| Any OS, several versions | a version manager such as asdf or mise (mise use elixir@1.19) |
| Docker | docker run -it --rm elixir:1.19 |
Check that it works:
$ elixir --version
Erlang/OTP 28 [erts-16.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]
Elixir 1.19.0 (compiled with Erlang/OTP 28)Installing gives you three commands:
| Command | Purpose |
|---|---|
iex |
Interactive Elixir, a shell (REPL) for trying expressions |
elixir |
runs a script file: elixir hello.exs |
mix |
the build tool: creates projects, compiles, runs tests, fetches packages |
Your first IEx session
Start iex in a terminal. Every expression you type is evaluated and its result is printed:
$ iex
Erlang/OTP 28 [erts-16.0] ...
Interactive Elixir (1.19.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 40 + 2
42
iex(2)> "elixir" |> String.upcase()
"ELIXIR"
iex(3)> h String.upcase
def upcase(string, mode \\ :default)
Converts all characters in the given string to uppercase...
iex(4)> i "hello"
Term
"hello"
Data type
BitString
...Useful IEx helpers:
| Helper | What it does |
|---|---|
h Enum.map |
shows the documentation of a module or function |
i value |
shows information about a value (type, description) |
v() / v(2) |
the last result / the result of line 2 |
r MyModule |
recompiles and reloads a module |
c "file.ex" |
compiles a file into the session |
recompile() |
recompiles the current Mix project (iex -S mix) |
Ctrl+C twice |
quits |
Tip: In IEx, a
TABpress completes module and function names: typeEnum.and press TAB to list everything inEnum.
Scripts: .exs and .ex files
Elixir uses two file extensions:
.exsfiles are scripts: they are compiled in memory and executed right away. Tests and configuration use.exs. All examples in this course are scripts..exfiles are compiled to.beambytecode files by Mix. Your application code lives in.exfiles.
Save the following as hello.exs and run it with elixir hello.exs:
name = "Ada"
IO.puts("Hello, #{name}!")
IO.puts("Running Elixir #{System.version()}")Hello, Ada!
Running Elixir 1.19.6
Values are printed with IO.inspect
In IEx every result is displayed automatically. In a script nothing is shown unless you print it. IO.puts is for text; IO.inspect shows any value the way IEx would, and returns it unchanged, so you can drop it into the middle of an expression:
IO.inspect([1, 2, 3])
IO.inspect(%{language: "Elixir", year: 2011})
IO.inspect({:ok, "saved"})
total = IO.inspect(10 * 4, label: "product") + 2
IO.puts(total)[1, 2, 3]
%{year: 2011, language: "Elixir"}
{:ok, "saved"}
product: 40
42
Note: The map was printed as
%{year: 2011, language: "Elixir"}, not in the order it was written. Maps are unordered, so never rely on the order in which their keys are printed.
Comments
A comment starts with # and runs to the end of the line. There are no block comments; documentation uses module attributes such as @doc (covered later).
# This whole line is a comment
price = 20 # a comment after code
IO.puts(price * 2)40
Common mistakes
- Using
IO.putson a list or map:IO.putsexpects text (or a list of characters/strings). UseIO.inspectfor data structures. - Forgetting the parentheses habit:
IO.puts "hi"works, but writing calls with parentheses (IO.puts("hi")) is the style the formatter prefers for most code. - Running
elixir file.exon application code that belongs in a Mix project: usemix runoriex -S mixthere.
Test yourself
Which command starts the interactive Elixir shell?
- elixir
- mix shell
- iex
- erl
What is the difference between .exs and .ex files?
.exsfiles are compiled to .beam files,.exfiles are not.exsfiles are scripts executed in memory;.exfiles are compiled to bytecode by Mix- There is no difference
.exfiles can only contain tests
What does IO.inspect(10 * 4, label: "product") return?
:ok- The string "product: 40"
40nil