Start by installing homebrew :-)
OS X comes with Ruby installed but it’s an older version, and you don’t want to be messing with core files so it’s better to use rbenv and ruby- build to manage and install your Ruby development environments.
Install both using Homebrew. Once done, add a line to your
~/.bash_profile
and reload your terminal profile.
$ brew install rbenv ruby-build
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
They allow you to install different versions of Ruby and specify which version to use on a per project basis. This is very useful to keep a consistent development environment if you need to work in a particular Ruby version.
Install the latest stable of Ruby (check the Ruby website). To see a list
of all available versions to install use rbenv install --list
.
$ rbenv install 2.0.0-p247
$ rbenv rehash
To set this version as the one to use globally so that you can make use of it in your terminal, do this:
$ rbenv global 2.0.0-p247
Ps: You need to run the rbenv rehash
after you install a new version of
Ruby.
Ps 2: Check out more commands in the rbenv
readme on Github. It’s worth
bookmarking that page for reference later, or there is always rbenv --help
.
The most common are:
Bundler is a Ruby gem manages an application’s dependencies, kind of like a shopping list of other libraries the application needs to work. If you’re just starting out with Ruby on Rails you will see just how important and helpful this gem is.
You can use the rbenv shell
command to ensure we have the correct version of
Ruby loaded in our terminal window, it sets a shell-specific Ruby version by
setting the BENV_VERSION
environment variable in your shell. This version
overrides application-specific versions and the global version. If you’re
paranoid you can always check your Ruby version with ruby --version
.
$ rbenv shell 2.0.0-p247
$ gem install bundler
$ rbenv rehash
To configure Bundler to install gems in a location relative to your projects instead of globally, in this case the vendor folder of a Rails project, do this:
$ mkdir ~/.bundle
$ touch ~/.bundle/config
$ echo 'BUNDLE_PATH: vendor/bundle' >> ~/.bundle/config
If you use Google for finding your Gem documentation you might consider saving a bit of time when installing gems by skipping the documentation.
$ echo 'gem: --no-rdoc --no-ri' >> ~/.gemrc
Ps: Notice rbenv rehash
has been used again when installing bundler, you need
to do this whenever you install a new gem that provides binaries. So if you’ve
installed a gem and the terminal tells you it can’t find it run rbenv rehash
.
Ps 2: As you’ll see from rbenv install --list
there are loads of Ruby
versions available including JRuby just remember you will need to re-install
your gems for each version as they are not shared.
Check out the Rails website for the whole story on Rails.
$ gem install rails
$ rbenv rehash
Rails has a numberq of dependencies to install so don’t be surprised if you see loads of other gems being installed at the same time.
Most people prefer MySQL as their DB of choice.
$ gem install mysql2
To connect:
$ mysql -u root
A few things that you should know:
# 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|stop|restart|reload|force-reload|status}
$ mysql.server start
If you are using MAMP and don’t need MySQL, check out the config file details in this other post.
Ps: For those using Rails & MAMP (using sockets, which seems to be the easier option) … I’m not quite sure if it works out of the box. I was tinkering with an actual MySQL install using homebrew by the time I found this solution. If it doesn’t, try install MySQL using homebrew first. There have been a few articles on the web discussing the fact that the MySQL bundled with MAMP doesn’t have everything reuired for a full MySQL installation (which might be necessary for the mysql2 gem install).
$ rails new helloworld
$ cd helloworld
Set the local Ruby version for this project to make sure this stays constant,
even if we change the global version later on. This command will write
automatically to .ruby-version
in your project directory. This file will
automatically change the Ruby version within this folder and warn you if you
don’t have it installed.
Then run Bundler to install all the project gems into vendor/bundle, they are kept with the project locally and won’t interfere with anything else outside.
$ rbenv local 2.0.0-p247
$ bundle install
If your gems ever stop working you can just delete the vendor/bundle directory and run the command again to re-install them. It’s also worth updating your .gitignore file so you don’t commit all of those gems!
Now to test if the application is working:
$ rails server
rbenv
Command Reference