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:
brew update brew install asdf
Now just check the last version of Erlang and install it globally:
asdf install erlang 24.0 asdf global erlang 24.0
Now install 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)
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
Now we can configure and run the project:
# 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
You'll need to install Docker and then create a docker file:
# docker-file.yml version: "3.8" services: database: environment: POSTGRES_HOST_AUTH_METHOD: trust image: postgres:11-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: "",
You're good to boot up your database:
docker-compose up -d
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:
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?