How do I load an elixir library in a simple elixir script?

There are no global package installs in Elixir like there are in Ruby. While it might be technically possible to compile the dependencies into .beam files and add them to the load path of your scripts (like the article you linked to does), if you want behavior somewhat similar to Ruby, I would recommend you to make use of mix run to run arbitrary scripts with all the project dependencies loaded.

Create one global mix project with all the dependencies you want specified in mix.exs, write your code in any .exs file (doesn't have to be in the same folder), and execute it by

cd /path/to/mix/project && mix run /path/to/.exs

You could even create a wrapper shell script to automatically do the above by just invoking my-elixir script.exs.

(I regularly do this while testing code for answers here on StackOverflow which use some common dependencies like HTTPoison and/or Poison.)

Tags:

Elixir