Skip to content

Instantly share code, notes, and snippets.

@monicao
Last active December 8, 2021 01:51
Show Gist options
  • Save monicao/d372716cdfbb7e9cf692 to your computer and use it in GitHub Desktop.
Save monicao/d372716cdfbb7e9cf692 to your computer and use it in GitHub Desktop.
Setting up your own Ruby Dev Environment on a Mac

Setting up the Ruby dev environment on Mavericks

Why would I want to do that?

  • You want to run guard
  • You want use Sublime plugins (like RSpec or Guard plugins)

Installing brew

Brew aka Homebrew is a package management system for Mac OSX.

Open up the Mac Terminal and run this command to install brew on your mac:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Install ruby through rbenv

Now that we have Homebrew installed, we can use it to install Ruby.

We're going to use rbenv to install and manage our Ruby versions. rbenv makes it really easy to have several versions of ruby on your laptop and switch between them.

To do this, run the following commands in your Terminal:

brew install rbenv ruby-build

# Add rbenv to bash so that it loads every time you open a terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile

# Install Ruby 2.1.1 and set it as the default version
rbenv install 2.1.1
rbenv global 2.1.1

ruby -v
# ruby 2.1.1

Check out this article for a list of rbenv commands: http://cbednarski.com/articles/installing-ruby/

Install rails

Install rails on your system so you can create new rails projects.

gem install rails

Rails is now installed, but in order for us to use the rails executable, we need to tell rbenv to see it:

rbenv rehash

And now we can verify Rails is installed:

rails -v
# Rails 4.1.1

Install the Postgres.app

"The easiest way to run Postgres on your Mac"

http://postgresapp.com/

Install the pg gem

Here's how to install the pg gem with Postgres.app. http://stackoverflow.com/questions/20224483/cannot-install-pg-gem-in-mavericks-with-postgres-app

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config

Install the command line developer tools

If you have XCode < 5.1.1

The nokogiri gem needs these. See here for details: http://jasdeep.ca/2013/10/installing-nokogiri-fails-os-x-mavericks/

xcode-select --install

If you have XCode > 5.1.1

Follow these instructions: http://stackoverflow.com/questions/23260748/manual-install-of-xcode-command-line-tools-not-working

  1. Open XCode
  2. Click on Xcode > Open Developer Tools > More Developers Tools This will take you to the Downloads for Apple Developers web site. Use your AppStore Id to login.
  3. Download Command Line Tools for your Mac OS X version. (Probably Mavericks April / March 2014).

See if it worked

gem install nokogiri

Install the gems for a Rails project

  cd /Users/monica/lighthouse/laser_shark
  bundle install

Follow the other setup instructions for that rails app. For laser_shark, you will have to edit your /etc/hosts, for example.

Change database.yml

In config/database.yml, make sure you have the following settings. Make sure you have the host, the password is blank, and the username is set to your computer's username. If you are not sure what your computer's username is, try running whoami in the terminal.

development:
  adapter: postgresql
  database: your_project_development
  host: localhost
  encoding: unicode
  pool: 5

test:
  adapter: postgresql
  database: your_project_test
  host: localhost
  encoding: unicode
  pool: 5

Here's my database.yml for a rails project called hooligans

development:
  adapter: postgresql
  database: hooligans_development
  host: localhost
  encoding: unicode
  pool: 5

To check if this works, see if you can create the database.

bin/rake db:create

Troubleshooting

Could not connect to server: Connection refused

If you see this error, make sure Postgres.app is started. You should see an elephant icon in your mac's top bar.

SYS:laser_shark SYS$ bin/rake db:create
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
/Users/SYS/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activerecord-4.1.1/lib/active_record/connection_adapters/postgresql_adapter.rb:881:in `initialize'

Help Improve this

I wrote this down from memory. If any of the steps here don't work, please fork this gist, fix the issue and share with the class.

@kvirani
Copy link

kvirani commented Jun 22, 2014

Thanks for this @monicao

AFAIK if you using postgres.app (as this gist suggests) you could remove the username and password fields from the database.yml file entirely.

Also, vagrant uses RVM, and that's what I'm used to using. My understanding is that functionality-wise they achieve the same results. In our vagrant box, we use RVM.

@eubenesa
Copy link

@monicao @kvirani

I made some additions to the Install the Postgres.app section:
https://gist.github.com/eubenesa/d1128f8c4b594710ccd6#install-the-postgresapp

Also, I switched to rbenv from RVM during like W4 of the bootcamp. I find rbenv cleaner, and it seems to play nicer with Zsh / Oh My Zsh. With rbenv, you can set Ruby version globally and locally for an individual project, etc. Just my two cents.

@astopo
Copy link

astopo commented Sep 2, 2014

Thanks for this Monica! Didn't run into any problems.

@KazW
Copy link

KazW commented Sep 30, 2014

@monicao @kvirani
I'd recommend avoiding the use of Postgres.app, installing it through brew is less hacky: no pg_config passing and no modification of the database.yml file (works out of the box after rails new). Unix sockets have caused less issues for us since authentication is handled implicitly.

If using brew, why not just do a brew install rvm postgresql? 😃 This is DRYer from a sys-admin perspective and it has a better removal path, Postgres.app isn't something a normal user can do (uninstalling is more complex than deleting the app).

Also, I just open sourced the Foodee Dev-Kit: https://github.com/Foodee/Dev-Kit

@monicao
Copy link
Author

monicao commented Sep 30, 2014

Thanks for the feedback guys!

I liked the idea of the Postgres App because it's easy to see if the database is started (icon in the task bar).
But I see your guys' point about database.yml... Updating the gist to use brew install postgresql instead.

@kweestian
Copy link

@monicao
Copy link
Author

monicao commented Dec 2, 2014

Thanks @kweestian. Updated the gist.

@kvirani
Copy link

kvirani commented Dec 22, 2014

@monicao / @KazW any good writeups / gists on setting up a ruby + pg + git (web dev) env for linux users? Something easy and detailed enough that we can give our Lighthouse "linux" students on Day1 (or even before day1)

I ask b/c we want Windows / Mac users to use vagrant but don't really push it on students with Linux laptops

@monicao
Copy link
Author

monicao commented Jan 3, 2015

@kvirani This guide looks good to me. The only thing is that they use rbenv. https://gorails.com/setup/ubuntu/14.10

@alberlau
Copy link

This helped me to resolve following issue:

Installing io-console 0.5.6 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory: /private/var/folders/2m/jvgr860s46qf54r5zm3k8kv00000gp/T/bundler20201126-55841-61jf5fio-console-0.5.6/gems/io-console-0.5.6/ext/io/console

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r ./siteconf20201126-55841-1ufmbek.rb extconf.rb
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.

Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/$(RUBY_BASE_NAME)
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:467:in try_do': The compiler failed to generate an executable file. (RuntimeError) You have to install development tools first. from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in block in try_compile'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:534:in with_werror' from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:585:in try_compile'
from /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/mkmf.rb:852:in macro_defined?' from extconf.rb:7:in

'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

/var/folders/2m/jvgr860s46qf54r5zm3k8kv00000gp/T/bundler20201126-55841-61jf5fio-console-0.5.6/extensions/universal-darwin-19/2.6.0/io-console-0.5.6/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /var/folders/2m/jvgr860s46qf54r5zm3k8kv00000gp/T/bundler20201126-55841-61jf5fio-console-0.5.6/gems/io-console-0.5.6 for inspection.
Results logged to /var/folders/2m/jvgr860s46qf54r5zm3k8kv00000gp/T/bundler20201126-55841-61jf5fio-console-0.5.6/extensions/universal-darwin-19/2.6.0/io-console-0.5.6/gem_make.out

An error occurred while installing io-console (0.5.6), and Bundler cannot continue.
Make sure that gem install io-console -v '0.5.6' --source 'https://rubygems.org/' succeeds before bundling.

In Gemfile:
irb was resolved to 1.2.4, which depends on
reline was resolved to 0.1.4, which depends on
io-console

I executed the following:

brew update
brew install coreutils
\curl -sSL https://get.rvm.io | bash -s stable --ruby
rvm install 2.6
rvm --default use 2.6.5
ruby -v

@monicao
Copy link
Author

monicao commented Nov 28, 2020

Thanks for posting this Albert!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment