Installing Elixir and Phoenix
Having a little bit of free time tonight, I thought I’d look into Elixir and Phoenix. Elixir is a relatively new language that runs on the Erlang VM and Phoenix is the web framework for it.
There has been a lot of talk on Hacker News about Elixir in the past couple of years. WhatsApp famously runs Erlang in its backend and it was a key factor for it scaling so well. This was the main reason I’ve been interested in it for a while.
A while back, I saw the Learn With Me: Elixir blog series by Kevin Peter and learned more about Elixir. I recommend the series — I’ve only gone through the first few chapters and it already gives me a very good background on Elixir.
So I thought I’d install Elixir to try things out a bit.
Elixir Installation — with version managers
So my goal is to install Elixir (and Erlang VM). I’ve been working with nodejs for the past couple of years and I really like the way nvm
handles installing and version-switching of node in your environment. Coming from the PHP world prior to that, this is a huge improvement, as PHP is installed at the system level and is difficult to adjust.
Because of this, I steered away from the default instruction of installing with the OS’s package manager (brew
/pacman
/yum
/apt-get
/etc.), and looked for something similar to nvm
.
Elixir’s install page mentions a few version managers. After reading over their READMEs I decided to go with kerl and kiex. kerl
is an Erlang version manager and kiex
is built to be similar to kerl. Both are fairly similar to nvm
in concept and usage, which is something I want.
In an effort to stay away from the system’s package manager, I install kerl
by simply downloading it and moving it to a directory in my home:
curl -O [https://raw.githubusercontent.com/kerl/kerl/master/kerl](https://raw.githubusercontent.com/kerl/kerl/master/kerl)
chmod a+x kerl
mkdir ~/.kerl
mv kerl ~/.kerl
Then I edit my .bashrc
file to add this line:
export PATH=$HOME/.kerl:$PATH
Now kerl
is available on my bash.
Next, installing kiex
is also easy, and is similar to nvm
how you run a script from their repo:
curl -sSL [https://raw.githubusercontent.com/taylor/kiex/master/install](https://raw.githubusercontent.com/taylor/kiex/master/install) | bash -s
Now both kerl
and kiex
are ready!
Installing a version of Erlang VM and Elixir
The thing to note about installing the Erlang VM is you have to build it. If you run kerl list releases
and then simply run kerl install 21.2
you’ll run into an error saying it’s not built. So you’ll want to run:
kerl build 21.2
This will begin a download of the Erlang source and compile it. It will take a while. (On my laptop it took 25 minutes. The download was 80+mb and took a few minutes, and the rest was the compile.) The lack of a progress bar can be concerning during compile, so if you’re wondering if it’s doing anything at all, you can tail the build log in a separate terminal to see what it’s doing:
tail -f ~/.kerl/builds/21.2/otp_build_21.2.log
The nice thing about our setup now is that since we placed the kerl
script in ~/.kerl
, the location it’s built is automatically within the directory, similar to how nvm
operates.
After build completes, we can do a kerl install:
kerl install 21.2 ~/.kerl/installs/21.2
Once installed, you’ll have to activate it:
. ~/.kerl/installs/21.2/activate
Both of the last two steps should be really fast.
Note: Use kerl_deactivate
to deactivate the installation at any time. Use kerl active
to verify the activated installation at any time.
Now we’re ready to install Elixir with kiex
. We can use kiex list known
to see list of versions, and install the latest like follow:
kiex install 1.8.1
Now a specific Elixir version is installed in your current environment. Finally, to switch to it to use it:
kiex use 1.8.1
And that’s it! Commands such as elixir
, iex
and mix
should now be available on your terminal.
Phoenix — the fastest, least dependencies way to get a server running (to get a feel for it)
Phoenix framework is really full-featured, so naturally it comes with things like working with Postgres by default and wants you set up a database etc. to get going.
If you want the quickest way possible to get a Phoenix server up and running to get a feel for it, you can first install Phoenix:
# install hex, the package manager of elixir
mix local.hex
# install phoenix
mix archive.install hex phx_new 1.4.0
Then create a new Phoenix project with minimal dependencies:
mix phx.new --no-ecto --no-html ~/phoenixtest
Finally, simply cd ~/phoenixtest
and start the server with:
mix phx.server
And your server will be up and running on localhost:4000!
P.S. the route for GET /
will return an error :)