Lesson 2 / 25

Installing Elixir: IEx, Mix and Hex

Install Elixir and Erlang, explore in IEx and create Mix projects.

The Elixir toolchain

Elixir needs Erlang/OTP underneath. Install both with a version manager such as asdf or mise (pinning versions in .tool-versions), or with your OS package manager; on Windows there are installers. IEx (Interactive Elixir) is the REPL: iex starts it, h Enum.map shows documentation, i value inspects a value, and iex -S mix loads your project. Mix is the build tool: mix new shop creates a project with mix.exs (project configuration and dependencies), lib/ for code and test/ for tests; mix compile, mix test, mix format and mix run are everyday commands. Use mix new shop --sup to generate an application with a supervision tree. Hex (hex.pm) is the package registry: add dependencies to deps in mix.exs with version requirements like {:jason, "~> 1.4"} and run mix deps.get; mix.lock pins exact versions. Documentation for every package is on HexDocs. For editors, use an Elixir language server (such as ElixirLS or the newer Expert project) in VS Code, Neovim or similar. Livebook provides interactive notebooks for learning and data work.

Creating and exploring a Mix project

Generate a supervised app, add a dependency and open IEx.

mise use erlang@27 elixir@1.18    # or: asdf install, using a .tool-versions file
elixir --version

mix new shop --sup                 # project with an application and supervisor
cd shop

# mix.exs (excerpt)
# defp deps do
#   [
#     {:jason, "~> 1.4"},
#     {:credo, "~> 1.7", only: [:dev, :test], runtime: false}
#   ]
# end

mix deps.get                       # fetch from Hex, writes mix.lock
mix test                           # run ExUnit tests
mix format                         # format all code
iex -S mix                         # IEx with the project loaded

# inside IEx:
#   h Enum.map         documentation
#   i "hello"           what is this value?
#   recompile()         reload changed code

A workshop with a librarian

Mix is your workshop bench where projects are built and tested; Hex is the librarian who fetches the right version of every tool you ask for; IEx is the scratch pad on the side for trying ideas.

Quick check: What does `mix new shop --sup` add compared with plain `mix new shop`?

  • A web server
  • A database
  • Static types
  • An application module with a supervision tree
Answer

An application module with a supervision tree — The --sup flag generates an Application that starts a supervisor.