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.

Friday, 22 June 2012

Railsbuntu

Been a long time since I wrote a post....

I am now sending myself into a new adventure : Writing a technical book RailsBuntu .

This will be done base on an agile context (thanks to leanpub.com), where the readers can interact with me, and maybe some will join me as it s going along.

But first let me tell you that the book is not yet available and published, I have a lot of work to do to put my notes all together in order to make it readable, and it is taking more time than I thought.

I may release chapter by chapter and depending of the feedback than I got I may divert a little from the line that I have chosen.

Please feel free to comment and let me know if there is a point that you would like to see.

The target of the book is Rails on Rails on a Ubuntu Server. So I will first look at the admin side of the server to get the web platform ready and then add rails examples needed for particular aspect such emailing, ajax example, html layout etc...

A section on troubleshooting may be useful, where I can put problems that I(or you) have encounter during my coding experience.

Wish me luck and be patient with me (I also have a full time job)

Friday, 18 February 2011

New rails application with mysql

Following previous post I will create a new rails application with mysql as database server. (as before under ubuntu10.10)
First mysql dependencies:
user@Lap:~$ sudo apt-get install mysql-server libmysqlclient-dev

user@Lap:~$ gem install mysql2 --no-ri --no-rdoc
Building native extensions. This could take a while...
Successfully installed mysql2-0.2.6
1 gem installed

user@Lap:~$ rails new app -d mysql

this will create the directory app with all the necessary files


user@Lap:~$ cd app

create the database for the app (info for database in config/database.yml)
user@Lap:~$ rake db:create

user@Lap:~$ rails generate scaffold command execute:string
invoke active_record
create db/migrate/20110219000712_create_commands.rb
create app/models/command.rb
.........

user@Lap:~$ rake db:migrate
== CreateCommands: migrating =================================================
-- create_table(:commands)
-> 0.1622s
== CreateCommands: migrated (0.1624s) ========================================

user@Lap:~$ rm public/index.html

user@Lap:~$ nano config/routes.rb
# just remember to delete public/index.html.
root :to => "commands#index"
#note this is the new rails3 way to do it


user@Lap:~$ rails server


Reference: Video similar command examples with rails2


Install RVM and Ruby enterprise on ubuntu10.10

an quick way to install RVM the Ruby Version Manager:

user@Lap:~$ sudo aptitude install build-essential git-core curl

user@Lap:~$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
hoops.... this is not valid anymore, now:
user@Lap:~$ 
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)



From there you can install any version of ruby and different gemset.


Before using rvm command, you need to add this line to your .bashrc (at the end of the file)
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Not any more! this is now included in the install(15/09/2011)

Then open new terminal type "rvm"


I like to use ruby enterprise (ree in short for rvm)
before to install ree let's install dependencies with the following command
user@Lap:~$ sudo aptitude install build-essential bison 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

Install ruby enterprise
user@Lap:~$ rvm install ree
Installing Ruby Enterprise Edition from source to: /home/user/.rvm/rubies/ree-1.8.7-2011.01
ree-1.8.7-2011.01 - #fetching (ruby-enterprise-1.8.7-2011.01)
....

use the install ree
user@Lap:~$ rvm use ree

then install the gem you want the usual way
user@Lap:~$ gem install name_of_gem
e.g
user@Lap:~$ gem install rails

Note: by default the gems are install in the global gemset but you are free to create different gemset with "rvm gemset create gemset_name"

Reference: RVM site