The first step to build a web application with the Phoenix Web Framework is to install the tools you need to start coding.
Follow the Install Instructions from the Elixir-Lang homepage. This will install the Elixir and Erlang programming languages on your machine.
The homebrew instructions worked seamlessly for me. Once you have installed Elixir, you can verify that it is properly installed by running the following commands:
$ elixir --version Elixir 1.0.4
$ erl -version Erlang (SMP,ASYNC_THREADS,HIPE) (BEAM) emulator version 6.2
Elixir provides a REPL, similar to IRB, called
iex. Jump into iex by running the following command:
As you would expect you can run standard Elixir code here:
$ iex Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.0.2) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> a = 10 + 14 24 iex(2)>
To get out of iex, press CTRL+C twice in a row.
In the future, if you need to manage multiple versions of Elixir on the same machine, the exenv project, will do just that. For this tutorial though, we'll stick to using just the single version of Elixir that we just installed on your computer.
The syntax of Elixir is very similar to ruby. The key differences are: macros instead of meta-programming, no classes or instance variables and pattern matching support.
To get a feel for pattern matching in Elixir, read Pattern Matching in Elixir for Rubyists from DockYard, it covers the topic in an excellent manner.
Most text editors don't include syntax highlighting for Elixir source code by default. There are packages for Sublime, but I will show you how to add support for Elixir into Atom. If you haven't installed the atom text editor, you can get it here.
Once you load atom, select Atom>Preferences... in the menu. Select the "packages" button, filter for the
language-elixir. Now you can install it. This will automatically highlight files with the extension
exs as elixir source code.
Right now, Postgres is the only database supported by
Ecto, which is what we'll be using to connect to our database. This makes deciding which to use easy!
If you don't have Postgres installed yet, follow the instructions to install it.
Then create an ecto user in Postgres:
$ createuser ecto --pwprompt
Enter a password that you will remember.
Then create the database for this project:
$ createdb -Oecto -Eutf8 splurty_development
Make sure you can login to your postgres database by running the following command:
$ psql splurty_development --user ecto --password
When it asks you for a password, enter it in the prompt. Once you get pulled into the postgres database prompt, press CTRL+D to exit.
Navigate in the terminal to the directory where your different projects live and clone the phoenix source code:
$ git clone https://github.com/phoenixframework/phoenix.git
Navigate into the phoenix framework folder:
This tutorial is geared to work with Phoenix version 0.8. As it's changing quite quickly, you'll want to make sure you're using that version.
$ git checkout v0.8.0
You need to be in the same directory as the phoenix project. You need to get the dependencies and compile the phoenix project before you go any further.
$ mix do deps.get, compile
Once that completes, run the following command:
$ mix phoenix.new splurty ../splurty
This command will generate a new phoenix web project called
splurty in the splurty directory of the parent folder.
This is the first time we're using the
mix command. Mix is a program that is a hybrid of bundler and rake if you're familiar with ruby on rails. In short it allows us to run commands and manages our dependencies.
If you've tried programming in scala, or any other functional programming language, you may have noticed that package management is usually overly complicated. Mix is very well designed and works as simply as rake and bundler.
Navigate to the directory that your splurty application is located in:
$ cd ../splurty
Fetch, install and compile your application's source code and dependencies by running this command:
$ mix do deps.get, compile
It should complete without showing you any error messages. Once it does you can start your server by running the following command:
$ mix phoenix.server
Navigate your browser to localhost:4000 and see your generated phoenix web application. Awesome!
Your application should look just like this at this step.