How to set-up a Ruby and Elixir dev environment on macOS Catalina

Edit: Please note there is a newer version of this guide here: https://astonj.com/how-to-set-up-a-ruby-and-elixir-dev-environment-on-macos-2021/


Having recently started afresh on macOS, I thought I’d make some notes on setting up a new dev environment. In this post we’ll set up a fresh dev environment for Ruby and Elixir – but you can use this guide for any similar web development set-up since the version manager we’ll be using (asdf) can be used for lots and lots and lots of different languages.

If you don’t need Ruby or Elixir, you can just omit those steps. We’ll also add a few other common web development tools or packages. In short, we’ll be covering installation of:

  • Xcode
  • Command line tools
  • Homebrew
  • ImageMagic
  • Git
  • Postgres
  • TextMate
  • MacVim
  • Asdf
  • Ruby
  • Rubygems
  • Rails
  • Erlang
  • Elixir
  • Hex
  • Phoenix

Xcode

Simply download and install Xcode from the App store.

Command Line Tools

Once Xcode has installed, open it, then go to preferences > downloads, and install command line tools. Or alternatively, enter this in terminal:

xcode-select --install

Homebrew

In the terminal copy and paste the following:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

In your .zshrc file add:

#Homebrew Completions
if type brew &>/dev/null; then
FPATH=$(brew --prefix)/share/zsh/site-functions:$FPATH
fi

Let’s get brewing – and install Postgres, ImageMagick and git

Let’s update Homebrew first:

brew update

Postgres

Then:

brew install postgres

Then to start Postgres and on startup, run:

brew services start postgresql

Then create your initial db:

createdb

Then:

psql
CREATE ROLE postgres LOGIN CREATEDB;

That will create your Postgres user (use CTRL Z to exit).

Git and ImageMagick

brew install imagemagick
brew install git

Then in terminal:

git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

Installing asdf

In terminal, type:

brew install asdf

Then in terminal run:

echo -e "\n. $(brew --prefix asdf)/asdf.sh" >> ~/.zshrc

This will add the following to the bottom of your ~/.zshrc file:

. /usr/local/opt/asdf/asdf.sh

Installing asdf plugin dependencies

In terminal:

brew install \
coreutils automake autoconf openssl \
libyaml readline libxslt libtool unixodbc \
unzip curl

If you need to upgrade asdf, use brew upgrade asdf.

How to remove asdf

If you ever need to remove asdf, you can do so pretty easily:

  • In your .zshrc remove the line that was added on installation:. /usr/local/opt/asdf/asdf.sh
  • Run rm -rf ~/.asdf/ ~/.tool-versions to completely remove all the asdf files from your system.
  • In terminal: brew uninstall --force asdf

Installing Ruby

Skip if you don’t need Ruby and Rails…

In terminal:

asdf plugin-add ruby

To see which versions are available:

asdf list-all ruby

Then install the versions you want with the following, just replace 2.6.5 with the version you need:

asdf install ruby 2.6.5

Then set your preferred defaults with:

asdf global ruby 2.6.5

And to use a specific version in any particular directory:

asdf local ruby 2.6.5

To add new Rubies:

asdf plugin-update ruby
asdf list-all ruby

Or simply:

asdf install ruby latest

Rubygems and Rails

Open a fresh terminal window and:

gem update --system

For Rails:

gem install rails
brew install yarn

Restart terminal and check your versions by:

ruby -v
rails -v

Installing Elixir & Erlang

Skip if you don’t need Elixir and Phoenix

In terminal:

asdf plugin-add Erlang
asdf plugin-add Elixir

To see which versions are available:

asdf list-all elixir
asdf list-all erlang

Then install the versions you want with the following, just replace version numbers with whichever versions you need:

asdf install erlang 22.2.3
asdf global erlang 22.2.3
asdf install elixir 1.9.4-otp-22
asdf global elixir 1.9.4-otp-22

And to use a specific version in any particular directory:

asdf global elixir 1.9.4-otp-22

Installing Hex and Phoenix

mix local.hex

Phoenix uses Brunch.io which relies on npm, the Node.js package manager, so lets install that as well via asdf:

asdf plugin-add nodejs
brew install gpg
bash ~/.asdf/plugins/nodejs/bin/import-release-team-keyring
asdf install nodejs 13.7.0
asdf global nodejs 13.7.0

To install Phoenix:

mix archive.install hex phx_new 1.4.12

Finally, check your versions by:

elixir -v
mix phx.new --version

Doesn’t that feel good? Now you can install your favourite code editors!

Code Editors

I’m going to install two here, TextMate and MacVim 😀

TextMate 2

Download from here: https://macromates.com/download

Download my Elixify theme and follow the instructions from here (but you just need to double click on the TextMate 2 theme called Elixify.tmbundle)

Install Elixir Bundle:

mkdir -p ~/Library/Application\ Support/TextMate/Pristine\ Copy/Bundles
cd ~/Library/Application\ Support/TextMate/Pristine\ Copy/Bundles
git clone git://github.com/elixir-lang/elixir-tmbundle Elixir.tmbundle

MacVim

brew install MacVim

Next you’ll want to use Janus, which can be installed by running this in terminal:

curl -L https://bit.ly/janus-bootstrap | bash
~/.gvimrc.after

In it paste:

color elixify
set guifont=Monaco:h12
let g:NERDTreeWinPos = "right"
set guioptions-=T " Removes top toolbar
set guioptions-=r " Removes right hand scroll bar
set go-=L " Removes left hand scroll bar
autocmd User Rails let b:surround_{char2nr('-')} = "<% \r %>" " displays <% %> correctly
:set cpoptions+=$ " puts a $ marker for the end of words/lines in cw/c$ commands

Then in terminal, to give it some breathing space at the top do:

defaults write org.vim.MacVim MMTextInsetTop ‘5’

To install the Elixify theme download from here and follow the instructions for MacVim.

mkdir ~/.vim/colors
cd ~/.vim/colors
mvim Elixify.vim

That’s it – you now have a fully working dev environment for Ruby, Elixir, Erlang, Node and any other language you want to add via the numerous supported by asdf… which I am really loving btw!!