cult3

Getting started with Elixir

Feb 29, 2016

Table of contents:

  1. What is Erlang?
  2. What is Elixir?
  3. Installing Elixir
  4. Running Interactive Elixir
  5. Running Elixir scripts
  6. Conclusion

I think a lot of developers reach a stage in their career when they long for something different, something that will change their perspective, and something that will make it possible to build things that just weren’t possible with their current toolset.

For a while I’ve looked at languages such as Haskell, Clojure, or Scala as the next thing I would learn to build a whole new set of applications, but I was never really inspired to pick up a book and start learning.

I was looking for that inspiration when I heard about Facebook acquiring WhatsApp for $19 Billion.

Incredibly, WhatsApp had only 32 employees at the acquisition, but they were supporting 450 million users!

How could that even be possible?

WhatsApp is written in Erlang, a language developed by Swedish telecom company Ericsson in the 1980s.

But even with the inspiration of building something that hundreds of millions of people could use, I only briefly looked into Erlang.

What is Erlang?

Erlang was developed by Ericsson in the 1980s as a language for building out their telecoms system. Ericsson needed a telecom systems that was scalable and reliable and that could consistently provide service with little or no downtime.

A telecom system isn’t much use if it’s flakey, is easily overwhelmed with simultaneous calls, or if you need to hang up the phone in order for a system upgrade to take place!

Whilst Erlang was original created for telecoms systems, it is actually a general-purpose programming language. However, Erlang does benefit from all of the qualities that were required of a language created for a telecom system such as concurrency, scalability, fault-tolerance, distribution, and high availability.

Despite Erlang being original developed in the 1980s, it couldn’t be more relevant today. In the 1980s, only telecoms systems needed the reliability, availability, and fault-tolerance of Erlang. With the rise of the Internet over the last 10 years, we now have lots of different systems that require these characteristics including messaging, gaming, financial services, and media, all whilst serving hundreds of millions of requests per day and millions of concurrent users.

The requirements of Ericsson in the 1980s are now table stakes in our Internet connected world.

What is Elixir?

So if we’re talking about Erlang, what is Elixir?

Elixir is built on top of Erlang to provide support for metaprogramming with macros, polymorphism via protocols, and a generally more friendly syntax that looks a lot like Ruby.

Elixir is a functional, concurrent language, and runs on the Erlang Virtual Machine, and so it inherits the benefits of being distributed and fault-tolerant.

Elixir was created by José Valim, a long time Open Source contributor and a well respected member of the Ruby community. In creating Elixir, José goals were “to enable higher extensibility and productivity in the Erlang VM while keeping compatibility with Erlang’s tools and ecosystem”.

Installing Elixir

To get up-to-date instructions for installing Elixir on your platform of choice, take a look at the Installing Elixir page on the Elixir website.

To check to make sure you have Elixir installed correctly, run the following command from Terminal:

elixir -v

You should see the version of Elixir you are current running as the response to this command.

Running Interactive Elixir

The quickest way to get going with Elixir is to open up Interactive Elixir. If you are coming from a Ruby background, this is very similar to IRB.

From your Terminal run the following command:

iex

You should be dropped into a Interactive Elixir prompt where you can run Elixir code and have it be evaluated and returned.

The canonical first thing you do when exploring a new language is print “Hello world!”, and so we can do that here:

IO.puts("Hello world!")

Running Elixir scripts

Using something like iex is great for getting your feet wet, but you will soon want to start running code from a file.

The easiest way to do this is to save your Elixir code as a script file.

For example, create a new file called hello.exs with the following contents:

IO.puts("Hello world!")

Next, run the following command from Terminal:

elixir hello.exs

You should see the “Hello world!” message printed to the screen.

Conclusion

Today is the first part of a new series on Culttt exploring the Elixir language, the Erlang ecosystem, and what it takes to build applications and services in this new world.

When I first started learning Elixir I was pretty stoked at the thought of what I would be able to build. I think expanding your programming repertoire with new languages, tools, and perspectives is a really great way to expand your skills as a developer.

So if you’ve been looking to add a new language to your tool belt, or you are interested in learning Elixir and Erlang, I hope you will join me on this new adventure.

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.