Creating an Elixir project and running code is straightforward thanks to good conventions and solid tooling. It's impressive when you consider what's going on under the hood: compiling Elixir files into byte code that is then run on an instance of the 20-year-old, battle-proven Erlang VM.

In this video from our new Developing With Elixir course, we walk through setting up a project and various ways to run Elixir files:

Here's a quick recap of the commands we used in the video to run an Elixir file:

  1. Run the elixir command with the relative path of the Elixir file:

    elixir lib/servy.ex
    

    The file gets compiled into bytecode (in memory) and then run on an Erlang virtual machine.

  2. Fire up an iex (Interactive Elixir) session and then use the c helper function to compile and run the file:

    iex
    
    iex> c "lib/servy.ex"
    

    The c helper function compiles the given file in memory, the module (Servy in this case) is loaded into the session, and any code outside of the module is interpreted.

    To exit the iex session, press Ctrl+C twice.

  3. Alternatively, you can tell iex to interpret an Elixir file while starting by passing the relative path of the file:

    iex lib/servy.ex
    
  4. When you start a standard iex session, it doesn't know about the paths and dependencies of a mix project. So to start a session in the context of a project, you need to pass the -S mix option:

    iex -S mix
    
  5. Finally, to recompile a module while in iex, use the r helper function:

    iex> r Servy
    

    This recompiles and reloads the given module, which is Servy in this case.

Build a complete Elixir app from scratch!

Save yourself time, frustration, and the perils of coding by coincidence by learning all the essentials of developing Elixir apps upfront in our Developing With Elixir course.