[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
2
3
4
$ elixir -v
Erlang/OTP 24 [erts-12.1.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.12.3 (compiled with Erlang/OTP 24)

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Fetch and install dependencies? [Yn] Y
* running mix deps.get
* running mix deps.compile

We are almost there! The following steps are missing:

$ cd hello

Then configure your database in config/dev.exs and run:

$ mix ecto.create

Start your Phoenix app with:

$ mix phx.server

You can also run your app inside IEx (Interactive Elixir) as:

$ iex -S mix phx.server
```

Ok, let's give it a try. First, we'll cd into the `hello/` directory we've just created:

```shell
cd hello

Config Database

Check or edit config/dev.exs file according to your database.

1
2
3
4
5
6
7
8
9
10
# config/dev.exs

# Configure your database
config :hello, Hello.Repo,
username: "postgres",
password: "postgres",
database: "postgres",
hostname: "postgres",
show_sensitive_data_on_connection_error: true,
pool_size: 10

Now we’ll create our database:

1
2
3
4
$ mix ecto.create
Compiling 14 files (.ex)
Generated hello app
The database for Hello.Repo has already been created

And finally, we’ll start the Phoenix server:

1
2
3
4
5
mix phx.server
[info] Running HelloWeb.Endpoint with cowboy 2.9.0 at 127.0.0.1:4000 (http)
[info] Access HelloWeb.Endpoint at http://localhost:4000
[watch] build finished, watching for changes...
...

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/

[5] Index - Erlang/OTP - https://www.erlang.org/