How to install Rails on Mac OS 10.11 El Capitan and 10.12 Sierra

Rails

It is a server-side web application framework running on the Ruby programming language, which specializes in Convention Over Configuration (COC) and Don’t repeat yourself (DRY).

In order to get started with Rails, you must first install it on your computer. This tutorial will be focused on Mac OS, specifically in version 10.11 El Capitan and 10.12 Sierra. I will be sharing a blog post for Windows, as well as Linux.

Mac OS comes with Ruby pre-installed out-of-the-box (OOTB), but you’ll likely want to update it to a more recent version. We will be using Homebrew for this.

Disclosure

I will assume you already have Git installed and configured correctly. If you haven’t done so, make sure to check out the official documentation.

You can validate the install was successful by running:

git config --list

output: git config –global color.ui true

git config –global user.name “Jon Doe”

git config –global user.email “foo@bar.com

ssh-keygen -t rsa -C “foo@bar.com

Homebrew

A package manager for Mac OS which makes it easier to install files on your computer.

To install, copy the following command and paste it into your terminal:

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

That will install Homebrew and its corresponding docs / dependencies.

You can confirm that it installed correctly by running:

brew help

If you already had Homebrew installed, listen up:

I had to run the following commands to get it to work.

brew link --overwrite autoconf
brew link --overwrite pkg-config
brew link --overwrite rbenv
brew link --overwrite ruby-build

The next thing we need to set up is rbenv.

rbenv

A Ruby version management tool built by Sam Stephenson.

Installation:

brew install rbenv ruby-build

Add rbenv to our .bash_profile so that it is available to us whenever we open a Terminal.

echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile

The next step will be to enable the xcode command line developer tools.

xcode-select --install

Having done so, we proceed to finding our new Ruby build.

Ruby

To list available Ruby versions:

rbenv install --list

Select Ruby version (opt for the latest stable build, for me it is 2.3.0, yours may vary)

We will now run two commands:

rbenv install 2.3.0
rbnev global 2.3.0
  1. Sets a local application-specific Ruby version by writing the version name to a .ruby-version file in the current directory. This version overrides the global version.

  2. Sets the global version of Ruby to be used in all shells by writing the version name to the ~/.rbenv/version file.

Now, validate the install:

ruby -v

If everything went correctly, it should output the new Ruby version.

You can also test this by running:

rbenv version

Installing Rails

To add Rails (or any other valid gem, as a matter of fact), to your RubyGems use the command gem install.

gem install rails -v 5.0.2

In order to be able to execute it from the command line interface (CLI) or terminal, you will need to tell rbenv to see it:

rbenv rehash

This command installs shims for all Ruby executables known to rbenv (i.e., ~/.rbenv/versions/bin/).

Another useful command is rbenv which; it displays the full path to the executable that rbenv will invoke when you run the given command.

For example:

rbenv which rails

output: /Users/giovanniorlando/.rbenv/versions/2.3.0/bin/rails

You can validate the installation using:

rails -v

output: Rails 5.0.2

Rails comes with sqlite3 support built in, which is great for development and testing, but not my first choice for a production database (DB).

My relational database of choice is MySQL.

You can install it using Homebrew:

brew install mysql

This will setup a MySQL database without a root password. To secure it run:

mysql_secure_installation

The following step is optional

To launch MySQL on startup:

ln -sfv /usr/local/opt/mysql/*plist ~/Library/LaunchAgents

To launch now:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

By default the database user is <strong>root</strong> with no password. You can test the connection by running:

mysql --user=root mysql

If you have assigned a password to the root account, you must also supply a –password or -p option, e.g.

mysql --user=root mysql --password=foobar

After you have tested your connection, the next step would be to scaffold a sample project:

cd ~/Sites
ruby rails new myrailsapp -d mysql
cd myrailsapp
rake db:create
rails server
  1. Navigate into your work directory
  2. Choose a database engine:
    • To use sqlite3 (default): `ruby rails new myrailsapp`
    • To use MySQL (recommended): `ruby rails new myrailsapp -d mysql`
  3. Navigate into the project directory
  4. Use rake to spin up a database
  5. Serve locally

You can now access your app via http://localhost:3000

And there you have it, you have successfully install Rails, and created your first project.

To learn more about rails, make sure to check out their GitHub repository.

If you enjoyed this blog post or found it helpful in any way, make sure to follow me on Twitter to find out when a new one is available.

Twitter: @giovanni0918
Github: giovanni0918
Website: giovanni-orlando.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s