How to setup Elixir, Phoenix and Postgresql on MacOS M1, M2
In this blog post we’ll see how to setup Erlang and Elixir on your M1, M2 Mac with Homebrew and ASDF.
This guide assumes you have a fresh mac and we’ll setup everything from the start. If you have already setup any of the tools mentioned, feel free to skip that part.
If you have your M1 mac setup with rosetta, make sure to run the installation process on a native terminal to prevent segmentation faults while building.
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
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, like
rbenv for Ruby,
nvm for NodeJS, each with it’s own configurations and usage syntax. This lead to a lot of conflicts between the tools. ASDF is a single package manager with a plugin interface that can handle multiple languages and their versions. ASDF keeps the versions of the different tools used in a
.tool-versions file in the directory. We are going to use ASDF to setup Elixir and Erlang globally.
Head on over to asdf-vm.com/guide/getting-started.html and setup ASDF.
- Use Homebrew to install the dependencies
- Prefer the official git method for installation, as ASDF would then manage itself.
- 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.
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. Install required dependencies for it. OpenSSL is required for secure communication and WxWidgets is needed for rendering out the debugger and observer. Even if you have OpenSSL installed, you need version
brew install firstname.lastname@example.org wxwidgets
Optional: Since Erlang is compiled on our machine, it is recommended to set compile time flags to get an optimal binary. Erlang compile time flags are configured by setting the
KERL_CONFIGURE_OPTIONSshell function. The below flags are used by Jose Valim. These flags disable linking with Java, which is only required if you want to interface with Java.
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 email@example.com)" 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
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
25installed 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
Since we have the OTP version
25installed, 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
iexto make sure the installation is successful.
Setting up Phoenix and Postgresql
Since we have setup Elixir we can setup Phoenix and Postgresql.
Before setting up Phoenix, reshim ASDF so the binaries are all linked up properly.
asdf reshim elixir
Hex is the package manager for Elixir and Erlang and
mixis the tool for managing dependencies. You have to set it up to download Phoenix. Running this command installs or upgrades hex.
Install Phoenix project generators.
mix archive.install hex phx_new
Postgresql is the database of choice for new Phoenix apps. You have to set it up so you have access to a database on your developing machine.
Install Postgresql using Homebrew. We are using the latest version of Postgres. This installs the Postgresql and starts the service that runs in the background.
brew install postgresql@15
Restart Postgres to make sure the service is running. You only have to do this once.
brew services restart postgresql@15
Create the default postgres user. Phoenix expects a default Postgres user to be present. Run
createuserto create the user, make sure to substitute the version of Postgres.
/opt/homebrew/opt/postgresql@15/bin/createuser -d postgres
If you have already setup Homebrew before the M1 upgrade, it would have been installed on
/usr/local/homebrew/opt/postgresql@15/bin/createuser -d postgres
To make sure we have everything installed properly, we are going to create a new test Phoenix project.
mix phx.new testin any directory. Answer
ywhen prompted to install dependencies.
Change into the directory and run
mix ecto.setupto setup the database.
mix phx.serverin the directory. You will have your Phoenix server running on http://localhost:4000.
Congrats, you just setup Elixir and Phoenix on your mac. Now it’s time to make something with it.
Bonus: Setup NodeJS
Add NodeJS Plugin to ASDF
asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git
Install and set the latest LTS version. The LTS version will be the most stable and supported one.
asdf install nodejs lts asdf global nodejs lts asdf reshim nodejs
That’s it. You now have NodeJS setup in your system as well. You can now use
npm to install new packages from NPM to your project.
- Homebrew - https://brew.sh
- ASDF Setup - https://asdf-vm.com/guide/getting-started.html
- ASDF Erlang - https://github.com/asdf-vm/asdf-erlang
- ASDF Elixir - https://github.com/asdf-vm/asdf-elixir
- Phoenix Getting Started - https://hexdocs.pm/phoenix/installation.html
- ASDF NodeJS - https://github.com/asdf-vm/asdf-nodejs