DaringAkalat

Installing Elixir 1.4.x and Phoenix 1.7.x on FreeBSD 13

Posted on Apr 21, 2023 by Daring Akalat in Elixir, Phoenix and FreeBSD

$ uname -a
FreeBSD freebsd 13.1-RELEASE-p6 FreeBSD 13.1-RELEASE-p6 GENERIC amd64
$ pwd
/home/akalat
$ pkg search elixir
elixir-1.14.3                  Functional, meta-programming aware language built on top of Erlang VM
elixir-hex-2.0.5               Package manager for the Erlang VM
elixir-make-0.4.2              Make compiler for Mix
$ pkg search erlang
erlang-25.3,4                  Functional programming language from Ericsson
erlang-man-25.3                Manual pages for Erlang/OTP
erlang-rebar3_hex-2.5.0        Rebar3 Hex library
erlang-recon-2.5.2             Collection of functions and scripts to debug Erlang in production
erlang-runtime21-21.3.8.24_2   Functional programming language from Ericsson
erlang-runtime22-22.3.4.26_1   Functional programming language from Ericsson
erlang-runtime23-23.3.4.18_1   Functional programming language from Ericsson
erlang-runtime24-24.3.4.10     Functional programming language from Ericsson
erlang-runtime25-25.3          Functional programming language from Ericsson
$ doas pkg install elixir elixir-hex elixir-make
Message from erlang-25.3,4:

--
Message from elixir-1.14.3:

--
Elixir requires a compatible Erlang/OTP runtime. Ensure that you have
one of the following installed:

- lang/erlang
- lang/erlang-runtime24 or newer
$ elixir -v
Erlang/OTP 25 [erts-13.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns] [dtrace]

Elixir 1.14.3 (compiled with Erlang/OTP 25)
mix local.rebar
* creating /home/akalat/.mix/elixir/1-14/rebar3
$ tree .mix
.mix
└── elixir
    └── 1-14
        └── rebar3

3 directories, 1 file
$ mix archive.install hex phx_new
New:
  phx_new 1.7.2
* Getting phx_new (Hex package)
All dependencies are up to date
Compiling 11 files (.ex)
Generated phx_new app
Generated archive "phx_new-1.7.2.ez" with MIX_ENV=prod
Are you sure you want to install "phx_new-1.7.2.ez"? [Yn]
* creating /home/akalat/.mix/archives/phx_new-1.7.2
$ tree .mix
.mix
├── archives
   └── phx_new-1.7.2
       └── phx_new-1.7.2
           └── ebin
               ├── Elixir.Mix.Tasks.Local.Phx.beam
               ├── Elixir.Mix.Tasks.Phx.New.Ecto.beam
               ├── Elixir.Mix.Tasks.Phx.New.Web.beam
               ├── Elixir.Mix.Tasks.Phx.New.beam
               ├── Elixir.Phx.New.Ecto.beam
               ├── Elixir.Phx.New.Generator.beam
               ├── Elixir.Phx.New.Mailer.beam
               ├── Elixir.Phx.New.Project.beam
               ├── Elixir.Phx.New.Single.beam
               ├── Elixir.Phx.New.Umbrella.beam
               ├── Elixir.Phx.New.Web.beam
               └── phx_new.app
└── elixir
    └── 1-14
        └── rebar3

7 directories, 13 files
$ mkdir -p ~/elixir/phoenix
$ cd ~/elixir/phoenix
$ pwd
/home/akalat/elixir/phoenix
$ mix phx.new hello --no-assets
* creating hello/config/config.exs
* creating hello/config/dev.exs
* creating hello/config/prod.exs

-- snip --

Fetch and install dependencies? [Yn]
* running mix deps.get
* running mix assets.setup
* 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
$ su
$ su postgres
$ service postgresql status
pg_ctl: no server running
$ service postgresql start
[32741] LOG:   ending log output to stderr
[32741] HINT:  Future log output will go to log destination "syslog".
$ psql
postgres=# CREATE ROLE akalat WITH ENCRYPTED PASSWORD 'daring1';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 akalat    | Cannot login                                               | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# ALTER ROLE akalat WITH CREATEDB LOGIN REPLICATION;

See https://www.postgresql.org/docs/current/sql-alterrole.html.

ALTER ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 akalat    | Create DB, Replication                                     | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# \q
$ exit && exit
$ whoami && pwd
akalat
/home/akalat/elixir/phoenix
$ cd hello
$ ee config/dev.exs
# config/dev.exs
import Config

# Configure your database
config :hello, Hello.Repo,
  username: "postgres",
  password: "postgres",

-- snip --

config :hello, HelloWeb.Endpoint,
  # Binding to loopback ipv4 address prevents access from other machines.
  # Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
  http: [ip: {127, 0, 0, 1}, port: 4000],

-- snip --
# config/dev.exs
import Config

# Configure your database
config :hello, Hello.Repo,
  username: "akalat",
  password: "daring1",

-- snip --

config :hello, HelloWeb.Endpoint,
  # Binding to loopback ipv4 address prevents access from other machines.
  # Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
  http: [ip: {0, 0, 0, 0}, port: 4000],

-- snip --
$ mix ecto.create
Compiling 15 files (.ex)
Generated hello app
The database for Hello.Repo has been created
$ ee config/config.exs
# config/config.exs
import Config

-- snip --

# Configures the endpoint
config :hello, HelloWeb.Endpoint,
  url: [host: "localhost"],

-- snip --
# config/config.exs
import Config

-- snip --

# Configures the endpoint
config :hello, HelloWeb.Endpoint,
  url: [host: "0.0.0.0"],

-- snip --
$ pkg search inotify-tools
inotify-tools-3.22.6.0         Command-line utilities to watch for file events
$ doas pkg install inotify-tools
$ mix phx.server
[info] Running HelloWeb.Endpoint with cowboy 2.9.0 at 0.0.0.0:4000 (http)
[info] Access HelloWeb.Endpoint at http://0.0.0.0:4000
$ curl http://0.0.0.0:4000