Thursday, 20 February 2014

Create Rails 4 application and use RVM


First, although I already blog few year ago about installing RVM, my blog needs to be updated with latest RVM set-up. The new command lines for the installation of RVM (tested on Ubuntu 13.10):

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
echo "source ~/.rvm/scripts/rvm" >> ~/.bashrc


Install ruby and gems 

rvm install 2.1.0
rvm use  --default 2.1.0
ruby -v


note the path of ruby and the associated gemset
.rvm/gems/ruby-2.1.0@global
This would be useful later on to know which rvm ruby "ruby-2.1.0" and rvm gemset "global" we are using so we can load them automatically every time we access the project directory.
 rvm list
rvm rubies

ree-1.8.7-2011.03 [ i686 ]
ruby-1.9.2-p320 [ i686 ]
ruby-1.9.3-p484 [ i686 ]
  • ruby-2.0.0-p353 [ i686 ]
 => ruby-2.1.0 [ i686 ] # => - current # =* - current && default # * - default


then add  rails gems and create new application called "jobsearch"
 rvm gemset list
 echo "gem: --no-ri --no-rdoc" > ~/.gemrc
 gem install rails 
 rails new jobsearch cd jobsearch
 bundle
Note: the --no-rdoc and the --no-ri will save you time especially on production server where the documentation is not really require.

Create the rvmrc file so that your directory switch automatically to the right ruby and gemset

 rvm --create --rvmrc ruby-2.1.0@global
The file .rvmrc is created and now every time that you go to the project directory of your application the right ruby and gems are loaded
$ cd ..
$ cd jobsearch/
You are using '.rvmrc', it requires trusting, it is slower and it is not compatible with other ruby managers,
you can switch to '.ruby-version' using 'rvm rvmrc to [.]ruby-version'
or ignore this warning with 'rvm rvmrc warning ignore /home/user/dev/jobsearch/.rvmrc',
'.rvmrc' will continue to be the default project file in RVM 1 and RVM 2,
to ignore the warning for all files run 'rvm rvmrc warning ignore all.rvmrcs'.
********************************************************************************

  • NOTICE *
********************************************************************************
  • RVM has encountered a new or modified .rvmrc file in the current directory, *
  • this is a shell script and therefore may contain any shell commands. *
  • *
  • Examine the contents of this file carefully to be sure the contents are *
  • safe before trusting it! *
  • Do you wish to trust '/home/user/dev/jobsearch/.rvmrc'? *
  • Choose v[iew] below to view the contents *
********************************************************************************
y[es], n[o], v[iew], c[ancel]> y



Add this line in the end of .rvmrc to display the selected gem

------------------------------
if $- == *i* # check for interactive shells
then
echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
else
echo "Using: $GEM_HOME" # don't use colors in interactive shells
fi
------------------------------

$ cd jobsearch/
You are using '.rvmrc', it requires trusting, it is slower and it is not compatible with other ruby managers,
you can switch to '.ruby-version' using 'rvm rvmrc to [.]ruby-version'
or ignore this warning with 'rvm rvmrc warning ignore /home/user/dev/jobsearch/.rvmrc',
'.rvmrc' will continue to be the default project file in RVM 1 and RVM 2,
to ignore the warning for all files run 'rvm rvmrc warning ignore all.rvmrcs'.
*********************************************************************************
* NOTICE                                                                                                           *
*********************************************************************************
* RVM has encountered a new or modified .rvmrc file in the current directory, this is a shell script and           *
* therefore may contain any shell commands.                                                                        *
*                                                                                                                  *
* Examine the contents of this file carefully to be sure the contents are safe before trusting it!                 *
* Do you wish to trust '/home/user/dev/jobsearch/.rvmrc'?                                                        *
* Choose v[iew] below to view the contents                                                                         *
*********************************************************************************
y[es], n[o], v[iew], c[ancel]> y
Using:
/home/user/.rvm/gems/ruby-2.1.0@global


You are now ready to run the basic application:

bundle exec rails s

=> Booting WEBrick=> Rails 4.0.3 application starting in development on http://0.0.0.0:3000=> Run `rails server -h` for more startup options=> Ctrl-C to shutdown server[2014-02-20 00:06:26] INFO  WEBrick 1.3.1[2014-02-20 00:06:26] INFO  ruby 2.1.0 (2013-12-25) [i686-linux][2014-02-20 00:06:26] INFO  WEBrick::HTTPServer#start: pid=17659 port=3000

The Benefit of RVM


RVM is a great tool when you want to test different rubies or if you have several applications on a single server, as you can use any combination of rubies and gemsets all on a single machine without any problem.