TExcelTAD
Home › Elixir › Elixir Get Started (IEx)

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:

bash
$ 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:

text
$ 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 TAB press completes module and function names: type Enum. and press TAB to list everything in Enum.

Scripts: .exs and .ex files

Elixir uses two file extensions:

  • .exs files are scripts: they are compiled in memory and executed right away. Tests and configuration use .exs. All examples in this course are scripts.
  • .ex files are compiled to .beam bytecode files by Mix. Your application code lives in .ex files.

Save the following as hello.exs and run it with elixir hello.exs:

elixir · try it yourself
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:

elixir · try it yourself
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).

elixir · try it yourself
# This whole line is a comment
price = 20   # a comment after code
IO.puts(price * 2)
40

Common mistakes

  • Using IO.puts on a list or map: IO.puts expects text (or a list of characters/strings). Use IO.inspect for 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.ex on application code that belongs in a Mix project: use mix run or iex -S mix there.

Test yourself

Which command starts the interactive Elixir shell?

  1. elixir
  2. mix shell
  3. iex
  4. erl

What is the difference between .exs and .ex files?

  1. .exs files are compiled to .beam files, .ex files are not
  2. .exs files are scripts executed in memory; .ex files are compiled to bytecode by Mix
  3. There is no difference
  4. .ex files can only contain tests

What does IO.inspect(10 * 4, label: "product") return?

  1. :ok
  2. The string "product: 40"
  3. 40
  4. nil