Hi everyone! 🙂
It’s been a while since I last updated this blog and a lot of things has surely happen – new technologies, new projects, new hobbies — indeed a lot of new things. One year surely flew fast!
So for this comeback post, I’ll share with you today my notes on installing an Elixir/Phoenix setup on a Mac OSX. The first time I did this was a year ago and I had to do it again last week — not much have changed but just thought of documenting it in case it’ll also help anyone out there.
So first, what is Elixir? Regular readers of this blog might have noticed that this is the first post that mentioned it. Elixir is a functional programming language that is built for distributed and fault-tolerant systems. Elixir compile to Erlang bytecode which then runs on BEAM (Erlang’s VM). Before we proceed with Elixir, what then is Erlang? Erlang is also a concurrent functional programming language that first appeared in the 1980’s which was used mainly back then for telecommunications to support hundreds of thousands of concurrent communications. It was developed in the Ericsson company by Joe Armstrong and company.
With this awesomeness of Erlang, Elixir leverages it by running on top of the Erlang VM and providing a syntax and feel that is reminiscent of more modern programming language such as Ruby and Python making it a top choice for writing embedded software application as well as web applications.
And on this post, we shall be seeing Phoenix – an MVC web framework running with Elixir. Let’s get started!
To setup a running Phoenix app, we need to set up the prerequisites first before we install Phoenix.
1. Install Elixir via HomeBrew
$> brew install elixir
2. Install the Hex Package Manager
$> mix local.hex
3. Check the Elixir version
$> elixir -v
* Phoenix requires Elixir 1.4 (or later) and Erlang 18 (or later). Your output would look something like this:
Erlang/OTP 19 [erts-8.1] [source-77fb4f8] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false] Elixir 1.3.4
4. Install the Mix Phoenix Archive
$> mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
* This contains the Phoenix application as well as the compiled BEAM files
5. Install NodeJS (Optional)
By default Phoenix uses brunch.io to compile your app’s static assets. Brunch.io on the other hand requires the Node Package Manager (NPM) to install its dependencies and NPM requires nodeJS. If you wish to not install NodeJS, you may skip this step and head to the Installation Verification section below.
a. Install NVM
To install NodeJS, let’s first install the Node Version Manager (NVM). NVM provides a way to organize and switch between several version of NodeJS in your system. Developers who have experienced working with several projects requiring different NodeJS versions appreciate the importance of this. To install Phoenix, we’ll be using only one version but since we’re already setting it up, let’s do it the right way.
$> curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
After running the above command, the following lines will be available in your ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc.
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
It installed mine in ~/.bashrc though nvm still wasn’t by my terminal even if I already restarted it so added the above lines to my ~/.bash_profile too and it already worked!
b. Install NodeJS via NVM
$> nvm install node
Once it’s done, we can verify that NodeJS is indeed installed:
$> node -v
a.k.a. Creating our first app!
1. Create a Phoenix app
$> mix phx.new myfirstapp
2. Run the server
$> cd myfirstapp $> mix phoenix.server
Note: You may need to run mix ecto.create in case you encounter the following error,
[error] Postgrex.Protocol (#PID<0.3421.0>) failed to connect: ** (Postgrex.Error) FATAL 3D000 (invalid_catalog_name)
And yay, that’s it! You now have a up and running Phoenix installation. 🙂
So that’s it for this post and hope to see you again in my future posts! Will be sure to write more on Elixir and Phoenix.
See you! ❤