guide, home

Getting Started with Elixir Phoenix Development

The Phoenix Framework is a productive web framework that does not compromise speed or maintainability.  

We're going to cover how to get a development environment up and running on macOS for Phoenix Framework.

ASDF

If you are a ruby developer, you've probably used rvm, or rbenv to manage ruby versions on your local machine.  asdf is a tool that can manage multiple language runtime versions on a per-project basis.  

Install via homebrew:

$ brew install asdf

And install dependencies:

brew install \
  coreutils automake autoconf openssl \
  libyaml readline libxslt libtool unixodbc \
  unzip curl wxmac fop

We're now ready to use asdf to manage versions.

To see all available asdf commands: https://asdf-vm.com/#/core-commands

Erlang and Elixir

Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.

Let's add the Erlang Elixir asdf plugins:

$ asdf plugin-add erlang https://github.com/asdf-vm/asdf-erlang.git 
$ asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git

OR

$ asdf plugin-update erlang 
$ asdf plugin-update elixir 

Then install Erlang:

$ asdf list-all erlang # lists the available versions

$ asdf install erlang 21.3.6

... 

Erlang 21.3.6 has been installed. Activate globally with:

    asdf global erlang 21.3.6

Activate locally in the current folder with:

    asdf local erlang 21.3.6

We'll repeat the same steps above for Elixir

$ asdf list-all elixir
$ asdf install elixir 1.8.1
$ asdf global elixir 1.8.1      

NodeJS and PostgreSQL

Before going too much further, ensure that you have NodeJS, and PostgreSQL installed and ready to be used on your machine.  We're going to skim over this section as you may already have these things installed.

Elixir Tools

Here are some basic descriptions for OTP and Mix that will help when getting started learning about the Phoenix Framework and Elixir ecosystems:

  • OTP (Open Telecom Platform) is a set of libraries that ships with Erlang. Erlang developers use OTP to build robust, fault-tolerant applications.
  • Mix is a build tool that ships with Elixir that provides tasks for creating, compiling, testing your application, managing its dependencies and much more.
  • Hex is package manager for the Erlang ecosystem.

Installing Phoenix

Now that our development environment is ready, let's install Hex

$ mix local.hex

And the Phoenix Framework:

$ mix archive.install hex phx_new 1.4.3

Webpack

A note about webpack before we begin: Phoenix will use webpack for asset management by default. Webpack’s dependencies are installed via the node package manager, not mix. Phoenix will prompt us to install them at the end of the mix phx.new task. If we say “no” at that point, and if we don’t install those dependencies later with npm install, our application will raise errors when we try to start it, and our assets may not load properly. If we don’t want to use webpack at all, we can simply pass --no-webpack to mix phx.new.

Ecto

A note about Ecto: Ecto allows our Phoenix application to communicate with a data store, such as PostgreSQL, MySQL, or MSSQL. If our application will not require this component we can skip this dependency by passing the --no-ecto flag to mix phx.new. This flag may also be combined with --no-webpack to create a skeleton application.

Create new Application

We're going to use mix to create our new application:

$ mix phx.new phxapp

...

Fetch and install dependencies? [Yn] y

...

We are almost there! The following steps are missing:

    $ cd phxapp

Then configure your database in config/dev.exs and run:

    $ mix ecto.create

Start your Phoenix app with:

    $ mix phx.server

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phx.server

Our app is now setup, so let's follow the instructions:

$ cd phxapp && mix ecto.create

...

Compiling 13 files (.ex)
Generated phxapp app
The database for Phxapp.Repo has been created

Then start the server:

$ mix phx.server

This will compile webpack assets, and load the Phoenix application at http://localhost:4000 where you'll see the Phoenix starter page:

Conclusion

Developing in Elixir and Erlang is not a very simple set up, and thanks to the great community and tools available it has become fairly painless to get going.  We hope you found this guide helpful and you're now on your way to building applications with Elixir Phoenix.

Author image

About Troy Martin

Husband, Father & Software Developer who tinkers with writing and building things.
  • Hamilton, ON