Getting Started with Phoenix and Elixir: Setup a CRUD App in Minutes

In this tutorial, I would be talking about how to setup up Elixir and Phoenix on your local machine and create a simple CRUD application in minutes.

Install Elixir

You can find all the information you need for installing Elixir on different operating systems here.

Install Phoenix

Run the following commands to install Phoenix. You can also get more details on this here.

$ mix local.hex
$ mix archive.install hex phx_new 1.4.10

Database Setup

Phoenix is configured to use PostgreSQL relational database for its applications by default. If you do not have PostgreSQL on your machine you can install it from here.

Create Phoenix App

Let’s create a project named tasklist.

$ mix tasklist

To create your database, run the command below. You can also configure your database in config/dev.exs.

$ cd tasklist
$ mix ecto.create

Once your database has been successfully created, start your Phoenix app with:

$ mix phx.server

Build your CRUD

The mix phoenix.gen.html command helps us to quickly scaffold parts of our application by creating models, views, controllers, templates, migrations and test files.

$ mix phx.gen.html Functions Task tasks title:string description:string

Next, add the following line to your router.ex file:

resources "/tasks", TaskController

The line above defines all the the routes for our CRUD. It is an alternative to writing all the individual routes which can be viewed by typing:

$ mix phx.routes

You can now restart your server and navigate to the browser tab your application is on, then change the URL to the /tasks path. And you are done!