Kula.blog

How to set up Rails on a Mac

Pretty long story of frustration but at the top I've put tl;dr :)

I want to learn Ruby on Rails #

For the new job, I want to learn the Ruby on Rails.

It's best to follow the official RoR guide, right? I'm not sure anymore. Setting up Rails when you're not familiar with the ecosystem can be hard. In my case, the root of the problem turned out to be a bug in the current version of Ruby on Rails. The newly generated project doesn't work. It is confusing. Especially after seeing all kinds of warnings before this error during installation.

If you struggle with Error loading the 'sqlite3' Active Record adapter check the error at step 12.

Below is what finally worked for me. If you have general problems with installation then read Install Ruby on Rails 5.2 · macOS High Sierra
by Daniel Kehoe
. It helped me a lot!

TL;DR How to set up Rails on a Mac: #

  1. $ brew install ruby
  2. $ brew install gpg
  3. $ gpg --keyserver hkp://pgp.mit.edu --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
  4. $ source ~/.rvm/scripts/rvm
  5. $ rvm install ruby-head # $ rvm install ruby-2.6.0 should work as well
  6. $ gem update
  7. $ echo "gem: --no-document" >> ~/.gemrc
  8. $ gem install nokogiri
  9. $ gem install rails

Finally, create a new Rails project:

  1. $ rails new rails_blog

5.2.2.1 Ruby on Rails sqlite3 bug: #

  1. $ cd rails_blog
  2. $ rails server

Open localhost:3000 in your browser:

Puma caught this error: Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? can't activate sqlite3 (~> 1.3.6), already activated sqlite3-1.4.0. Make sure all dependencies are added to Gemfile. (LoadError)
~/.rvm/rubies/ruby-head/lib/ruby/2.7.0/bundler/rubygems_integration.rb:408:in `block (2 levels) in replace_gem'
~/.rvm/gems/ruby-head/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:12:in `<main>'
~/.rvm/gems/ruby-head/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'

Fix: #

It's a bug in Rails. Fix is on stackoverflow.

Change in Gemfile:

# Use sqlite3 as the database for Active Record
- gem 'sqlite3'
+ gem 'sqlite3', '~> 1.3.6'

Make sure to install it!

  1. $ bundle install

Time to try again!

  1. $ rails server

Finally localhost:3000 works! 🎉


default rails page screenshot




Long story #

I'm installing Ruby and Rails on my personal workstation. I won't need easy upgrades in the future. I won't need separate libs for different projects. I won't need RVM!

First attempt with global Ruby #

Install by Homebrew #

$ brew install ruby

==> Pouring ruby-2.6.2.mojave.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
/usr/local/lib/ruby/gems/2.6.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile

For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

I know it's long but it will show up later ;)

$ gem install rails

ERROR: While executing gem ... (Errno::EACCES)

Throw sudo at it!

$ sudo gem install rails

# I can't go back in time to see it
# But I got asked if I want to overwrite bundler
# y - Why not? If you need it ¯\_(ツ)_/¯

A lot of weird things happened. It seems it's ready to go!

Time to generate a new project #

$ rails new rails_blog

Your user account isn't allowed to install to the system RubyGems.
You can cancel this installation and run:

bundle install --path vendor/bundle

to install the gems into ./vendor/bundle/, or you can enter your password and install the bundled gems to RubyGems using sudo.

Hm... it looks weird. Why do I need to type a password when creating a new project?

Let's go with it for now.

Let's try! #

$ bin/rails server is straight from Rails Guide

$ bin/rails server
You must use Bundler 2 or greater with this lockfile.

But, I have no idea where it gets this old version?

$ bundle -v
Bundler version 2.0.1

But in the app:

$ bin/bundle -v
Traceback (most recent call last):
2: from bin/bundle:3:in `<main>'
1: from /usr/local/Cellar/ruby/2.6.2/lib/ruby/2.6.0/rubygems.rb:262:in `bin_path'
/usr/local/Cellar/ruby/2.6.2/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe'
: Could not find 'bundler' (1.17.2) required by your **********/blog/Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:1.17.2`
$ bundle update --bundler
Bundle updated!

# So if I try it?

$ bin/bundle update --bundler
You must use Bundler 2 or greater with this lockfile.
# But the problem is still not solved! :(

It's most likely user error on my part, but it's annoying. #

At this point, I started believing PSA: Do not use system Ruby...

But there is a workaround!

Change Gemfile.lock to use 1.17.2:

BUNDLED WITH
- 2.0.1
+ 1.17.2

Of course, the server doesn't work:

It's the bug I mentioned at the top. It was ALMOST working! 😡 I didn't realize how easy that was to fix.

Puma caught this error: Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? can't activate sqlite3 (~> 1.3.6), already activated sqlite3-1.4.0. Make sure all dependencies are added to Gemfile. (LoadError)
/usr/local/Cellar/ruby/2.6.2/lib/ruby/2.6.0/bundler/rubygems_integration.rb:408:in `block (2 levels) in replace_gem'
/usr/local/lib/ruby/gems/2.6.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:12:in `<main>'
/usr/local/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'

It seems that it uses 2.6.0 not 2.6.2!

I've tried reinstalling ruby. But, noticed homebrew message when installing the 2.6.2 version:

By default, binaries installed by gem will be placed into: /usr/local/lib/ruby/gems/2.6.0/bin

So it's not about the version of Ruby. Maybe sudo? Maybe replacing bundler during installation caused it?

Start over... #

Rails replaced some gems during installation. I remember that bunder was one of them. Why not reinstall the gems?

Uninstall all gems #

Thanks to: uninstall all installed gems in osx I knew what to do:

$ gem uninstall -aIx
ERROR: While executing gem ... (Errno::EACCES)

$ sudo gem uninstall -aIx
# Done!

Install Rails again #

$ sudo gem install rails

Generate a project #

$ rails new rails_blog

# ....

Your user account isn't allowed to install to the system RubyGems.
You can cancel this installation and run:

bundle install --path vendor/bundle

to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to RubyGems using sudo.

Installing bootsnap 1.4.1 with native extensions
Using bundler 1.17.2

To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:1.17.2`

Even if you accept. There is this old bundler in logs 1.17.2 >:|

Gave up. Uninstalling ALL! #

$ brew uninstall ruby
Uninstalling /usr/local/Cellar/ruby/2.6.2... (19,342 files, 32.3MB)

$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

Maybe Mac built-in bundler is old and it's used for some reason?

Time for RVM! #

It's a longer version of the tutorial at the top

Please remember to check official tutorial at rvm.io

Fist step #

$ gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

I have to install gpg2 it seems. Homebrew to the rescue!

$ brew install gpg2

$ gpg2
bash: gpg2: command not found

On RVM Security I've found:

We use GPG for signing. Both gpg and gpg2 should be fine.

Let's use gpg then! If only it would work!

Unfortunately keyserver that they suggest by default didn't work. There are alternatives and this one works:

$ gpg --keyserver hkp://pgp.mit.edu --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Install RVM #

\curl -sSL https://get.rvm.io | bash -s stable

and restart your terminal or source it to make sure you use it :)

$ source ~/.rvm/scripts/rvm

Install Ruby #

$ rvm install 2.6

Error running './configure --prefix=~/.rvm/rubies/ruby-2.6.0 --with-opt-dir=/usr/local/opt/libyaml:/usr/local/opt/readline:/usr/local/opt/libksba:/usr/local/opt/openssl@1.1 --disable-install-doc --enable-shared',
please read ~/.rvm/log/1553090290_ruby-2.6.0/configure.log
There has been an error while running configure. Halting the installation.

NOT AGAIN! 🤦‍

Let's try a couple of things

$ rvm autolibs homebrew
# nothing done
$ rvm autolibs packages
# nothing done

I've noticed there is a configure.log file. Wonder what's there?

configure: error: something wrong with LDFLAGS="-L/usr/local/opt/ruby/lib"

Btw. Why not give me this message in the console!?

Found similar issue on Github which made me check my .bash_profile:

export PATH="/usr/local/opt/ruby/bin:$PATH"
export PATH="/usr/local/lib/ruby/gems/2.6.0/bin:$PATH"

# For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"

# For pkg-config to find ruby you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Doesn't it look like what Homebrew message told me to do? Remove all!

export PATH="/usr/local/opt/ruby/bin:$PATH"
export PATH="/usr/local/lib/ruby/gems/2.6.0/bin:$PATH"

- # For compilers to find ruby you may need to set:
- # export LDFLAGS="-L/usr/local/opt/ruby/lib"
- # export CPPFLAGS="-I/usr/local/opt/ruby/include"

- # For pkg-config to find ruby you may need to set:
- # export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Install Ruby (again) #

For some reason, I thought that ruby-head is the newest stable ruby...

$ rvm install ruby-head

Warning! Requested ruby installation which requires another ruby available - installing ruby-2 first.

ruby-2.6.0 - #removing src/ruby-2.6.0..
Checking requirements for osx.
Certificates bundle '/usr/local/etc/openssl@1.1/cert.pem' is already up to date.
Requirements installation successful.
Installing Ruby from source to: ~/.rvm/rubies/ruby-2.6.0, this may take a while depending on your cpu(s)...
ruby-2.6.0 - #downloading ruby-2.6.0, this may take a while depending on your connection...
ruby-2.6.0 - #extracting ruby-2.6.0 to ~/.rvm/src/ruby-2.6.0.....
ruby-2.6.0 - #configuring......
Error running './configure --prefix=~/.rvm/rubies/ruby-2.6.0 --with-opt-dir=/usr/local/opt/libyaml:/usr/local/opt/readline:/usr/local/opt/libksba:/usr/local/opt/openssl@1.1 --disable-install-doc --enable-shared',
please read ~/.rvm/log/1553090891_ruby-2.6.0/configure.log
There has been an error while running configure. Halting the installation.

To proceed rvm requires a ruby-2 compatible ruby is installed.
We attempted to install it automatically but it failed with status 1.
Please install it manually (or a compatible alternative) to proceed.

So it seems that I need ruby 2 to install Ruby 2? WEIRD!

Install Homebrew Ruby again #

$ brew install ruby
==> Downloading https://homebrew.bintray.com/bottles/ruby-2.6.2.mojave.bottle.tar.gz
Already downloaded: ~/Library/Caches/Homebrew/downloads/6a94447f67df04ea4533c44b47aaad050fa8a7bbe7128027b08ca223f85b057e--ruby-2.6.2.mojave.bottle.tar.gz
==> Pouring ruby-2.6.2.mojave.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
/usr/local/lib/ruby/gems/2.6.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile

For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

==> Summary
🍺 /usr/local/Cellar/ruby/2.6.2: 19,342 files, 32.3MB

Install Ruby by RVM #

$ rvm install ruby-head

Installed! 🎉

Time to install Rails #

Following Install Ruby on Rails 5.2 · macOS High Sierra Tutorial
by Daniel Kehoe

$ gem update

$ echo "gem: --no-document" >> ~/.gemrc

It should speed up installations

$ gem install nokogiri

$ gem install rails


$ rails new rails_blog



Post-install message from sass:

Ruby Sass is deprecated and will be unmaintained as of 26 March 2019.

* If you use Sass as a command-line tool, we recommend using Dart Sass, the new
primary implementation: https://sass-lang.com/install

* If you use Sass as a plug-in for a Ruby web framework, we recommend using the
sassc gem: https://github.com/sass/sassc-ruby#readme

* For more details, please refer to the Sass blog:
http://sass.logdown.com/posts/7081811

run bundle exec spring binstub --all
* bin/rake: spring inserted
* bin/rails: spring inserted

First time seeing anything done in Dart. Why would you do that if you're not working for Google?

Turns out top contributors do work for Google. ¯_(ツ)_/¯

Let's try running it #

$ rails server taken from Install Ruby on Rails 5.2 · macOS High Sierra Tutorial
by Daniel Kehoe


$ rails server



Puma caught this error: Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? can't activate sqlite3 (~> 1.3.6), already activated sqlite3-1.4.0. Make sure all dependencies are added to Gemfile. (LoadError)
~/.rvm/rubies/ruby-head/lib/ruby/2.7.0/bundler/rubygems_integration.rb:408:in `block (2 levels) in replace_gem'
~/.rvm/gems/ruby-head/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:12:in `<main>'
~/.rvm/gems/ruby-head/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'

That's the same error as before!

Ugh...

It can't be bundler thing again...

I've seen it. I can't remember troubleshooting it. I should give it a try.

BOOM! First Google result!

It's a bug in Rails that's easy to fix.

add do Gemfile line:

gem 'sqlite3', '~> 1.3.6'

and run

$ bundle install

Finally localhost:3000 works! 🎉

That was way harder than it should have been. Let's start learning Rails!


← Home