Installing and Using Elixir and Phoenix Framework

Elixir is a functional, concurrent and dynamic programming language, projected to build scalable applications easily and productively.

On thios post we will have a look at how to install Elixir and it’s dependencies. We will also see how to install Phoenix Framework, a web web framework written with Elixir.

Installing Elixir

Elixir runs under Erlang’s Virtual Machine (BEAM). Erlang is a functional and concurrent programming language, created by Ericsson with the aim of creating scalable, fault tolerant, distributed and high availability applications.

Therefore, to install Elixir, it is necessary to install the Erlang first.

To install Erlang on MacOS we can use Homebrew (a MacOS package manager), MacPorts or `asdf` (version manager for diefferents languages and tools). For this post, we will use Homebrew:

$ brew update
$ brew install erlang

The Erlang installation on Linux is quite simple. We can use the Package Manager of Linux distribution we are using. Below are the commands for installing on Ubuntu/Debian. The process is the same for other distributions:

$ sudo apt-get update
$ sudo apt-get install erlang

For Windows installation, visit https://www.erlang.org/downloads and download the installer (eg: https://erlang.org/download/otp_64_21.3.exe), double click and follow the steps.

With Erlang installed, we are ready to install Elixir. The process is similar with Erlang Installation.

To install Elixir on Mac we can also use Homebrew, but for this post I will use `asdf`. With `asdf` we can install different versions of the same tool. Visit: https://blog.psantos.dev/installing-and-start-using-asdf-on-macos-high-sierra/ to see how to install `asdf` on MacOS. `asdf` can also be used on Linux (Debian/Ubuntu or other distributions),. So, the process to install elixir on MacOS/Linux using `asdf` is similar:

$ asdf update
$ asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
$ asdf install elixir 1.8.1

On Windows we use a installer. To download the installer visit: https://repo.hex.pm/elixir-websetup.exe, open it with double click and follow the steps (next.. next… next… finish 🙂 ).

On Windows we can also use a package manager like Chocolatey to intall Elixir. With Chocolatey installed, just run:

$ cinst elixir

You can find instructions how to install and use Chocolatey on Windows on the following link: https://chocolatey.org/install.

After installing Elixir, we can test the installation running on the Terminal (MacOS ou Linux) or on Windows Command Line the following command:

$ iex

If everything is OK, we will have an image as below (MacOS):

With that result, we can celebrate 🎉. We installed Elixir successfully.

And we can create our first Elixir project running:

$ mix new super_project

Installing Phoenix Framework

Phoenix Framework is a web framework written in Elixir. Part of Phoenix Core Team comes from Ruby/Rails community, so greats ideas where also taken from this community and aplied to Phoenix. Phoenix Framework, like Ruby on Rails, is a framework with a focus on developer’s productivity. It offers a default structure for the application generated, comes with code generators, routing file and supports relational databases like PostgreSQL (by default), MySQL/MariaDB.

Phoenix Framework promotes the best security practices by default and comes with testing tools.

Since we already have Erlang and Elixir installed, we can install Phoenix now and start creating our Web applications. The process is the same for all operating systems. We just need to have Elixir installed.

NOTE: Phoenix Framework uses Webpack to compile assets. So we need to have Nodejs installed before installing Phoenix. Instructions e Downloads for Nodejs can be found here: https://nodejs.org/en/download/.

Before we install Phoenix, we also need to install Hex, the Package manager for Elixir. The same way we have composer for PHP, gem/bundler for Ruby, npm for JavaScript… and so on. On the Terminal run:

$ mix local.hex

With HEX installed, we can install Phoenix. Run on the Terminal:

$ mix archive.install phx_new

NOTE: By default, Phoenix uses PostgreSQL. So, we need to have PostgreSQL installed.

With Phoenix installed, we can now generate our forst web application:

$ mix phx.new hello

As I said above, Phoenix uses PostgreSQL by default, if we want to switch to MySQL for example, we need to run the command with `–database` flag:

$ mix phx.new hello --database mysql

Running the above command, Phoenix will create the default structure of our application inside the folder with the name of our application (“hello” in our case). Enter the folder and run the following commands:

$ cd hello
$ mix ecto.create

As instructed by the installer itself, we need to run the command mix ecto.create to create our database. If you receive a database connection error, check the config/dev.exs file and change the settings.

Now we can run our server:

$ mix phx.server

With the server running, we can access our new application visiting: http://localhost:4000:

And voilá!!! We have our first Phoenix application running 😀

In the next posts, we will see how to create controllers, views, and templates… see you then.

,,