Caio Bianchi

Getting Elixir And Phoenix Set Up

Today I want to share a rather pleasant experience I’ve been having in the world of web development, and that is working with Elixir.

Elixir is a language created on top of the Erlang VM (BEAM) by one of Ruby/Rails core contributors José Valim and is designed to scale on systems that require concurrent processes.

Phoenix is a web framework built on top of Elixir, think Rails for Elixir. And it’s been widely adopted by many Ruby developers as a way to overcome performance limitations with a familiar syntax and similar patterns.

Installing Elixir

Let’s get to it. This guide assumes that you’re using MacOS, although the same could be performed on an Unix or Windows-based machine.

First we need to get Elixir installed:

brew update && brew install elixir

Because Elixir is still on version 1.4 (as the time of this writing), there haven’t been many attempts at version managers yet, however if you need to manage multiple versions of Elixir, asdf is a good candidate and I recommend you to check it out.

If everything worked out, you should be able to verify if Elixir has been installed on your system by running: elixir --version

Elixir comes with an interactive mode included (think irb, jsc etc…)

iex

iex(1)> 40 + 2
42
iex(2)> "hello" <> " world"
"hello world" 

Installing Phoenix

When you install Elixir for the first time, you will need to get Hex installed. Hex is a package manager for Elixir and Erlang libraries (Again, think RubyGems, npm etc…)

Here’s the command to install Hex (If you already have it installed, it’ll just upgrade to the latest version):

mix local.hex

Now onto the interesting bits. Installing Phoenix is as easy as:

mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

Basically what this will do is, it will query Hex for the latest published version of Phoenix and make it available for use with Elixir.

Dependencies

Once you have Phoenix installed, there’s a couple of dependencies to it. And they’re pretty reasonable IMO: node and postgres.

To install both, simply run:

brew install node && brew install postgres

PS.: I have had issues with my postgresql install via Homebrew, because I forgot my username/password. Make sure you have those before you continue.

Creating your first Phoenix Project

Run: mix phoenix.new my_app and cd into my_app to browse the files.

You can access the console by running:

iex -S mix phoenix.server

Before starting up the server, you will need to create the DB:

mix ecto.create

And then run the server:

mix phoenix.server

Phoenix has a few preset tasks to help you get started and learn more about the framework:

mix phoenix.gen.channel  # Generates a Phoenix channel
mix phoenix.gen.html     # Generates controller, model and views for an HTML based resource
mix phoenix.gen.json     # Generates a controller and model for a JSON based resource

The above commands are somewhat equivalent to Rails’ scaffold

Hope this guide is helpful and if you have any questions, please let me know.

This project is maintained by caiobianchi