Step 2: Use Libraries

Learn how to add external libraries in Elixir.

Elixir comes with a bunch of libraries preinstalled. Some are written in Elixir, and others in Erlang.

The first port of call is the Elixir documentation. Often, we’ll find a built-in library that does what we want.

Next, we see if any standard Erlang libraries do what we need. This isn’t a simple task. We visit http://erlang.org/doc/ and look in the left sidebar for “Application Groups.” Here, libraries are sorted by top-level category.

If we find what we’re looking for in either of these places, we’re all set because all these libraries are already available to the application. But if the built-in libraries don’t contain what we need, we’ll have to add an external dependency.

Finding an external library

Ruby has RubyGems, Python has pip, Node.js has npm and Elixir has the Hex package manager.

Visit https://hex.pm and search the list of Elixir packages that integrate nicely with a Mix-based project.

If all else fails, Google and GitHub will work. We can search for terms such as “elixir http client” or “erlang distributed logger,” and the needed libraries are likely to turn up.

In our case, we need an HTTP client. We find that Elixir has nothing built in, but hex.pm has a number of HTTP client libraries.

HTTPoison looks like a good option. So, how do we include it in our project?

Adding a library to our project

Mix takes the view that all external libraries should be copied into the project’s directory structure. The good news is that it handles all this for us. We just need to list the ...