I have been into Rails recently and found out that configuring Rails Environment is easy in Mac. So I will be sharing the steps I followed to setup the environment and running my first rails app.
Installing a C compiler: Mac OSX’s installation Disc comes with a set of developer tools which is not installed by default. So go ahead and install XCODE from the Installation DVD. It will install all necessary tools to compile and install packages from source. And we will need compiling source to install versions of ruby using RVM(Ruby version manager).
Installing RVM: Ruby Version Manager is a very handy tool while working with multiple version of Rubies. Snow Leopard comes with Ruby 1.8.7. And using RVM you can install Ruby 1.9.2 or the current stable version 1.9.3. And switching between version using RVM is very easy. So let’s jump into installing RVM. Rvm’s installation is very easy. The rvm website provides a one liner installation script to run in your terminal.
1
|
|
Then to load the rvm you have to run the following command:
1
|
|
But you are not going to load the rvm every time you start terminal. So its better to add the following file in your ~/.bashrc or ~/.bash_profile file and ~/.zshrc if you are using zsh as your default shell.
1 2 |
|
After loading the rvm script file to check if rvm is properly installed in your system run the following command in terminal
1
|
|
If you it says “rvm is a shell function” or “rvm is a function” then it’s fine and you are good to go.
Now go ahead and install an updated ruby version like 1.9.2 or 1.9.3 using the command
1 2 3 |
|
It will take some time and that specific ruby version will be installed automatically. Now to check which version of ruby is default in terminal you can try the following command
1
|
|
It’s supposed to say 1.8.7 if you havent upgraded the default Ruby version. Now to use the newly installed Ruby you have to tell rvm to do so using the command
1 2 3 4 5 |
|
now if you run ruby -v you will see your desired ruby version. Now let install rails gem using gem install command.
1
|
|
It will take some time and install latest rails system into your system. Now let’s create a new rails project. Move to your desired directory and run the following command
1
|
|
It will create a new rails application named blog in current directory. Now move into the new blog directory. and then run following command for bundling the gems :
1 2 |
|
It will take some time and install all gems required to run the application. Now start the app using the follwoing command
1
|
|
if you followed the previous steps properly you should see something like the following
1 2 3 4 5 6 7 |
|
It will start WEBrick (a simple web server rails ships with itself ). And you can open your browser and point it to http://0.0.0.0:3000 and you should see the following screen
To test a full featured rails 3 app you can try this invoice app from github. This app is quite complete and even have pdf invoice generation. To install and run it try the following command in terminal, you will have to have GIT installed. If you dont have git installed on your machine follow instructions from this page http://help.github.com/mac-set-up-git/ to install git.
1 2 3 4 5 6 7 |
|
and then check the app in your browser in the following url. http://0.0.0.0:3000 you should see the app running. :) here is a screenshot of the app after you add register and add some data
Like me if you want to get fancy url’s for you rails apps through virtual hosts then follow the steps in this tutorials .
Rails by default uses sqlite as default database engine if you want to use mysql as default database then using the following command instead
1
|
|
Then configure your database access info in the file located at config/database.yml and you are good to go. Though you have to have mysql2 gem installed in your system. Add
1
|
|
in Gemfile in the root of your app directory and then run bundle install again
1
|
|
If you have MAMP installed and want to know how you can reuse mamp’s mysql installation with rail’s apps then follow this tutorial I wrote earlier. :)
If you are using ubuntu you may try this tutorial http://blog.sudobits.com/2011/10/27/how-to-install-ruby-on-rails-in-ubuntu-11-10/
or this http://www.web2linux.com/05/installing-rails-3-on-ubuntu-10-04-lucid-lynx/