Monday, May 28, 2012

Create Ruby on Rails development environment for Windows XP: DOS prompt and pik

This post details a Windows setup for the book Rails 3 in Action.  Having done both an Ubuntu 12.04 and a Windows XP RoR setup, I strongly recommend the former: much quicker and easier.


I was having some issues with my Cygwin/RVM install so I decided to try using the DOS command prompt and pik instead.



http://stackoverflow.com/questions/9189628/install-ruby-on-rails-on-windows
I think the answerer to this post incorrectly uses bundler to try and install gems (at least for me it gave an error, but using pik gem install <gemname> worked).


Note that for the console I'm using the Windows DOS command line instead of cygwin

Installed Ruby via the Ruby installer

    Added Ruby to path as part of the installer option

Installed pik

D:\> gem install pik
D:\> pik_install <directory_in_PATH>

Ran pik

D:\> pik
D:\> pik list ## confirm proper Ruby version



Installed DevKit

Ran installer to %DEVKIT_HOME%

%DEVKIT_HOME%\> ruby dk.rb init
%DEVKIT_HOME%\> ruby dk.rb install

Test the DevKit installation

%DEVKIT_HOME%\> pik gem install rdiscount --platform=ruby
%DEVKIT_HOME%\> ruby -rubygems -e "require 'rdiscount'; puts Discount.new('**Hello there, Eric!**').to_html"
Should output 
<p><strong>Hello there, Eric!</strong></p>

Installed gems using pik

I didn't find a lot of pik usage information, but I decided to install gems with pik.  Using 'gem list' and 'pik gem list' in different consoles leads me to believe that the installation methods acheive the same result.  Pik dosen't appear to have native gemset-like commands, but see this post for a hack.
D:\> pik gem install bundler --pre
D:\> pik gem install rake
D:\> pik gem install activesupport
D:\> pik gem install mysql
D:\> pik gem install libv8
D:\> pik gem install rails
D:\> pik gem install execjs
D:\> pik gem install jquery-rails


Installed locally downloaded gems

D:\> pik gem install linecache19-0.5.13.gem


This one required the source code for my Ruby version
D:\> set RVM_SRC=<path_to_source>
D:\> pik gem install ruby-debug-base19-0.11.26.gem -- --with-ruby-include=%RVM_SRC%




Errors

Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read
server certificate B: certificate verify failed (https://rubygems.org/gems/<gem_name>)

I got this above error when running bundle install.  See this post for several fixes.  This one worked for me, but I had to create the destination directory before running the script.

Sunday, May 27, 2012

Working on an existing Rails application in Ubuntu 12.04

My previous post described environment setup.  This post will focus on getting an existing application up and running.


Getting the application

I'm using Aptana and the existing project is on a bitbucket Mercurial repo.  Using the IDE I checked out the application.


The following commands are run from inside the project directory structure.

Bundler

Use bundler to get the appropriate gems for the project (see the file Gemfile).  I initially got errors due to missing packages on the Ubuntu side (postgresql-server-dev-all was not installed).


$ bundle


Database setup

Create databases

Looking at database.yml I see which databases I need to create.  Mine are postgresql (see the adapter fields) and two different databases.  Using psql's CREATE DATABASE <database_name> command I set both the test and  development databases up.


Create role

I needed to create a role for my Linux user using CREATE ROLE <role_name>


Populate databases

I needed to add execjs and therubyracer to my Gemfile.  I also needed to add `, :require => false` after my shoulda and shoulda-context gems in Gemfile per https://gist.github.com/1549790.  Per the link it seems that the load worked fine without the require => false.


$ rake db:schema:load


Run the application

$ rails server


http://localhost:3000 will display the RoR welcome screen.  You'll have to modify the URL based on your application.


Dude, where's my gemset?

If the .rvmc file for the project on which your working specifies a gemset that you don't have and/or that you didn't use to install the proper gems you can copy the one you did have setup to match the one specified in the file.  This is especially helpful if you want to use the Terminal in Aptana.
I needed to manually rerun the ruby debug installation because that failed during the copy.

$ rvm gemset list
$ rvm gemset copy 1.9.3-p194@<existing_gemset> 1.9.3-p194@<.rvms_gemset>
$ export RVM_SRC=$HOME/.rvm/src/ruby-1.9.3-p194
$ gem install ruby-debug-base19-0.11.26.gem -- --with-ruby-include=$RVM_SRC

Install Ruby on Rails development tools on Ubuntu 12.04

I ran into a number of issues installing this entire stack and then running a 1.9.3 / 3.2.0 application.  I won't detail the errors here.  The order here is somewhat important as the postgresql-server-dev-all was required during bundling.
Please see my next post for information regarding setup for a specific application.

Install postgreSQL

https://help.ubuntu.com/community/PostgreSQL

$ sudo apt-get install postgresql
$ sudo apt-get install postgresql-server-dev-all

Looks like database was initialized and started for me and user postgres was created.  I used ps -ef | grep postgres to figure out where the relavent directories used by my installation where.  The installation created the install in /usr/lib/postgresql/9.1 and the data directory as /var/lib/postgresql/9.1/main.  Configuration files in /etc/postgresql/9.1/main.


User postgres didn't have the postgresql bin directory on the path so I had to create a new .bashrc file for postgres so I don't have to reinitialize variables every time I open a console.  
export PATH=/usr/lib/postgresql/9.1/bin:$PATH
export PGDATA=/var/lib/postgresql/9.1/main

I modified pg_hba.conf to use the trust method so I wouldn't have to supply a password to the database.  This was done since the database.yml file that is part of the project assumes no password needed for db access.

Install adminpack to assist pgAdminIII

$ sudo apt-get install postgresql-contrib-9.1
$ sudo -u postgres psql < /usr/share/postgresql/9.1/extension/adminpack--1.0.sql
$ psql
postgres=# CREATE EXTENSION adminpack;


Install pgAdminIII

$ sudo apt-get install pgadmin3

Install Mercurial

$ sudo apt-get install mercurial

Install Aptana IDE

http://www.aptana.com/products/radrails


Install the Eclipse/Aptana MercurialEclipse plugin

Here's the update site:
http://cbes.javaforge.com/update

Install RVM

$ sudo apt-get update
$ sudo bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
$ source ~/.rvm/scripts/rvm
$ rvm get head && rvm-smile
$ sudo apt-get install libtool
$ rvm pkg install libyaml
$ rvm install ruby-1.9.3-p194

$ rvm --default use 1.9.3

Install Rails

$ gem install rails -v 3.2.0
$ gem install execjs
$ gem install therubyracer

Install bundler


http://stackoverflow.com/questions/6438116/rails-with-ruby-debugger-throw-symbol-not-found-ruby-current-thread-loaderro



$ rvm get stable
$ rvm use ruby-1.9.3-p194@<gemset_name> --create
$ wget http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
$ wget http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
$ gem install linecache19-0.5.13.gem
$ export RVM_SRC=$HOME/.rvm/src/ruby-1.9.3-p194
$ gem install ruby-debug-base19-0.11.26.gem -- --with-ruby-include=$RVM_SRC
$ gem install bundler --pre


Dude, where's my gemset?

If the .rvmc file for the project on which your working specifies a gemset that you don't have and/or that you didn't use to install the proper gems you can copy the one you did have setup to match the one specified in the file.  This is especially helpful if you want to use the Terminal in Aptana.
I needed to manually rerun the ruby debug installation because that failed during the copy.

$ rvm gemset list
$ rvm gemset copy 1.9.3-p194@<existing_gemset> 1.9.3-p194@<.rvms_gemset>
$ export RVM_SRC=$HOME/.rvm/src/ruby-1.9.3-p194
$ gem install ruby-debug-base19-0.11.26.gem -- --with-ruby-include=$RVM_SRC