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
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
$ 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
To install Elixir on Mac we can also use Homebrew, but for this
$ asdf update
$ asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
$ asdf install elixir 1.8.1
On
On
$ 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.
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
$ 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
$ 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 mix ecto.create
config/dev.exs
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.