Development
Creating Your First Ruby on Rails Application From Scratch on Mac OS X
Setting up a Ruby on Rails development environment and building a first application from scratch on Mac OS X.
- ruby
- rails
- macos
(view full size, opens in a new tab)
Today I will be showing you how to setup a Ruby on Rails development environment on Mac OS X 10.9 Mavericks. While this guide may work for other versions of OS X, it is not guarenteed to work.
Getting the Prerequisites Installed
To setup Ruby on Rails, we need to make sure we have a few things, such as the Ruby language and RubyGems packaging system.
First thing we need to do is install Homebrew which will allow us to compile software quickly and easily from source code. To install Homebrew, run the following command:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
This will install Homebrew. Next, we have to be sure that Homebrew is ready to run, so we have to run the following command before we attempt to use Homebrew.
brew doctor
Next, we need to update Ruby. Mavericks ships with a system version of Ruby, Ruby 2.0.0p247. This version of Ruby sadly has known security vulnerabilities and as such it is in our best interest to update to the newest version of Ruby using RVM, or Ruby Version Manager. To install RVM with Ruby, run the following command:
\curl -sSL https://get.rvm.io | bash -s stable --ruby
Follow the directions to set it up. You may need to run a command or restart terminal to get RVM to work. To ensure it is installed correctly, run this command:
rvm --version && type rvm | head -1
which should output something like:
rvm 1.25.28 (stable) by Wayne E. Seguin <[email protected]>, Michal Papis <[email protected]> [https://rvm.io/]
rvm is a function
Our installation should have come with the latest version of Ruby, at this time that is 2.1.2, but to be sure let's attempt to install it and set the version of ruby we would like to use to be 2.1.2.
rvm install 2.1.2
Already installed ruby-2.1.2.
To reinstall use:
rvm reinstall ruby-2.1.2
rvm use 2.1.2
Using /Users/USERNAME/.rvm/gems/ruby-2.1.2
Lastly, we just need to load rvm automatically when we start the terminal. To do this, we edit ~/.profile (potentially ~/.bash_profile) by using sudo nano ~/.profile and add:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM i$
With Homebrew, RVM and Ruby installed and using the latest versions, let's get Ruby on Rails installed.
Installing Ruby on Rails
Installing Ruby on Rails is simple thanks to the concept of Gems in Ruby. A gem in ruby is a package that makes distributing Ruby programs or libraries simple and easy. Gems are handled by the package manager RubyGems.
To install the Ruby on Rails gem, we run the following command:
gem install rails
This process may take a little while as it needs to get a number of dependecies and install them depending on your system setup. Just sit back and relax while it does this. For me, it took the longest to get past Installing ri documentation for rails-4.1.5. This alone can take upwards of 5 minutes with a core i7 processor and SSD hard drive.
When the process has finished installing, you should see a line like:
Done installing documentation for actionmailer, actionpack, actionview, activemodel, activerecord, activesupport, arel, builder, erubis, hike, i18n, mail, mime-types, minitest, multi_json, polyglot, rack, rack-test, rails, railties, sprockets, sprockets-rails, thor, thread_safe, tilt, treetop, tzinfo after 312 seconds
27 gems installed
To verify that Ruby on Rails has been installed, in the terminal we can use the -v flag to check the version of Ruby on Rails like so. The output is directly below the command.
rails -v
Rails 4.1.5
Getting a Database for Ruby on Rails
By default, Ruby on Rails uses the SQLite database. While this can work for many people, it's probably not what we will want to use it because it's simply a file stored on disk and instead we may want to use MySQL or PostgreSQL. Here, I will be showing you how to setup PostgreSQL.
Installing PostgreSQL
To have Homebrew install PostgreSQL, we can run the following command:
brew install postgresql
If we would like to have PostgreSQL loaded at startup and then start it up for our current installation, we run the following command
# Start PostgreSQL at Startup
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
# Start PostgreSQL now
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
By default the postgresql user is your current OS X username with no password. For example, my OS X user is named KevinKirsche so I can login to postgresql with that username.
Creating Our First Ruby on Rails Application
Now that we have:
- Installed Homebrew
- Installed RVM
- Installed / Setup the latest version of Ruby
- Installed the Ruby on Rails Gem
- Installed the database system of your choice
We are ready to make our first Ruby on Rails application.
The command to create a new Ruby on Rails application is rails new ApplicationName. If you do not want to use the SQLite database, you can use the -d flag to specify the database system you would like to use. In this example we will use PostgreSQL.
# Note you will need to change config/database.yml's username to be
# the same as your OSX user account. (for example, mine is 'KevinKirsche')
rails new ApplicationName -d postgresql
This will take some time but should be under 2 minutes. Near the end you will see Your bundle is complete! which means it was installed correctly. To see the structure of our application, we will use the cd command, short for change directory, to enter our application. Assuming our Application's name was ApplicationName as shown in the code above, we would run:
cd ApplicationName
If we didn't use the SQLite database, we now need to setup the configuration file for our database. This is found in the config/database.yml file. This can be done in a text editor or from the command line using vi or nano.
At the time of writing this, you simply need to change line 84 for PostgreSQL to work.
Next, we want to create our application's database. This can be done with the rake command as seen below.
rake db:create
If you get the error Access denied for user 'root'@'localhost' (using password: NO) then your config/database.yml was not configured properly to access your database.
Lastly, we start up the built in rails server, known as WEBrick so that we can see our page. It's luckily very simple.
rails server
Everything is ready now and we can go to http://localhost:3000/ and we should see the following page if everything is working correctly.