When I started using Rails, I heard a lot about all the different components that you should have to enhance rails like RVM, RSpec, Guard, Git and many others. Unfortunately, I could never find a tutorial that put it all together from start to finish in setting up a project in Rails. Since then, I’ve worked on several ruby projects and this is the set up I use when creating a new project from scratch on a machine that previously did not have Rails.
1. Installing Ruby Version Manager (RVM)
Ruby Version Manager is a great tool for isolating your development environment to each individual project. This allows you to have specific ruby versions with specific gemsets isolated from the rest of your development environment, virtually eliminating any potential conflicts. Although not absolutely required to use Rails or Ruby, RVM will greatly reduce headaches with managing ruby. To learn more about RVM, visit the RVM homepage.
Luckily, RVM provides an install script that we can download and execute with a single command. This command will download the latest code from the github repository, so you need to have git and curl installed on the system. If you are using a recent linux distro like Ubuntu 11.10, then you might already have git and curl installed. If not, execute the following command:
$ sudo apt-get install git curl
Start the RVM installation by running this command:
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Now that the installation completed, we need to add the loader for RVM to our .bash_profile. Open the file in your favorite editor:
$ gedit .bash_profile
Next, past the following command into the text file:
[[ -s "/home/artem/.rvm/scripts/rvm" ]] && source "/home/artem/.rvm/scripts/rvm"
For the .bash_profile to take effect, we need to restart the bash shell. Once the bash shell restarts, let’s verify that RVM is properly installed by running the following command:
$ type rvm | head -1 rvm is a function
Next, let’s check what dependencies we need to install on our platform by running:
$ rvm requirements
This returns a number of requirements you will need for different ruby compilers. We will use MRI (official) ruby interpreter, so copy and paste the command under that and run it:
$ sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion
2. Installing Ruby
Now that we got RVM all ready to go, let’s jump right into installing Ruby. First, let’s see what version of ruby are available to us through RVM:
$ rvm list known
We can see there are quite a few different ruby installations available:
# MRI Rubies [ruby-]1.8.6[-p420] [ruby-]1.8.6-head [ruby-]1.8.7[-p352] [ruby-]1.8.7-head [ruby-]1.9.1-p378 [ruby-]1.9.1[-p431] [ruby-]1.9.1-head [ruby-]1.9.2-p180 [ruby-]1.9.2[-p290] [ruby-]1.9.2-head [ruby-]1.9.3-preview1 [ruby-]1.9.3-rc1 [ruby-]1.9.3[-p0] [ruby-]1.9.3-head ruby-head # GoRuby goruby # JRuby jruby-1.2.0 ... # Rubinius rbx-1.0.1 ... # Ruby Enterprise Edition ree-1.8.6 ... # Kiji kiji # MagLev maglev[-head] .. # Mac OS X Snow Leopard Only macruby[-0.10] ... # IronRuby -- Not implemented yet. ironruby-0.9.3 ...
Let’s start by installing Ruby 1.8.7:
$ rvm install 1.8.7
This will download and install Ruby 1.8.7. After installation is complete let’s set RVM to use this Ruby installation:
$ rvm use 1.8.7@defaultgems --create Using /home/artem/.rvm/gems/ruby-1.8.7-p352 with gemset defaultgems
You can also set this to be the default version, overriding the system installation of Ruby, by running the following command:
$ rvm use 1.8.7@defaultgems --default Using /home/artem/.rvm/gems/ruby-1.8.7-p352 with gemset defaultgems
You can always switch back to the system ruby by running:
$ rvm use system
3. Installing RubyGems and Rails
Ruby is fully functional now, but we are not done yet. We want to get Rails set up on this installation, so let’s start by installing RubyGems (ruby package manager). Start by downloading the setup file from RubyForge, then extract the archive and execute the setup.rb file:
$ sudo ruby setup.rb
Once the installation finishes, simply execute the following command to install Rails:
$ gem install rails
4. Starting the first project and configuring the basics
We are finally ready to get to the good part of actually setting up a new project. Start by navigating to the directory where you want to store rails project and create a new rails project:
$ rails new Project -T $ cd Project
Note the ‘-T’ option; it tells rails not to use Test::Unit. Instead, we will use RSpec that we will install shortly.
Remember we installed Ruby 1.8.7? Let’s say for this project we want to use version 1.9.2 instead (because that’s what our server uses), but we only want 1.9.2 to execute for this project AND we want it to that automatically. Additionally, we want to isolate the gems for this project so that we don’t have any hard to trace conflicts between unused gems. It’s a tough order, but that’s exactly what RVM is made to do. We can customize the RVM behavior by placing .rvmrc file inside the root directory of our project:
$ gedit .rvmrc
Now we need to put one line to load the ruby we want with a gemset that’s specific to the project. Recall that we wanted to use Ruby 1.9.2, so paste the following:
Save the file and exit the directory and enter it again:
$ cd .. $ cd Project
RVM will ask you if you trust this .rvmrc file (and you should! you made it). Click yes, and it should prompt you with an error that we didn’t install this version of Ruby yet. Copy and paste the instructions given by the error:
$ rvm install 1.9.2 $ rvm gemset create 'project_name'
At this point, we need to reinstall rails for this specific gemset, so run:
$ gem install rails
5. Installing RSpec and Guard
Now that we got our project set up, we should add a testing environment since we removed Unit::Test. Let’s start by opening the Gemfile and adding the following:
group :test, :development do gem 'rspec-rails' gem 'capybara' gem 'guard-rspec' end
Next run the bundle command from the bash shell, and initialize RSpec and Guard:
$ bundle $ rails g rspec:install mkdir spec/support spec/models spec/routing guard init rspec
And, now we are all set to start programming the first application. Fortunately, starting a new project will take a lot less time, now that we got everything installed, so don’t be afraid to experiment
Let me know how this works out for you. If you have any question, be sure to leave comments below and share this if you liked it!
- No public Twitter messages.