Introduction
Welcome!
In this tutorial, we are going to teach you about Elixir fundamentals - the language syntax, how to define modules, how to manipulate the characteristics of common data structures, and more. This chapter will focus on ensuring that Elixir is installed and that you can successfully run Elixir’s Interactive Shell, called IEx.
Our requirements are (see elixir -v
):
- Elixir 1.5.0 onwards
- Erlang/OTP 19 onwards
Let’s get started!
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.
The Elixir guides are also available in EPUB format:
Installation
If you haven’t yet installed Elixir, visit our installation page. Once you are done, you can run elixir --version
to get the current Elixir version.
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. Let’s warm up with some basic expressions.
Open up iex
and type the following expressions:
Erlang/OTP 20.0 [64-bit] [smp:2:2] [...]
Interactive Elixir (1.9.0) - press Ctrl+C to exit
iex(1)> 40 + 2
42
iex(2)> "hello" <> " world"
"hello world"
Please note that some details like version numbers may differ a bit in your session; that’s not important. From now on iex
sessions will be stripped down to focus on the code. To exit iex
press Ctrl+C
twice.
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.
Note: if you are on Windows, you can also try
iex.bat --werl
which may provide a better experience depending on which console you are using.
Note: if you want to find and execute a given script in PATH so it will be loaded in
iex
use:iex -S SCRIPTNAME
. Later you’ll learn about Mix, Elixir’s build tool, and how you can compile and load entire applications withiex -S mix run
. See Supervisor and application for more details.
Running scripts
After getting familiar with the basics of the language you may want to try writing simple programs. This can be accomplished by putting the following Elixir code into a file:
IO.puts "Hello world from Elixir"
Save it as simple.exs
and execute it with elixir
:
$ elixir simple.exs
Hello world from Elixir
Later on we will learn how to compile Elixir code (in Chapter 8) and how to use the Mix build tool (in the Mix & OTP guide). For now, let’s move on to Chapter 2.
Asking questions
When going through this getting started guide, it is common to have questions; after all, that is part of the learning process! There are many places maintained by the community where you can ask questions, here are some of them:
- Official #elixir-lang on freenode IRC
- Elixir Forum
- Elixir on Slack
- Elixir on Discord
- elixir tag on StackOverflow
When asking questions, remember these two tips:
-
Instead of asking “how to do X in Elixir”, ask “how to solve Y in Elixir”. In other words, don’t ask how to implement a particular solution, instead describe the problem at hand. Stating the problem gives more context and less bias for a correct answer.
-
In case things are not working as expected, please include as much information as you can in your report, for example: your Elixir version, the code snippet and the error message alongside the error stacktrace. Use sites like Gist to paste this information.