1 Interactive Elixir

Welcome!

In this tutorial we are going to show you how to get started with Elixir. This chapter will cover installation and how to get started with the Interactive Elixir shell called IEx.

If you find any errors in the tutorial or on the website, please report a bug or send a pull request to our issue tracker. If you suspect it is a language bug, please let us know in the language issue tracker.

Our requirements are:

  • Erlang - Version 17.0 onwards
  • Elixir - Version 0.15.0 onwards

Let's get started!

1.1 Installers

Elixir provides the installers for:

Not running on the platforms above? You can still get Elixir from different distributions, using a Precompiled package or Compiling from Source.

1.2 Distributions

Elixir is available on the following distributions:

  • Homebrew for Mac OS X
    • Update your homebrew to latest with brew update
    • Install Elixir: brew install elixir
  • Fedora 17+ and Fedora Rawhide
    • sudo yum -y install elixir
  • Arch Linux (on AUR)
    • yaourt -S elixir
  • openSUSE (and SLES 11 SP3+)
    • Add Erlang devel repo with zypper ar -f obs://devel:languages:erlang/ erlang
    • Install Elixir: zypper in elixir
  • Gentoo
    • emerge --ask dev-lang/elixir
  • Chocolatey for Windows
    • cinst elixir
  • FreeBSD
    • From ports: cd /usr/ports/lang/elixir && make install clean
    • From pkg: pkg install elixir

Those distributions will likely install Erlang automatically for you too. In case they don't, check the 1.5 Installing Erlang section.

1.3 Precompiled package

Elixir provides a precompiled package for every release. First install Erlang and then download and unzip the Precompiled.zip file for the latest release.

Once the release is unpacked, you are ready to run the elixir and iex commands from the bin directory. It is recommended that you add Elixir's bin path to your PATH environment variable to ease development.

1.4 Compiling from source (Unix and MinGW)

You can download and compile Elixir in few steps. The first one is to install Erlang.

Next you should download the latest release, unpack it and then run make inside the unpacked directory.

Note: if you are running on Windows, read this page on setting up your environment for compiling Elixir: https://github.com/elixir-lang/elixir/wiki/Windows

After compiling, you are ready to run the elixir and iex commands from the bin directory. It is recommended that you add Elixir's bin path to your PATH environment variable to ease development:

$ export PATH="$PATH:/path/to/elixir/bin"

In case you are feeling a bit more adventurous, you can also compile from master:

$ git clone https://github.com/elixir-lang/elixir.git
$ cd elixir
$ make clean test

If the tests pass, you are ready to go. Otherwise, feel free to open an issue in the issues tracker on Github.

1.5 Installing Erlang

The only prerequisite for Elixir is Erlang, version 17.0 or later, which can be easily installed with Precompiled packages. In case you want to install it directly from source, it can be found on the Erlang website or by following the excellent tutorial available in the Riak documentation.

For Windows developers, we recommend the precompiled packages. Those on a UNIX platform can probably get Erlang installed via one of the many package distribution tools.

After Erlang is installed, you should be able to open up the command line (or command prompt) and check the Erlang version by typing erl. You will see some information as follows:

Erlang/OTP 17 (erts-6) [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]

Notice that depending on how you installed Erlang, Erlang binaries won't be available in your PATH. Be sure to have Erlang binaries in your PATH, otherwise Elixir won't work!

1.6 Interactive mode

When you install Elixir, you will have three new executables: iex, elixir and elixirc. If you compiled Elixir from source or are using a packaged version, you can find these inside the bin directory.

For now, let's start by running iex (or iex.bat if you are on Windows) which stands for Interactive Elixir. In interactive mode, we can type any Elixir expression and get its result straight away. Let's warm up with some basic expressions:

Interactive Elixir - press Ctrl+C to exit (type h() ENTER for help)

iex> 40 + 2
42
iex> "hello" <> " world"
"hello world"

It seems we are ready to go! We will use the interactive shell quite a lot in the next chapters to get a bit more familiar with the language constructs and basic types, starting in the next chapter.