I wrote Part I of this series to provide a foundation upon which to build a collection of minimal guides to developing software on a Mac. In this post let’s look at what needs to be installed on your Mac for developing a Rails application.
A Ruby Version Manager (But Not the One You Think)
Ruby language development moves at a fairly decent pace, and there was a time when it seemed like new versions of Ruby came out several times a year. In addition to that there are, or were, several Ruby flavors:
- Ruby Enterprise Edition (now EOL)
- Rubinius (appears defunct)
- JRuby
And, of course, there is just “Ruby”. Keep in mind that the Ruby language specification is open and anyone can develop an interpreter. This, coupled with the push to build high-performance interpreters for support applications such as a <href=”https://twitter.com”>Twitter or Airbnb led to a dizzying array of options, particularly in the early 2010s.
There was a time when I would have recommended RVM to manage all of this. Unfortunately RVM suffers from the desire to do way too much, and the last straw for me was when it decided to install its own copy of Homebrew, and to add insult to injury, in the wrong location.
Instead, we’re going to use ruby-install and chruby instead with Homebrew:
1 2 3 |
% brew install ruby-install % brew install chruby |
Take care to read the instructions after the install of chruby:
1 2 3 4 5 6 |
Add the following to the ~/.bash_profile or ~/.zshrc file: source /opt/homebrew/opt/chruby/share/chruby/chruby.sh To enable auto-switching of Rubies specified by .ruby-version files, add the following to ~/.bash_profile or ~/.zshrc: source /opt/homebrew/opt/chruby/share/chruby/auto.sh |
Now, let’s install a Ruby:
1 2 3 4 5 6 7 8 |
ruby-install 3.0 -- --enable-shared >>> Updating ruby versions ... >>> Installing ruby 3.0.4 into /Users/joe/.rubies/ruby-3.0.4 ... >>> Installing dependencies for ruby 3.0.4 ... ... ... ... >>> Successfully installed ruby 3.0.4 into /Users/joe/.rubies/ruby-3.0.4 |
You’re going to see a lot stuff downloaded, configured, compiled, and installed. This is why we wrote the original post about installing the very basic tools needed to begin development on a Mac.
If everything compiles successfully you’ll see >>> Successfully installed ruby 3.0.4 into /Users/
(or something similar depending upon the exact Ruby version). Yet, if you do a which ruby
you may see /usr/bin/ruby
which is still the system default.
Here is where chruby
comes to our aid:
# chruby
ruby-3.0.4
Note: You may need to reload your ~/.zshrc
after compiling a new Ruby for chruby
to see it.
So let’s switch to ruby-3.0.4
:
1 2 3 4 5 |
% chruby 3.0.4 % which ruby /Users/joe/.rubies/ruby-3.0.4/bin/ruby |
You might also be wondering what the -- --enable-shared
is about. Check out this post.
A Word about OpenSSL
You’ll encounter OpenSSL and it’s headers and libraries over and over (and over) again when doing almost any type of software development, particularly development involving making TLS or HTTPS connections. OpenSSL is open-source software and not included by default in many environments, but so much depends on it. To make matters worse, there are now two versions: OpenSSL 1.1 and OpenSSL 3.0. Don’t get discouraged when you start doing battle with OpenSSL.
gem and bundle
Two other must-have tools for doing development with Ruby are gem and bundler. In version 3.0.4 of Ruby both are these are present, so let’s create a basic Sinatra app!
1 2 3 4 5 6 7 |
% mkdir -p ~/projects/ruby/basic_sinatra_app % cd ~/projects/ruby/basic_sinatra_app % bundle init Writing new Gemfile to /Users/joe/projects/ruby/basic_sinatra_app/Gemfile |
Open the Gemfile
and edit it to look like:
1 2 3 4 5 6 7 |
# frozen_string_literal: true source "https://rubygems.org" git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } gem "sinatra" gem "faraday" gem "nokogiri" |
Then type bundle install
. You’ll notice the last line:
1 |
Use `bundle info [gemname]` to see where a bundled gem is installed. |
And looking at the nokogiri
gem:
1 2 3 4 5 |
bundle info nokogiri * nokogiri (1.13.9) Summary: Nokogiri makes it easy and painless to work with XML and HTML from Ruby. ... Path: /Users/joe/.gem/ruby/3.0.4/gems/nokogiri-1.13.9-arm64-darwin |
Now, this is a fine point, but one that needs to be made: for our basic_sinatra_app
project we installed gems in the “global” gem directory. Global is in quotes here, because it’s not global to the system, only to the user, and only to the specific version of Ruby.
To prove this to yourself, try this:
1 2 3 4 5 6 7 8 9 |
% ruby-install 2.7 % source ~/.zshrc % chruby 2.7 % gem info nokogiri *** LOCAL GEMS *** |
Here is where things typically go afoul, and that’s a developer’s machine will often have a number of different installations of Rubies, gems, and so on. It is easy to allow these environments to proliferate to the point you don’t know what’s installed where, or which version of Ruby you needed for the latest app you’re building.
On to Rails
We’re going to make a disclaimer here, and that is we’re using Rails 7. Why the disclaimer? With Rails 7.0 there has been a move away from dependencies on tools such as npm
that make the installation and use out of the box much easier. Don’t worry, npm
will be covered in the So You Want to Develop on NodeJS part of the series.
1 2 3 4 5 6 7 |
% chruby 3.0.4 % mkdir -p ~/projects/ruby/basic_rails_app % cd ~/projects/ruby/basic_rails_app % bundle init |
Edit your Gemfile to look like this:
1 2 3 4 5 6 |
# frozen_string_literal: true source "https://rubygems.org" git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } gem "rails" |
and bundle install
.
rails
is now at /Users/joe/.gem/ruby/3.0.4/gems/rails-7.0.4
. Oh, if that were the only step.
Typing bundle exec rails new .
and you’ll find that the environment wants to overwrite the Gemfile
, and that’s okay since we just created it. But, a Gemfile that was 6 lines is now 72 lines. There’s a lot there, but, if all works properly:
1 2 3 4 5 |
bundle exec rails s => Booting Puma => Rails 7.0.4 application starting in development ... * Listening on http://127.0.0.1:3000 |
Cleaning Up
In this post we installed two tools: ruby-install
and chruby
. In the process some additional packages were installed by Homebrew. How could we go about uninstalling everything? Well, good luck with that, and its that very problem that makes things difficult at times, or rather, more complicated than they need to be. At a minimum though we can easily delete the ~/.rubies
directory that ruby-install
populated, which in turn will remove any installed gems.
The Mac I’m using to write these posts I’ve deliberately tried to “keep clean” – after Part II I now have:
1 2 3 4 5 6 |
% brew list ==> Formulae autoconf ca-certificates libffi openssl@1.1 xz automake chruby libyaml readline bison gdbm m4 ruby-install |
Much of these were installed to help build Ruby itself, e.g., autoconf
, automake
, bison
, and so on. xz
is there to decompress the Ruby source “tarballs”.
You can see, though, that while it doesn’t take much to get started building a Rails application, we built upon the first post of obtaining the command line developer tools, Homebrew, and so on. All software development, not just on the Mac, is like this. What starts as one install leads to another leads to eight more.
Until next time.