Chapter 2: Installing Ruby On Rails on a Mac

2.1 Terminal and Unix setup

UNIX

  • the backbone of Mac OS X.
  • can’t do that from the graphical user interface
  • use a command line application called Terminal.

Command

command Description
cd directory change directories
cd ~ go back to my home directory
cd .. go back up one directory
ls a listing of what’s in the current directory
Tab key auto complete name
ls -la give us a list of all the things in the directory including hidden configuration files
echo ‘hello’ return a value
echo $SHELL return the value of a special constant that’s set up called shell
which show where a program is located
which echo show where echo is located : /bin/echo
cat.bash_profile output the contents of file .bash_profile
cmd K clear terminal screen

Note:

  • When we’re in UNIX, we’re not just in UNIX. We’re also in a working environment: the Bash shell. By default on a Mac, it is the born shell called bash.
  • the location of that file is important, because it is possible to have more than one copy of an application on your computer.But since we’re on the command line, and we’re just simply typing something like echo, it needs to know which one it should go and find.
    • the way it decides is by using another variable called Path
echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
  • This is my current path, the places that it will look for commands and the order that it will look for them. So first it will look in user/bin to see if I have echo in there. If I don’t, then next it will look inside /bin to see if it is there. Then we’re looking user/sbin, sbin, /opt/X11/bin, and finally in this path here.

  • Note that it got it on the second one here,\bin, that’s where it found echo, so it didn’t find it in user\bin, he found it in just bin. This is very important.
    • If Unix isn’t finding programs that you’re looking for, it may be because your $PATH does not include the path where that’s located. It’s not going to look anywhere else. It’s going to look in these places, that are in this list /usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin, and then, it’s going to give up and say, sorry, I can’t find it.
  • .bash_profile and .profile are basically the same things. .bash_profileis just for the bash shell, whereas .profile is for all shells. I typically tend to use bash_profile just to make sure that it’s clear that these are the instructions I want to use with bash. You don’t want to have both unless you really are certain about what you are doing, because they can conflict with each other.

    • text editor nano eg. use nano .bash_profile to open up .bash_profile
    • .bash_profile:

      PS1="David$ "
      alias ll="ls -lahG"
      export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
    • PS1 will change default prompt shhhappy$ to be David$
    • alias alias ll to be equal to ls -lahG It’s the same thing as ls-la that I’ve been typing with h and G just to make it look a little prettier.
    • export PATH lets $PATH be equal to /usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH The directory we first want to look in to see if we have anything installed is /usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin. i.e Before looking in the default places $PATH, check a few of my custom folders/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin first.
    • This user/local is where we’re going to be storing things and that’s a very common way to do it so that all of your programs that you install will all be installed in your user local folder. They’ll be kept there and separate from everything else.
    • the .profile and .bash_profile get run whenever a new terminal window opens. You can close terminal and open again or just type source ~/.bash_profile.

2.2 Xcode

  • On the Mac, the very first thing we need to do, before we install any other Software, is to install XCode.
  • XCode is a package that contains Apple’s developer tools.
  • In particular, we need to install a program called the Compiler which will allow us to prepare open-source software.
    • For use on our computer, XCode contains a compiler called gcc. ###Install commandline tool for Xcode this step is necessary.

2.3 Homebrew

  • need easy ways to find open-source software, to be able to install it and then to manage it over time. Either upgrading to new versions or deleting it in the future.
  • the open-source world offer a lot of different package managers to help us with this process. One of the best package managers for Mac is called Homebre, and we will install Homebrew next because it will make it easier for us to install the other components of the software that we’ll need.

Install Homebrew

  1. In your terminal type
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. verify the version of Homebrew
brew -v

Approximated result

Homebrew 0.9.5
  1. To make sure Homebrew is feeling alright, type:
brew doctor

If it asks you to run brew update, do so.

 If it returns:
 “No such file or directory - /usr/local/Cellar”
 You should type and enter:
 cd sudo mkdir /usr/local/Cellar
 Enter your login password and press enter (you won't be able to see it)
 If it returns:
 Error: No such file or directory - /Library/Developer/CommandLineTools/usr/bin/clang 
 You should update Xcode CommandLineTools (https://developer.apple.com/downloads/index.action#)
  1. To update Homebrew:
brew update

2.4 Ruby

Ruby Version

There’s three main versions of Ruby which you’re likely to come across. These are recent versions.

  • Version 1.8
    • the version that was released a long time ago but it was very popular and it stuck around for a long time.
    • It was a large span of time before we released 1.9. So 1.8 is out there. And very popular.
    • In fact it came pre-installed on all Mac Operating Systems starting with version 10.5 (Tiger) through through Mac OS 10.8(Mountain Lion).
    • Version 1.8 is pre-installed and ready to go. Now most cases that’s version 1.87.
    • no longer supported (dead software that will have no any more bug fixes or anything like that.
  • version 1.9.2 or higher is required for Ruby on Rails 4
  • version 2.0 or higher is preferred. -if you have Mac OS 10.9, you may already have Ruby 2.0 or something newer installed
  • No Mac ever ship with the 1.9 version for whatever reason, they’re going to go straight from 1.8 to 2.0.

Install options

  • www.ruby-lang.org
    • the place that you can go to find out the most recent information about the Ruby language, and there’s also some tutorials, some documentation there that can be really useful.
  • Homebrew
    • can install Ruby directly

Even better, and more popular in the Ruby and Rails development community is to use a manager for Ruby:

  • Ruby Version Manager (RVM)
  • RBENV

what’s especially great about them is they let you install multiple versions of Ruby and switch between them and even have projects that are on different versions.

over the last year, rbenv has become the more popular choice in the developmentcommunity.

Using RVM to Install Ruby

RVM is short for Ruby Version Manager. RVM makes it easy to install different versions of Ruby on your computer and manage them as well

  1. To install RVM, type this in your terminal
\curl -L https://get.rvm.io | bash -s stable

2.Close your terminal and then re-open it. Now, lets see if RVM was loaded properly:

type rvm | head -n 1

3.Now install Ruby with:

rvm use ruby --install --default
ruby -v

Using rbenv to Install Ruby

  1. To install rbenv, type this in your terminal
brew install RBENV

this will install Ruby Env for you

  1. type this in your terminal
brew install ruby-build
  • This is the little program that helps out Ruby env.
  • allows it to create new versions of Ruby,which you can use.
  • the building part of it is a little separate from the management portion
  1. add a line code at the end of .bash_profile, which tells rbenv to initialize itself
eval "$(rbenv init -)"
  1. Now check current ruby verion compared with version from www.ruby-lang.org , get the newest version (e.g. Ruby 2.1.5)
ruby -v

assume the current version is 1.8.7

5.Install the newest version of Ruby

rbenv install 2.1.5

Ruby will be installled to /user/shhhappy/.rbenv/versions/2.1.5

6.check current ruby verion

ruby -v

the result is still 1.8.7

  1. Type in this code line in terminal
rbenv rehash

rbenv rehash is a very important command. Any time that we install a new version of Ruby, or, when we install any kind of a Gem that also provides commands in the command line.

8.check ruby verion again

ruby -v
  • the result is still 1.8.7
  • That’s because we need to tell it we want to use the 2.0 version globally.
  1. make 2.0 version globally
RBENV global 2.1.5

-in addition to global, we ca use local, which would be for a certain project. - local would then set that for that project or shell, which means to just write now in the currently running window. Just switch there for a minute. Don’t switch globally. Don’t switch my project. Just quickly switch over, while I do some things, into this new version.

  1. check ruby verion again
ruby -v
rbenv versions
  • the result is 2.1.5
  1. in addition to rbenv install there’s also uninstall which would allow us to delete old versions if we don’t need them any more.

2.5 RubyGems

  • We’ve already installed two package manager
    • installed homebrew to manage open source apps
    • installed rbenv to manage our Ruby versions
  • RubyGems is another package manager
    • will manage our Ruby helper libraries and plug ins that our Ruby on Rails application is going to use.
    • A single RubyGem or just called Gem for short, is simply ruby code that has been packaged up for easy distribution using the package manager.
  • Rails makes extensive use of RubyGems
    • Rails is itself a Gem

      gem -v

      used to find out if you have RubyGems installed.

      which gem

      gives a clue about how that got installed. You can see that it’s in this ./rbenv folder, inside shems. And that lets me know that during the process of installing RBN and Ruby, it also installed RubyGems at the same time.

  • if you didn’t install Ruby using RBN, maybe Apple had already installed it for you. Or you use an alternative method like rvm, then you may see a different location here, and maybe you get a different version.

Get List of RubyGems

gem list

show the gems that are currently installed.

Update RubyGems

 gem update --system

That will go out to the RubyGems server. Look and see if we have the latest version and update it if necessary.

Get Help for RubyGems

 gem --help

that will give you more information about how you can go about using RubyGems. a good place to look for help and guidance.

RubyGems website

  • RubyGems website
  • All of the different gems that are published get put here on rubygems.org.This is the central repository, the place where everyone can look for these gems.
  • You can search for different gems

2.6 Ruby on Rails

  • Ruby on Rails is actually a Ruby Gem
    • because we have the Ruby Gems package manager installed, it’s going to make it really, really simple for us to install and manage different versions of Ruby on Rails.

Install Bundler

  • Now the first gem that we’re going to install acctually won’t be rails. We’re actaully going to start by instaelling somtething called bundler.
    • Bundler is a tool that helps your rails application to load the right ruby gems
    • keeps track for us and makes sure that each gem gets loaded, when it needs to be
    • Imagine for a moment that we have more than one ruby on rails application on our computer. The first application might be older and want to use version 1 of a particular gem. While another, more up to date, ruby on rails application, might want to use the newer version 2 of the gem. And many gems require other gems to function, and those have different versions too. Back in rails version 2, keeping all of these gem versions straight and making sure the correct ones were loaded, became a big headache. So, bundler was added in rails 3, to manage the gems that each application needs.
gem install bundler

his will tell the Ruby Gem’s package manager to go to the Ruby Gem’s website to look for the latest version of Bundler, to download it and then install it.

Bundler allows us to run commands from the command line, too

Note: you need to type rehash whenever there’s something that’s going to have a command line command that needs to be available.

rbenv rehash

In this case, it will make bundler avaliable

bundler -v

tells the version of Bundler

which bundler 

tell me where bundler is located,and you can see that it’s located in that rbenv folder. The rehash command is what put it there

Install Ruby on Rails

gem install rails

But before we hit return there are a couple of options that we could provide here. You may have noticed when it installed bundler that it also installed some documentation so whenever we install ruby gems its going to want to install that documentation as well. That documentation just sits quietly on our hard drive taking up space and almost no one ever refers to it. And for Rails, that documentation is quite extensive. So we can skip the download and processing of that documentation by passing it in the --no-ri --no-rdoc and it will not generate either the ri or rdoc documentation for this.

gem install rails --no-ri --no-rdoc

It speeds up the installation process and it doesn’t take up space on your hard drive with documentation that you’re never going to look at.

But we may not want the latest version. It depends on when you’re watching this training. Now this training will work with all versions of Rails 4.0. If we get to version 5.0 of Rails, it may still work but if you need to specify, if you need to go back and pick a previous version.

gem install rails --no-ri --no-rdoc --version 4.0.0
rbenv rehash

will make rails avaliable

rails -v
which rails

will check Rails version and location - which rails will show us that it added that shim to the rbenv folder

So we type list gem list and we’ll see a list of all of the different gems that it installed. You can see that Rails has lots of dependencies, other gems that it depends on.

actionmailer (4.1.7, 4.1.4)
actionpack (4.1.7, 4.1.4)
actionview (4.1.7, 4.1.4)
activemodel (4.1.7, 4.1.4)
activerecord (4.1.7, 4.1.4)
activesupport (4.1.7, 4.1.4)

is the core parts of Ruby on Rails

  • you’ll just work with rails and rails will call on these gems when they’re needed.

2.7 MySQL

When developing web applications, we’re going to use a database to store information

Three steps to getting MySQL installed.

  1. install MySQL itself
  2. set a default MySQL password
    • it is not an essential step but it’s a best practice
  3. Install MySQL RubyGem -so that Ruby and our Ruby libraries like Rails will be able to talk to MySQL as fast as possible.

To check and see if you have MySQL installed,

mysql --version 
  • Any version 5 will be just fine, but it’s not pre-installed by default on Mac OS 10, need to add it if you haven’t used it before.

Install MySQL From MySQL website

  1. go to http://dev.mysql.com
  2. look for the Downloads link
  3. look for MySQL Community Server
  4. download and install community server version of MySQL (community server version)
    • community server version is suitable for use when developing on your local computer and also for deploying a finished Rails application to a production server as well. -When people talk about using MySQL, chances are they’re talking about using the MySQL Community Server.

Three key criteria that determine which version we’re going to choose

  • the Mac OS X version that you have
    • if you have 10.6, well obviously you want to download a 10.6 version. If you have 10.7, or if you have 10.8 and there’s not a 10.8 one listed here, or something even newer, go ahead and download the most recent one.These 10.7 versions do work with Mac OS 10.8.
  • these choices come in two formats
    • either compressed tar archives or DMG archives -We’re going to want the DMG archive. That’s the one that lets us launch an installer from our desktop
    • The tar archive is really for installing the other command line,
  • pick 64 bit. Or, 32 bit
    • difference between them is based on what kind of processor you have installed on your Mac.
    • Chances are you have a 64 bit processor.
    • Intel core solo or Intel core duo : 32 bit processor
    • Intel Core 2 Duo is 64 bit processor

Install MySQL From MySQL Homebrew

brew install mysql one nice thing is that when it goes out to the server, it knows what your processor is and it knows what your operating system version are.

\\To connect to server:
    mysql -uroot (or mysql -u root)
\\ launchctl means launch control
\\To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
\\Then to load mysql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
\\Or, if you don't want/need launchctl, you can just run:
    mysql.server start
\\other options
 mysql.server stop
 mysql.server restart 
 mysql.server status

if error occurs

 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)        

solution: check system preference -> mysql, the status was STOP. Just restart it and it works fine on my mac now.

RUN MySQL

To check version of MySQL and where it is installed:

$ mysql --version 
mysql  Ver 14.14 Distrib 5.6.21, for osx10.8 (x86_64) using  EditLine wrapper
$ which mysql
/usr/local/bin/mysql

To connect to server:

mysql -u root 

Note:If you had previously installed this and secured it with a root password you might also need to provide the -p option which would let you then type your password.

Set up MySQL Password

To secure MySQL’s root password

mysqladmin  -u root password 

Note: If you had a previous password you could provide the -p option.

mysqladmin  -u root password -p

It would ask for your old password first, and then your new password.

Run MySQL with Password

mysql -u root -p

input exit to exit SQL

Install MySQL RubyGem

gem install mysql2

Note:

  • mysql used to be the main gem that we would use for Ruby on Rails but there is a new and improved version. And the way that you get it ismysql2. It’s actually a completely separate version created by someone else but it’s the version that we should be using not just mysql but mysql2.
  • it says building native extensions, because it actually builds code for us that’s in something like c so that it’s able to run really, really fast.

2.8 Web Server

back-end pieces installed

All of the back-end pieces installed: - Ruby - Ruby Gems - Rails - MySQL

web server in production

we need to have a web server when we’re in production. - When we put this out there on the internet for people to access, they’re going to come with their browser. They’re going to type in a URL, and it’s going to connect to our web server. To return a request to them. And that web server will then in turn talk to our Rails application to figure out what it ought to return.

web server in development

the same thing is to be true in development. We’re going to follow the exact same process, we’re going to have our browser connecting to a web server, talking to our Rails application.

which server do we choose for our development environment?

the main choices of web server:

Web server: Apache 1 or 2

  • There’s a lot to recommend Apache. It’s the most popular, web server out there.
  • it ships with, Mac OS X. So you already have it installed.
  • if you go to System Preferences > Pick Sharing, and then, turn on Web Sharing, you’ll be turning on Apache, so that Apache will then be listening for web requests and returning web pages.
  • In my PHP tutorials, that’s what we used to be able to return PHP pages.
  • There’s also a piece of software for Apache called Passenger
    • referred to sometimes as mod_rails,
    • that’s an Apache module, which allows Apache to talk to Rails so that we have an interface between Apache and Rails. There’s definitely a great combination and I would say that probably the vast majority of rails driven websites out there in production are using Apache 2 with Passenger

Web server: Engine X

  • a new competitor to Apache.
  • It’s very popular. Gaining quickly in popularity.
  • Has a lot of the same feature that Apache has.
  • Has a lot of features, lots of configuration option

Web server: lighter weight options

  • Lighttpd (“Lighty”)
  • Mongrel
  • WEBrick

Advantage:

  • lighter weight: they don’t have all of the features, they don’t necessarily have the same robustness(A characteristic describing a model’s, test’s or system’s ability to effectively perform while its variables or assumptions are altered.) that Apache and Engine X have, but they do have more speed.
  • They really try and focus on speed. Leave out the features, focus on the speed

Combination Web servers

  • In a more advanced apps, a lot of times you’ll see people sort of mixing and matching and combining these different web servers to be able to respond to requests as quickly as possible, but still have all of those features.

Web server In this tutorial: WEBrick

  • a very simple webserver that ships with Rails. i.e installed when Rails installed
  • recommend that you stick with WEBrick. It’s preinstalled, preconfigured to use it.
  • just does one thing and does it well. It takes your browser request, talks to your Rails application, returns the results.
  • easy and there is no configuration involved

2.9 Text editor

Need a good text editor which you can use for writing your code

key features

code coloring or syntax highlighting

  • it understands the language that we’re programming in well enough
  • different parts of the language can be assigned different colors.
  • for example if we are programming in Ruby, then Ruby class name might be in green, a Ruby method name might be in red, and a variable name might be in blue. It makes it very easy to scan the document and find what we’re looking for, instead of just having to read black text on the white background.
  • need to have some knowledge about the syntax for Ruby, Rails, HTML, CSS, and JavaScript, and can color it appropriately.

easily navigate a whole project at once

  • very important in Rails, because a lot of code be broken up between different files, and we’re going to have code calling from one file to another. So we’re going to be bouncing back and forth, making one change here, flipping to another file, making it change there.
  • want something that’s like a Project Window, or the ability to have open file tabs that will very easily allow us to flip back and forth between different pages. We don’t want to have to go back to Mac OS10’s Finder every time we need to reopen a new file.

good search and replace

  • want to be able to search your entire project and find every time that you’ve called a certain method name or used a certain variable

auto-pairing of brackets, parentheses and quotes

  • By far the most common mistake that developers make in programming is forgetting to close a parenthesis or quotation mark.
  • If you have a program that’s auto-pairing those for you, then as soon as you type the first open parenthesis. It types the second closed pareenthesis at the same time. When you keep typing it types between them that we you always make sure you have both the open and closed parenthesis that are automatically paired.

prefered features

auto indent

  • don’t have to keep hitting Tab to indent your code every time
  • automatiaclly indent you to the right spot

code completion

  • You type a few characters. You hit a magic key, like Tab. And it says, oh, I know what you’re trying to type. And it will type the rest for you. It will save you quite a few keystrokes and let you get your ideas into code faster

themes

  • customize the color of your document
  • to change not only the document in the background color, but the actual syntax coloring as well.

Text editor in this Tutorial: sublime text

  • we’re going to be using Sublime.
  • become the most popular editor in the rails community.
  • excellent features
  • a good price. They offer a free trial.

download sublime text

configuration sublime text

in termimal

1.get $PATH

echo $PATH
  1. install a small little symbolic link
  • symbolic link
    • a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution)
ln -s  /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl  /usr/local/bin/subl
  • this tell it to point to a small little program that’s inside sublime that will allow us to use sublime from the command line
  • making an alias from this location /usr/local/bin/sublto this location to ’/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl`
  1. Open file or folder
subl filename
subl folderName
  • open sublime with this file already loaded
  • an extremely handy tool to have
  • to open up an entire folder to view a whole set of files and folders at the same time nad view them as a project

  • Now sublime has lots of features. You can go to the sublime text website to find documentation and find out how to get the most out of it

other editors and IDEs

  • IDE stands for Integrated Development Environment
  • TextMate
  • RubyMine
  • RadRails
  • Eclipse
  • Netbeans
  • Komodo
  • Coda
  • MacVim