Like A Girl

Pushing the conversation on gender equality.

Code Like A Girl

How To Install Elixir And Phoenix On Mac

I was introduced to Elixir by a coworker shortly after I started my first software engineering job. I had been learning and working on Ruby on Rails applications beforehand, and was excited to check out this new language. As it turns out, all my future projects ended up being in Elixir. And at this point, I believe I have more experience in Elixir than Ruby.

I decided to write tutorials for Elixir since it is a fairly new language, and there are a lack of resources to help beginners get started. My goal is to reduce the friction for whoever wants to get started and facilitate the learning process. I know that I used to Google everything when I got started with Ruby. I want it to be the same with learning Elixir, and that is just not the case right now. It is very easy to get stuck and not find any answers on Stack Overflow, or the rest of the internet. Hopefully that will change in the next couple of years, as I foresee more and more people starting to choose Elixir for their applications.

So what is Elixir? Elixir is a functional language that makes you start thinking of everything in concurrent processes. Ever since I started working in Elixir, I have been able to understand more of what goes on in the background of an application. The code is very explicit and it is easy to tell how things work. If a process in your application fails, there is a supervision tree that will spin the process right back up, letting you focus on just writing the happy path for your application and letting everything else fail.

It is very easy to run things in Elixir in parallel rather than synchronously (one after the other), making it a very powerful tool. Not to mention it has amazing language features like multiple function heads based on pattern matching, function guards, and personalized macros. If you want to build scalable and fault-tolerant applications, Elixir is the right tool for you.

Installation Guide

So let’s get started and get Elixir and Phoenix installed! Phoenix is the web framework most commonly used for Elixir applications. We will build our controllers and views using Phoenix so we can have a web front for our applications.

If you haven’t setup your Mac for development yet, please read through my previous blog and install all the software since we’ll be using it through this tutorial (link below).

How To Setup Your MacBook For Web Development

Note: the $ in the paragraphs below denote a bash command in the terminal. If you are copying this code into your terminal, don’t copy the $ or it won’t work.

Installing Elixir

Install Elixir using brew by entering the commands below on your terminal. (You can find your terminal by pressing Cmd+Space and typing terminal)

$ brew update
$ brew install elixir

Once Elixir is installed, you can open Elixir’s interactive shell and make Elixir say hello by typing the following commands:

$ iex
iex> IO.puts "hello world from elixir"

That’s it! You have Elixir on your machine! Exciting, right!?

Installing Phoenix

In order to install Phoenix and other dependencies, we first need to install Elixir’s package manager, Hex. Type “Y” when prompted if you are sure you want to install.

$ mix local.hex

Also, you need to check your Elixir version before installing Phoenix, as it requires it to be 1.4 or later.

$ elixir -v

If you don’t get an Elixir version that’s greater than 1.4, you can upgrade your Elixir version using brew.

$ brew upgrade elixir

Finally, use mix (Elixir’s build tool) to install Phoenix. Enter “Y” when prompted if you are sure you want to install.

$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez

And that’s it! If you’d prefer a more through installation guide, please refer to the Phoenix hex docs.

Our First App

We are going to create a quick app to make sure everything is working properly. Using your terminal, go to a folder of your choice to place your new test app, and then follow along!

$ mix phx.new hello

When prompted to install dependencies enter “Y”, that way we know that node and npm are working.

Once the app has been created, you will get some instructions on how to access your app, create a database, start your server, and start interactive elixir inside your server.

Go into your application directory, start postgres (our database system), and create the hello database.

$ cd hello
$ brew services start postgresql
$ mix ecto.create

I ran into some issues while creating the database (more evident in my YouTube video) which I outline on the section below. If you didn’t then good for you! and continue on.

Let’s generate a migration file to create a users table for our app.

$ mix ecto.gen.migration create_table

Go to your text editor to see your new migration file.

$ atom .

To your left you should see your directory tree. Go to `priv/repo/migrations` and open the only file you see there and write your migration.

def change
create table(:users) do
add :name, :string
add :last_name, :string

timestamps()
end
end

Save your file, return to your terminal and run your migration.

$ mix ecto.migrate

You now have a users table! I purposely didn’t explain what’s going on here very throughly as we are just testing that everything we installed is working properly. We will be creating our own apps with Elixir and Phoenix through the next few tutorials where I’ll explain what everything means. I am super excited to dive in to this awesome language and framework!

If you run into a Postgres error

When I tried to create my database I got a database connection error.

** (Mix) The database for Hello.Repo couldn't be created: an exception was raised:
** (DB.Connection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
(db_connection) lib/db_connection/connection/connection.ex:148: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

After several minutes of research I found out I hadn’t started my database system. If you installed brew services you can use that to start it (brew services start postgresql ) and hopefully have no issues. However, I proceeded to use a different method and I got a different error for the role “postgres” not existing.

** (Postgrex.Error) FATAL 28000 (invalid_authorization_specification): role "postgres" does not exist
(db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
(connection) lib/connection.ex:622: Connection.enter_connect/5
(stdlib) proc_lib.erl:247: :porc_lib.init_p_do_apply/3

Turns out I hadn’t created the default user “postgres.” Which you can create by running:

$ createuser -d postgres

After that, I started using homebrew to manage my postgres service and that’s why I recommended installing and using it in my previous tutorial. So many minutes wasted… Welcome to programming!

Summary

Thank you for reading this tutorial! I hope you are as excited as I am about coding and Elixir. Let me know of any questions you may have by commenting below. My next blog should be about building our very first Elixir application! Stay tuned!

Follow me on instagram for daily shenanigans.