How to run Ruby on Rails on a Mac

I’m running Mac OSX 10.6.4 and Ruby on Rails is already installed natively on the machine. I believe most if not all versions of the Mac OS include Rails. To locate it all you have to do is launch the Terminal which can be found in Applications > Utilities > Terminal using a finder window.

Just launch Terminal and type in rails to confirm that the app exists and get a path printed to where it is. For me the path is /usr/bin/rails and a whole list of Options is also printed out. Now, you don’t actually have to launch rails by navigating that path, you can use it straight from the Terminal with the following commands.

  • pwd (print working directory)
  • cd (change directory)
  • ls (list) ls -F (shows type)
  • mkdir (make directory, folder)
  • rm (remove)
  • rmdir “name of directory” (removes dir)
  • which “command”
  • man “command” (print out manual page)
  • cat “file” (prints out a file)
  • more “file” (stops it page by page)
  • rm -rf “name” (remove directory)
  • Ctrl+c breaks out

I started out by typing pwd to print the directory I was in (which turned out to be “Home$” now in your case this might be your name since that is the default that Mac OSX usually names it when you setup your computer. My next command was mkdir Rails which created a new folder called /Rails in my Home directory. I then typed cd Rails to get into the new directory and then typed rails test (test is the name of the new folder that rails will create in the folder Rails).

At this point a whole bunch of lines will print as rails installs all of the components for a new site in the /test folder. That’s exactly what is shown in the image just below.

Next you can use the change directory command “cd” to get into the test folder you just created with the rails function. Type cd test and then confirm your location by typing pwd. Next you can tell ruby to create a scaffold which actually creates strings and CRUD (create read update and delete) functions. Here is an example: ruby script/generate scaffold test name:string address:text email:string price:decimal

You just created four variables name, address, email and price! Next setup the database by typing rake db:migrate and then launch the local test server by typing ruby script/server. At this point you can launch an internet browser such as FireFox or Safari and type http://localhost:3000/test to see the basic functions and site you created. To edit any of this contents HTML just go to the Rails/test folder and click into public to see html, javascripts and stylesheets.

That’s it, just a basic intro to Ruby here but hopefully it helps anyone who is on a Mac get started or remember some of the basic commands. I recommend checking out the book Head First Rails by David Griffiths for more information.

One last thing worth mentioning, if you launch the server by typing ruby script/server and then try to generate your scaffold or setup the database it won’t work. You have to stop the server first by pressing control+c to break out. You can always start the server again later with the ruby script/server command.

One comment

  1. Nice tutorial. You might also want to check what version of rails is installed using the “gem” command.

    To list the installed gems
    $ gem list
    actionmailer (2.3.5, 2.2.2, 2.1.2, 2.0.2, 1.3.6)
    actionpack (2.3.5, 2.2.2, 2.1.2, 2.0.2, 1.13.6)
    actionwebservice (1.2.6)
    activerecord (2.3.5, 2.2.2, 2.1.2, 2.0.2, 1.15.6)
    activeresource (2.3.5, 2.2.2, 2.1.2, 2.0.2)
    activesupport (2.3.5, 2.2.2, 2.1.2, 2.0.2, 1.4.4)

    rails (2.3.5, 2.2.2, 2.1.2, 2.0.2, 1.2.6)

    To see if a newer version of rails is available

    $ gem outdated

    rails (2.3.5 < 3.0.3)

    To update rails to the latest version

    $ sudo gem update rails
    Password: (type in your login password )
    Updating installed gems
    Updating rails
    ….

    If you mess up and want to undo your last gem update

    $sudo gem uninstall rails -v (rails version to uninstall )

    The sudo command lets you run programs as the superuser. (Use sparingly with care ).

    If you are running mysql and would like to have rails use it instead of the default SQLite database that's set up for you when you run the rails command

    $ rails -d mysql test

    Create the scaffold as mentioned but before you migrate the database run

    $rake db:create

    It will create a new database in mysql

    You may have to edit the configuration file for access to the database. It's located at
    test/config/database.yml

    Hope this helps.