Setup Erlang and Elixir, Phoenix Framework on Mac

Setup Erlang and Elixir, Phoenix Framework on Mac

Setup Tools

This guide for guideline everything from the start and setup any of the tools mention.

Before installation

make sure to run the installation process on a native terminal to prevent segmentation faults while building.

Setup Homebrew

Homebrew is the de-facto package manager on macOS. You can install most packages required for development and other applications with a single command. Follow the official instructions at brew.sh and setup Homebrew on the machine. The script will install XCode Command Line tools and setup the Homebrew directory on /opt/homebrew.

Setup ASDF

Development requires multiple language runtimes to be present on the system. Multiple projects also require multiple versions of the same environment to be present. Previously we had to setup multiple tools to manage multiple environments

Head on over to asdf-vm.com/guide/getting-started.html and setup ASDF.

  1. Use Homebrew to install the dependencies

  2. Prefer the official git method for installation, as ASDF would then manage itself.

  3. Select what Shell you use and setup the script properly. This sets the path correctly for the different tools.

Setup Erlang and Elixir

Elixir is built on top of Erlang. So we have to setup Erlang first and then setup Elixir.

Setup Erlang

  • Install the Erlang plugin and dependencies for ASDF. Open your shell and type these commands.

        asdf plugin add erlang https://github.com/asdf-vm/asdf-erlang.git
    
  • ASDF downloads source files and compiles Erlang on our machine.. Even if you have OpenSSL installed, you need version 1.1.

        brew install openssl@1.1 wxwidgets
    
  • Optional: Since Erlang is compiled on our machine, it is recommended to set compile time flags to get an optimal binary.

        export KERL_CONFIGURE_OPTIONS="--disable-debug --disable-silent-rules --without-javac --enable-shared-zlib --enable-dynamic-ssl-lib --enable-threads --enable-kernel-poll --enable-wx --enable-webview --enable-darwin-64bit --enable-gettimeofday-as-os-system-time --with-ssl=$(brew --prefix openssl@1.1)" KERL_BUILD_DOCS="yes"
    
  • Download and install the latest version of Erlang and set it as the default global version.

        # Downloads and installs the latest version
        asdf install erlang latest
    
        # Sets the global version of Erlang
        asdf global erlang latest
    

    You can install specific version of Erlang if you want to. This is useful when you want to install older versions of Elixir that are tied to specific OTP versions.

        # Lists all the erlang versions
        asdf list-all erlang
    
        asdf install erlang 25.2
    
        asdf global erlang 25.2
    

Setup Elixir

Every Elixir version has a list of Erlang versions that it supports. ASDF Elixir downloads pre-compiled versions of the run-time. When installing Elixir version make sure you have a corresponding Erlang version installed first.

  • Check which version of Erlang is installed on your system.

      asdf current erlang
    

    Take a note of the version number, which might come out as 25.2. That means we have OTP version 25 installed by our system.

  • List all available Elixir versions from ASDF

        asdf list-all elixir
    
        ...
        1.14.2-otp-23
        1.14.2-otp-24
        1.14.2-otp-25
        1.14.3
        1.14.3-otp-23
        1.14.3-otp-24
        1.14.3-otp-25
        main
        main-otp-22
        main-otp-23
        main-otp-24
        main-otp-25
        master
        master-otp-21
        master-otp-22
        master-otp-23
        master-otp-24
    
  • Install Elixir

    Since we have the OTP version 25 installed, we’ll select the latest version that has been compiled with the OTP release.

        asdf install elixir 1.14.3-otp-25
    
        asdf global elixir 1.14.3-otp-25
    
  • Validate if the installation has succeeded

    Run iex to make sure the installation is successful.

Setting up Phoenix and Postgresql

Setup Elixir Phoenix and Postgresql.

Setup Phoenix

  • Before setting up Phoenix, reshim ASDF so the binaries are all linked up properly.

        asdf reshim elixir
    
  • Install Hex

    Hex is the package manager for Elixir and Erlang and mix is the tool for managing dependencies. You have to set it up to download Phoenix. Running this command installs or upgrades hex.

        mix local.hex
    
  • When have some error in command, lets trying follow

        mix archive.install hex phx_new
    
  • Install Phoenix project generators.

        mix archive.install hex phx_new
    

Test Installation

To make sure everything installed properly, going to create a new test Phoenix project.

  • Type and Run mix phx.new test in any directory. Answer y when prompted to install dependencies.

  • Change into the path directory and run mix ecto.setup to setup the database.

  • Run mix phx.server in the directory. You will have your Phoenix server running on http://localhost:4000.

Congrats, setup Elixir and Phoenix on mac is succed. Now it’s time to make something with it.