Erlang and Elixir with asdf in Raspberry 4 with Ubuntu 20.04

To install the latest/current versions of erlang (23.0.2) and elixir (1.10.3), follow the instructions below:

Install asdf ( full instructions https://asdf-vm.com/#/core-manage-asdf-vm):

SHELL

Add the following two lines in your ~/.bashrc file:

SHELL

Reload/Source your bash file with

SHELL

Install the erlang plugin

SHELL

If you get the following warnings:

SHELL

Install the missing packages:

SHELL

Install latest erlang version:

SHELL

Set it up globally (if you want):

SHELL

Add elixir plugin:

SHELL

Install latest elixir version (1.10.3):

SHELL

Set it up globally:

SHELL

Finally you can also install the latest Phoenix version (1.5.3):

SHELL

Using a primary key not named id with Ecto and Mysql

Problem

You would like to use a legacy or a database you don’t have control over, with Phoenix, but the primary key of the table it is not named ‘id’.

** (Mariaex.Error) (1054): Unknown column 'p0.id' in 'field list'

Solution

You can define the primary key using the @primary_key as in:

 @primary_key {:id_other_name, :integer, []}
  schema "db_table" do
    field :description, :string
    ...
  end

Taken from here

(issues) lib/issues/github_issues.ex:14: Issues.GithubIssues.handle_response({:error, %HTTPoison.Error{id: nil, reason: :connect_timeout}})

Problem

Trying to follow the example in ‘Programming Elixir 1.2’ for fetching the issues from Github you are getting the following error:

(issues) lib/issues/github_issues.ex:14: Issues.GithubIssues.handle_response({:error, %HTTPoison.Error{id: nil, reason: :connect_timeout}})

Solution

You are quite likely behind a proxy and you get a timeout. Add the proxy details to the HTTPoison.get request as follows:

iex(4)> HTTPoison.get!( "https://api.github.com/repos/elixir-lang/elixir", [], [{:proxy, "your_proxy_ip:your_proxy_port"}])

And you could make the following change to the github_issues.ex file to be able to work with the proxy:

def fetch(user, project, proxy) do
  issues_url(user, project)
  |> HTTPoison.get(@user_agent, proxy)
  |> handle_response
end

So you can call it like the following from iex:

iex(1)> Issues.GithubIssues.fetch("elixir-lang", "elixir", [{:proxy, "your_proxy_ip:your_proxy_port"}])

Request failed (404) ** (Mix) Package fetch failed and no cached copy available

Problem

You are trying to use hex for installing dependencies but when you are behind a proxy you get the following message:

Request failed (404)
** (Mix) Package fetch failed and no cached copy available

Solution

You can use the following to set up hex (taken from the answer here:

mix hex.config http_proxy http://proxy.mycompany.com

Using Elixir with Docker

In order to be able to use Elixir with the help of Docker, so that you can run different containers with different versions, and to have a shared code folder, you could follow the steps below:

  • Install docker in your system. Installation instructions for different systems are here
  • Download the elixir image from the Docker Hub:
    sudo docker pull trenpixster/elixir
  • List the images on your host:
    sudo ps docker images
  • Start a specific version of the elixir docker container (ie elixir 1.0.3):
    sudo docker run -t -i trenpixster/elixir:1.0.3 /bin/bash
  • Use a shared folder with code between the docker container and your host:
    sudo docker run -v /home/user/Prog:/Prog -t -i trenpixster/elixir:1.0.3 /bin/bash

    where the folders after the -v option are /home/user/Prog (host) and /Prog (docker)

  • Use a port forwarding:
    sudo docker run -p 8000 -v /home/user/Prog:/Prog -t -i trenpixster/elixir:1.0.3 /bin/bash

Updating Phoenix from 0.3.1 to 0.4.0

Problem

You would like to upgrade to the latest version of Phoenix 0.4.0 from the previous version of 0.3.1. When you try to update the dependencies and try to start phoenix again (mix phoenix.start) you get the following error:

** (Mix) You're trying to run :your_app_name on Elixir v1.0.0-rc1 
but it has declared in its mix.exs file it supports 
only Elixir ~> 0.15.0

Solution

After upgrading your Elixir version to the latest (1.0.0-rc1), and updating the path to your elixir installation so that it uses the new one, clear the dependencies from your project:

mix deps.clear --all

Change the dependency in your mix.exs file to be as the following:

def project do
    [ app: :your_app_name,
      version: "0.0.1",
      elixir: "~> 1.0.0-rc1",
      elixirc_paths: ["lib", "web"],
      deps: deps ]
  end

and then get the dependencies again with:

mix deps.get

after that you should be able to start your phoenix project with tne new version.