Perfect Steps of Installing Erlang and Elixir to Apple Silicon Mac (2021 Dec edition)
Tags: elixir, erlang, macos, asdf
Published: 2021-12-17 12:26:27

In this article, I'll show the perfect steps of installing Erlang and Elixir to Apple Silicon Mac from scratch using Homebrew and `asdf`.

## 1. Upgrade macOS (if M1)

You can skip this step for M1 Pro/Max.

If M1, pre-installed macOS is Big Sur. So, first of all, you need to upgrade macOS into Monterey 12.1. However, upgrading from the pre-installed macOS Big Sur to Monterey will be failed due to the defect of the installer of Monterey. So, you should **update Big Sur as the first step.** Next, upgrade it into Monterey 12.0.1. Next, update it into Monterey 12.1.


## 2. Install Xcode 13.2.1 (optional)

You can skip this step even though you will install Homebrew.

Install Xcode 13.2.1 by App store.

## 3. Install Homebrew

1. Launch terminal.
2. Install Homebrew according to [the official website](https://brew.sh/index).
3. Run commands shown in the output results after installation.

The commands mentioned above are as follows (it requires your login password by `sudo`):

```zsh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ${ZDOTDIR:-~}/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
```

Be sure to check and use the installation procedure on the official website and the commands displayed after installation.

## 4. Install Erlang and Elixir by Homebrew

Run the following commands at the terminal:

```zsh
brew update
brew install elixir
```

These commands install the latest Erlang and Elixir. If you use only the latest ones, these commands will be the easiest.

## 4'. Install only Erlang by Homebrew

If you want to install only Erlang, run the following commands at the terminal:

```zsh
brew update
brew install erlang
```

## 4''. Install Erlang and Elixir by `asdf`

Unfortunately, the installation procedure on the official website gets incomplete: unlinked to `wx` and `odbc`, lack of documents, etc.

### 4''-1. Install `asdf` by Homebrew

Install `asdf` by Homebrew according to [the official website](http://asdf-vm.com/guide/getting-started.html#_1-install-dependencies). I'll show the commands as follows, but you should check the official website:

```zsh
brew install asdf
echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
source ${ZDOTDIR:-~}/.zshrc
```

### 4''-2. Install Erlang by `asdf`

**This step is the most important.** Firstly, install pre-requisite libraries:

```zsh
asdf plugin add erlang https://github.com/asdf-vm/asdf-erlang.git
brew install wxwidgets
brew install openjdk
echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ${ZDOTDIR:-~}/.zshrc
source ${ZDOTDIR:-~}/.zshrc
brew install fop
```

Then, set the environment variables `KERL_CONFIGURE_OPTION`, `CC` and `LDFLAGS` as follows:

```zsh
export KERL_CONFIGURE_OPTIONS="--with-ssl=$(brew --prefix openssl@1.1) --with-odbc=$(brew --prefix unixodbc)"
export CC="/usr/bin/clang -I$(brew --prefix unixodbc)/include" 
export LDFLAGS=-L$(brew --prefix unixodbc)/lib
```

If you will install Erlang/OTP 24.2+ and use OpenSSL 3, install OpenSSL 3 and reset `KERL_CONFIGURE_OPTION`:

```zsh
brew install openssl@3
export KERL_CONFIGURE_OPTIONS="--with-ssl=$(brew --prefix openssl@3) --with-odbc=$(brew --prefix unixodbc)"
```

I got the solution to link `odbc` by referring this issue: https://github.com/asdf-vm/asdf-erlang/issues/191

Next, if you'll install the latest one, run the following commands:

```zsh
asdf install erlang latest
asdf global erlang latest
```

You can get the versions that can be installed as follows:

```zsh
asdf list-all erlang
```

Unfortunately, some specific older versions cannot be installed into Apple Silicon Mac.  

If you'll install OTP 22, follow this issue: https://github.com/asdf-vm/asdf-erlang/issues/221

After installation, unset `CC` and `LDFLAGS`:

```zsh
unset CC LDFLAGS
```

### 4''-3. Install Elixir by `asdf`

Install the Elixir plugin by `asdf` according to [the official website](https://github.com/asdf-vm/asdf-elixir).

```zsh
asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir.git
```

Next, if you'll install the latest one, run the following commands:

```zsh
asdf install elixir latest
asdf global elixir latest
```

You can get the versions that can be installed as follows:

```zsh
asdf list-all elixir
```

## Summary

You'll get Elixir and Erlang on Apple Silicon as the steps mentioned above.