Subscribe now

Introduction to Elixir [02.13.2017]

Welcome to the first episode of DailyDrip's new Elixir topic. In this series, we'll walk through learning Elixir from the ground up. Once we've got a firm footing in the language, we'll move on to building a large-scale production web application using the Phoenix Framework. We'll also explore a variety of other topics as we go.

What is Elixir and why are you interested in learning it?

In the words of the Elixir website, Elixir is a dynamic, functional language designed for building scalable and maintainable applications.

It leverages the Erlang Virtual Machine, which has been used for building high-traffic, low-latency applications ranging from telephone switches to the WhatsApp backend and Heroku's routing layer.

In general, I find Elixir interesting for three reasons:

  • I find the Actor Model of computation very compelling, and Erlang's where it has had the most time to develop.
  • Since it's built upon the Erlang VM, you can use it to write extremely reliable, distributed, concurrent systems.
  • I find the syntax pleasant, and the tooling outstanding.

I've been using it now for over three years and it has been an amazing experience that I'm eager to share.

Who am I and why am I qualified to take you along as I learn it?

My name is Josh Adams. I was the CTO of a software development consultancy, Isotope11, for nearly a decade. I've built large-scale systems in everything ranging from Ruby and JavaScript to Erlang/Elixir and Java. I've built financial systems that handled billions of dollars worth of transactions each year and search systems for government agencies. I've also taught people Elixir for three and a half years, and built a few production systems with it.

Assuming you're convinced by all of this, let's get started learning.


Installing Erlang

The only prerequisite for installing Elixir is Erlang, since Elixir runs on the Erlang VM.

Installing Erlang is exceedingly simple. There are instructions and downloads available at the main Erlang site, but I think the easiest way to get started is to download a precompiled package provided by the fine folks at Erlang Solutions.

There are downloads for various operating systems there, if you want to just fetch one and install it. You could also install with your package manager.

On a Mac, you can install it with brew:

brew install erlang

Or on Windows, you can use chocolatey:

choco install erlang

I personally like to be able to easily run different versions of my programming languages, and for this I use the asdf version manager.

You can install asdf by cloning it into ~/.asdf:

# Feel free to check for a new version, but v0.2.1 is the latest as of this
# writing.
git clone ~/.asdf --branch v0.2.1

Then load it in your shell:

# For Mac OSX and bash
echo -e '\n. $HOME/.asdf/' >> ~/.bash_profile
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bash_profile

# For Ubuntu or other linux distros, and bash
echo -e '\n. $HOME/.asdf/' >> ~/.bashrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc

Once that's done, you can use asdf to install all kinds of programming languages. They each have plugins. To install the asdf-erlang plugin, run the following:

asdf plugin-add erlang

Then you can install it with:

# List all of the versions of Erlang available:
asdf list-all erlang
# As of this writing, 19.2 is the latest release.  Install it:
asdf install erlang 19.2
# To make it the global default version of Erlang:
asdf global erlang 19.2

Verifying your Erlang installation

You can verify that the installation worked by entering an Erlang Shell. At a console prompt, type erl and you should be greeted by something that looks like this:

jadams ~ λ erl
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.2  (abort with ^G)

If you see that, congratulations - you've installed Elixir's entire prerequisite list. Type q(). and press Enter to exit.


Installing Elixir

You can find an Elixir distribution to install by visiting the Elixir site's install page.

Again, you can install on a Mac trivially:

brew install elixir

Or on Windows, you can use chocolatey:

choco install elixir

If you would rather use asdf, you can first install the asdf-elixir plugin:

asdf plugin-add elixir

Then install Elixir with:

# List all of the versions of Elixir available:
asdf list-all elixir
# As of this writing, 1.4.0 is the latest release.  Install it:
asdf install elixir 1.4.0
# To make it the global default version of Elixir:
asdf global elixir 1.4.0

Firing up iex

To verify that your Elixir install was successful, type iex. Assuming you did everything right, you should be greeted with a prompt that looks like this:

jadams ~ λ iex
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

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

Just press Ctrl+C twice to close the shell out. You can also exit the shell by pressing Ctrl+\ if you want a single keystroke :)


Today, we walked through installing Erlang and Elixir. I've added some links in the resources section to encourage you to get involved in the community. See you soon!


Where can you get more help with Elixir?

What is the Actor Model?

I had a subscriber ask me for a succinct explanation of the Actor Model. Without going into it too deeply, I'd describe it thusly:

An actor is an object that has its own lifecycle and lives concurrently. An actor communicates with another actor by placing a message in his mailbox. The second actor can then read the mailbox in his preferred order, and respond to the messages at his leisure. This is a large part of the Erlang 'shared nothing' model, and one of the core concepts that leads to its fault tolerance and ease of distribution.

If you want more details, there's an excellent video that MSDN's Channel 9 did interviewing Carl Hewitt, Erik Meijer, and Clemens Szyperski on the actor model.