I know, you already heard something like this in the Rails world. But for somebody that never created an application with Phoenix and Elixir, it is a good point to start.
First of all, you need to install the Erlang/Elixir package manager. In this world, try to use the asdf that you can install via brew using Mac:
brew update brew install asdf
You need to teach asdf how install Erlang and Elixir adding some plugins on it:
asdf plugin add erlang asdf plugin add elixir
Now install the last version of Erlang and install it globally:
asdf install erlang 24.0 asdf global erlang 24.0
And then the last version of Elixir:
asdf install elixir 1.12 asdf global elixir 1.12
Check your versions:
elixir --version # Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] # Elixir 1.12.0 (compiled with Erlang/OTP 22)
You save which Erlang and Elixir version you use in a file called
.tool-versions, very similar the
.ruby-version in Ruby world:
# .tool-versions elixir 1.12 erlang 24.0
Our dependencies package manager calls Hex so let's install it:
And finally, we can install the Phoenix Framework:
mix archive.install hex phx_new 1.5.9
Creating the project
mix phx.new bible cd bible
You'll need to install Docker and then create a docker file:
# docker-compose.yml version: "3.8" services: database: environment: POSTGRES_HOST_AUTH_METHOD: trust image: postgres:13-alpine ports: - 5432:5432
Since we won't use a password in dev and test, let's set it to empty:
# config/dev.exs config :bible, Bible.Repo, username: "postgres", password: "",
# config/test.exs config :bible, Bible.Repo, username: "postgres", password: "",
- If you need to change the port, just add one extra property
port: 5434, for example.
You're good to boot up your database:
docker-compose up -d
Now run some commands:
# installs the dependencies mix deps.get # creates the database mix ecto.setup # installs assets cd assets && yarn install && cd - # run the server mix phx.server
Visit the URL and see your project running: localhost:4000
Creating an automatic CRUD
In Phoenix, we separate the code inside Modules, so the
Persons will be a module containing our CRUD code.
Person is the model name and
persons in the table with
description as fields.
mix phx.gen.html Persons Person persons name:string description:text
Enable the CRUD routes adding the following code in the final of the block
scope "/", BibleWeb do:
# lib/bible_web/router.ex scope "/", BibleWeb do resources "/persons", PersonController end
You should apply the migration in the database:
Just visit localhost:4000/persons and have fun!
Elixir and Phoenix are funny and strong worlds and we already have a lot of tools around this community. I believe that this language will be the future and will replace a couple of Java-like systems in big companies, mainly financial ones. What do you think?