Start Ruby on Rails on Mac

2020-06-10 hit count image

Let's see how to install Ruby on Rails and start a project on Mac.

Outline

Recently, I have a chance to start Rub on Rails project. In this blog post, I will introduce how to install Ruby on Rails on Mac and start a project with installed Rails.

This blog post is a series. You can see the other posts in below.

Also, you can see the this blog post sample source code on Github

Install Homebrew

We will use Mac pacakge manger Homebrew to install Rails on Mac. execute the command below to check Homebrew installed on Mac.

brew --version

If Homebrew was installed already, you can see the result below.

Homebrew 2.2.6
Homebrew/homebrew-core (git revision 93ac3; last commit 2020-02-18)
Homebrew/homebrew-cask (git revision 373c1; last commit 2020-02-18)

If you get the result above, go to the next step. If you don’t get it, execute the command below to install Homebrew.

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

After installing, execute the command below to check Homebrew installed.

brew --version

If Homebrew was installed well, you can see the result below.

Homebrew 2.2.6
Homebrew/homebrew-core (git revision 93ac3; last commit 2020-02-18)
Homebrew/homebrew-cask (git revision 373c1; last commit 2020-02-18)

Install rbenv

Mac has basically Ruby. So, you can install Rails directly, but to support various versions of Ruby, we’ll install rbenv that is ruby version manager.

Execute the command below to install rbenv.

brew install rbenv ruby-build

After installing, you can see the result below.

==> Pouring rbenv-1.1.2.catalina.bottle.tar.gz
🍺  /usr/local/Cellar/rbenv/1.1.2: 36 files, 69KB
==> Caveats
==> ruby-build
ruby-build installs a non-Homebrew OpenSSL for each Ruby version installed and these are never upgraded.

To link Rubies to Homebrew's OpenSSL 1.1 (which is upgraded) add the following
to your ~/.zshrc:
  export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

Note: this may interfere with building old versions of Ruby (e.g <2.4) that use
OpenSSL <1.1.

And then, following the above, open .zshrc file and modify it like below.

# code ~/.zshrc
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix openssl@1.1)"

Initialize rbenv

After installing rbenv, execute the command below to initialize rbenv.

rbenv init

After executing the command, you can see the result below.

# Load rbenv automatically by appending
# the following to ~/.zshrc:

eval "$(rbenv init -)"

Following the above, open .zshrc file and modify it like below.

# code ~/.zshrc
eval "$(rbenv init -)"

After all settings, execute the command below to check all configuration are set well.

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

After executing, you can see the result below.

Checking for `rbenv' in PATH: /usr/local/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /usr/local/bin/rbenv-install (ruby-build 20200115)
Counting installed Ruby versions: none
  There aren't any Ruby versions installed under `/Users/jeonghean_kim/.rbenv/versions'.
  You can install Ruby versions like so: rbenv install 2.2.4
Checking RubyGems settings: OK
Auditing installed plugins: OK

Install other versions of Ruby

Let’s see how to install another version of Ruby on Mac. First, execute the command below to check current Ruby version.

ruby -v

After executing, you can see the screen below.

ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

Execute the command below to check Ruby versions that we can install via rbenv.

rbenv install -l

After executing, you can see Ruby version list like below.

...
truffleruby-1.0.0-rc3
truffleruby-1.0.0-rc5
truffleruby-1.0.0-rc6
truffleruby-1.0.0-rc7
truffleruby-1.0.0-rc8
truffleruby-1.0.0-rc9
truffleruby-19.0.0
truffleruby-19.1.0
truffleruby-19.2.0
truffleruby-19.2.0.1
truffleruby-19.3.0
truffleruby-19.3.0.2
truffleruby-19.3.1

There are so many versions, so execute the command below to get recent stable Ruby version.

rbenv install -l | sed -n '/^[[:space:]]*[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}[[:space:]]*$/ h;${g;p;}'

And then, you can see the result below.

2.7.0

Execute the command below to install recent stable Ruby.

rbenv install 2.7.0

Also, you can directly install recent stable Ruby via executing the command below.

rbenv install $(rbenv install -l | sed -n '/^[[:space:]]*[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}[[:space:]]*$/ h;${g;p;}')

Of course, you can install old version Ruby.

rbenv install 2.3.1

After installing, execute the command below to re-configuration new environment.

rbenv rehash

Fix Error

I got the error message like below when I installed.

Last 10 log lines:
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking minix/config.h usability... no
checking minix/config.h presence... no
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking for cd using physical directory... cd -P
checking whether CFLAGS is valid... no
configure: error: something wrong with CFLAGS="-I/usr/local/include -L/usr/local/lib  "

I’ve fixed it to modify ./zshrc file like below.

# code ~/.zshrc
export CFLAGS=""

Change version

First, execute the command below to check current Ruby version.

rbenv versions

You can see the result like below.

* system (set by /Users/jeonghean_kim/.rbenv/version)
  2.7.0

The * mark is currently selected Ruby version.

You can execute the command below to check Ruby version again.

ruby -v
# ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]

Execute the command below to change Ruby version.

rbenv global 2.7.0
rbenv rehash

And then execute the command below to check version. you can see the version is changed

ruby -v
# ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]
rbenv versions
#   system (set by /Users/jeonghean_kim/.rbenv/version)
# * 2.7.0

Install Bundler

Let’s install Bundler which is Ruby Gem manager. Gem is kind of libraries(packages). Bundler installs and manages the libraries(packages).

Execute the command below to install Bundler.

gem install bundler

After installing, execute the command below to check Bundler installed well.

bundler -v
# Bundler version 2.1.4

Install Rails

Let’s install Rails. Execute the command below to install Rails.

gem install rails
rbenv rehash

After installing, you can check Rails install well via executing the command below.

rails -v
# Rails 6.0.2.1

Install yarn

For creating Rails project and executing it, we need to install yarn. yarn is kind of manager to install javascript libraries and manage them.

Execute the command below to install yarn

brew install yarn

Create and start Rails project

Let’s make Rails project. Execute the command below to create new Rails project.

rails new StudyRails

If you got the message like below, when you create Rails project,

The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.

After creating, execute the command below.

cd StudyRails
bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java
bundle install

If you get the error message like below,

Your Ruby version is 2.6.3, but your Gemfile specified 2.7.0

The reason is bundle path is not recognized well. I solved it to add the contents below to .zshrc file.

# code ~/.zshrc
alias bundler=/Users/$USER/.rbenv/shims/bundler
alias ruby=/Users/$USER/.rbenv/shims/ruby
alias bundle=/Users/$USER/.rbenv/shims/bundle
alias gem=/Users/$USER/.rbenv/shims/gem
alias rails=/Users/$USER/.rbenv/shims/rails

After creating, execute the command bewlot to start Rails project.

# cd StudyRails
bundle exec rails server

If you get the message like below,

Warning: the running version of Bundler (2.1.2) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`.

Execute the commnad below.

bundle exec rails server

After running the server, you can go to http://127.0.0.1:3000/ on the browser.

Ruby on Rails result of running the server

If you have no problem, you can see Rails page like above.

Completed

We’ve seen how to change Ruby version and install Rails to start Rails project. I thought the setting is simple, but I’v had many problem because of path setting on .zshrc for jekyll blog.

export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH

If you have some problem, I recommand you to check zshrc and delete wrong path setting. Now, let’s play with Rails project!

Reference

This blog post is a series. You can see the other posts in below.

Also, you can see the this blog post sample source code on Github

Posts