[Elixir Phoenix] Install, Up and Run Phoenix
Phoenix
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.
Prerequites
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):
1 | mix local.hex |
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.
Installation
Phoenix
To check Elixir and Erlang version, run:
1 | elixir -v |
Once we have Elixir and Erlang, we are ready to install the Phoenix application generator:
1 | 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:
1 | 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.
1 | Fetch and install dependencies? [Yn] Y |
Config Database
Check or edit config/dev.exs
file according to your database.
1 | # config/dev.exs |
Now we’ll create our database:
1 | mix ecto.create |
And finally, we’ll start the Phoenix server:
1 | mix phx.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.
References
[1] Phoenix Framework - https://www.phoenixframework.org/
[2] Installation — Phoenix v1.6.2 - https://hexdocs.pm/phoenix/installation.html
[3] Up and Running — Phoenix v1.6.2 - https://hexdocs.pm/phoenix/up_and_running.html
[4] The Elixir programming language - https://elixir-lang.org/