Richard Jonas's


Elixir - the first steps 1


In this blog series I want to share my first steps in Elixir. Okay, maybe not the very first steps since I have been watching this very exciting language for some months. Now I have some kind of picture in my mind, how I can develop something more interesting than Hello World.

To be serious and effective we need to set a big enough goal which we can implement. I have always been on the server side, so let us remain there for now.

Let us build a webshop

Okay, at first it seems to be a brave promise, but with big goals we can achieve a lot, right? So let us start to build an Elixir project which uses Riak as a database backend. And see how we can write an HTML5, Elixir, Riak (NoSQL) application and try not just read and speak about there things, but actually do.

What is Elixir?

Elixir is a dynamic, functional language which runs on Erlang ecosystem. The inventor is José Valim who found interesting to port a Ruby-like language on Erlang VM. For my opinion that is a nice idea, since Erlang syntax is a bit old, sometimes not sexy for the newcomers. But in my opinion the Erlang VM is a great thing if we are speaking about scalability and robustness.

Elixir files are written into files with .ex extensions, after compilation they will be .beam files as you got used to in Erlang. Then those beam files are loaded by the Erlang VM and run them. Let install Elixir.

kiex the Elixir's kerl

In Erlang with kerl we can manage different Erlang installations in a single environment. It is necessary since we want to see how our project can be built with older Erlang versions. The same tool was made for Elixir, so let us install that at first.

\curl -sSL | bash -s

This will install kiex into ~/.kiex directory. By running kiex we can see

$ ~/.kiex/bin/kiex
kiex commands:
    list                      - shows currently installed Elixirs
    list known                - shows available Elixir releases
    list branches             - shows available Elixir branches
    install          - installs the given release version
    use  [--default] - uses the given version for this shell
    shell            - uses the given version for this shell
    default          - sets the default version to be used
    selfupdate                - updates kiex itself
    implode                   - removes kiex and all installed Elixirs
    reset                     - resets default Elixir version to null

With list known we can get the available Elixir versions from the repo.

$ ~/.kiex/bin/kiex list known
Getting the available releases from

Known Elixir releases:

We can install the latest version by ~/.kiex/bin/kiex install 1.2.1 elixir-1.2.1. The only thing you need to do is to source the commands what kiex use elixir-1.2.1 emits. From that time you can execute Elixir environment by iex.

Mix, finally

The very great thing in Elixir is that, it is shipped with a build environment called Mix. In Erlang we have been looking for great build environments, and finally rebar and were born. In Elixir that problem is solved from the first day. Elixir contains a build tool called mix. With mix help we can see all the possibilities we can do with this tool. Let us create a new project. With mix help new see how to create new projects.

$ mix new webshop --app webshop --sup --module Store
* creating
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/webshop.ex
* creating test
* creating test/test_helper.exs
* creating test/webshop_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd webshop
    mix test

Run "mix help" for more commands.

In the mix.exs there is the spine of the project, it is the application descriptor. It contains the Erlang-style application descriptor, the applications to be started and the dependencies. mix.exs is an Elixir module (!).

defmodule Store.Mixfile do
  use Mix.Project

  def project do
    [app: :webshop,
     version: "0.0.1",
     elixir: "~> 1.1",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]

  def application do
    [applications: [:logger],
     mod: {Store, []}]

  defp deps do

It says that the project requires Elixir 1.1 at least, and it starts logger application. There is no dependencies. In the config/config.exs we can specify configuration values for the applications. Elixir defines environment when it is speaking about projects. There are dev, test, prod environments and with

MIX_ENV=dev iex -S mix

we can run the application in dev environment. Also we can run the tests by mix test, or can just compile the application by mix compile. With mix run or iex -S mix we can run the application. The later runs the application in console mode, so we can inspect what is happening during running the application.

So now we have an Elixir environment, we know how to install different Elixir versions in the same OS, we also create a project with mix. After that we can do some programming work.

First steps

Mix generated some codes for us in order that we can start easier. It generated a module file, a test file and a config file. Let us check them.

defmodule Store do
  use Application

  def start(_type, _args) do
    import Supervisor.Spec, warn: false

    children = [
      # worker(Store.Worker, [arg1, arg2, arg3]),

    opts = [strategy: :one_for_one, name: Store.Supervisor]
    Supervisor.start_link(children, opts)

If you check the source code of Application module in Elixir you can see that use Application defines a default stop function and also puts the behaviour attribute in the code. So we need to implement the start function already in order that the compiler won't complain.

In the Supervisor.Spec module we can find convenience functions for defining supervision tree element, like supervisors, children and workers. With the worker function it is easier to define a worker child than writing complex supervisor specs by heart. One way or other the start function should return an {:ok, pid} in case of success. In the background the Erlang application controller will collect that pid and insert in in an ETS table, so it won't be forgotten and also this process will be monitored by the application controller.

To put it all together, it is a very basic Elixir application to show how an Erlang-style application can be created. I like the simplicity of the code, there is much less boilerplate code which is fine. In the background however we need to take care what happens with the Erlang OTP processes.

Next PostNewer Post Previous PostOlder Post Home


  1. interesting piece of information, I had come to know about your web-page from my friend pramod, jaipur,i have read atleast eight posts of yours by now, and let me tell you, your blog gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a million once again, Regards Guidewire training in hyderabad

  2. Very informative piece of article, this blog has helped me to understand the concept even better. Keep on blogging.
    msbi training in chennai

  3. Great content thanks for sharing this informative blog which provided me technical information keep posting.
    webshere training in chennai

  4. Tools are the best thing which can be easy downloaded from Techubi Tools

  5. Best Digital Marketing company Anantapur

    helpful information, thanks for writing and share this information

  6. Good Information about microsoft, thanks for sharing nice post on MSBI Online Training

  7. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.
    HTML5 courses in chennai

  8. Hey, Your post is very informative and helpful for us.
    In fact i am looking this type of article from some days.
    Thanks a lot to share this informative article.
    QuickBooks Training in Hyderabad