Phoenix is a web development framework written in Elixir which implements the server-side Model View Controller (MVC) pattern. Many of its components and concepts will seem familiar to those of us with experience in other web frameworks like Ruby on Rails or Python’s Django.
Elixir 1.12 or later
Phoenix is written in Elixir, and our application code will also be written in Elixir. We won’t get far in a Phoenix app without it! The Elixir site maintains a great Installation Page to help.
If we have just installed Elixir for the first time, we will need to install the Hex package manager as well. Hex is necessary to get a Phoenix app running (by installing dependencies) and to install any extra dependencies we might need along the way.
Here’s the command to install Hex (If you have Hex already installed, it will upgrade Hex to the latest version):
Erlang 22 or later
Elixir code compiles to Erlang byte code to run on the Erlang virtual machine. Without Erlang, Elixir code has no virtual machine to run on, so we need to install Erlang as well.
When we install Elixir using instructions from the Elixir Installation Page - https://elixir-lang.org/install.html, we will usually get Erlang too. If Erlang was not installed along with Elixir, please see the Erlang Instructions - https://elixir-lang.org/install.html#installing-erlang section of the Elixir Installation Page for instructions.
To check Elixir and Erlang version, run:
Once we have Elixir and Erlang, we are ready to install the Phoenix application generator:
mix archive.install hex phx_new
Up and Running
We can run mix phx.new from any directory in order to bootstrap our Phoenix application. Phoenix will accept either an absolute or relative path for the directory of our new project. Assuming that the name of our application is hello, let’s run the following command:
mix phx.new hello
A note about Ecto: Ecto allows our Phoenix application to communicate with a data store, such as PostgreSQL, MySQL, and others. If our application will not require this component we can skip this dependency by passing the
--no-ecto flag to mix phx.new.
To learn more about
mix phx.new you can read the Mix Tasks Guide - https://hexdocs.pm/phoenix/mix_tasks.html#phoenix-specific-mix-tasks.
When it’s done, it will ask us if we want it to install our dependencies for us. Let’s say yes to that.
Fetch and install dependencies? [Yn] Y
Check or edit
config/dev.exs file according to your database.
Now we’ll create our database:
And finally, we’ll start the Phoenix server:
By default Phoenix accepts requests on port
4000. If we point our favorite web browser at
http://localhost:4000, we should see the Phoenix Framework welcome page.