Wednesday, August 12, 2009

Installing support for Ruby development

Previous: Updating Ubuntu.

Ruby


To install Ruby support:
sudo apt-get install ruby-full

Geany (an IDE)


Low let's get Geany, a rudimentary IDE I find handy for Ruby development.
sudo apt-get install geany

Gems (to install other Ruby packages)


This installation procedure is a little different from the usual apt-get thing.

1. Look on Ruby Forge and see what the latest version of Ruby Gems is: http://rubyforge.org/frs/?group_id=126. Hover over the .tgz filename to see the correct URL for the wget command.

2. The Ruby community does not recommend using apt-get to install Ruby Gems because Advanced Package Tool and gems may get out of sync about versions. The recommended way is to open a Terminal window and enter the following commands (in this case the version I found in step 1 was 1.3.4). Long lines are split for readability in Blogger. Use common sense.
cd ~
wget http://rubyforge.org/frs/download.php/57643/
rubygems-1.3.4.tgz
tar xzvf rubygems-1.3.4.tgz
cd rubygems-1.3.4
sudo ruby setup.rb
3. Note which executable was installed (a message appears near the end of the installation) and define a symlink to it. In this case it was version 1.8.
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
4. If the installation worked, the following command will display version information. In this case we want to see "1.3.4". By changing directories first I help myself believe the PATH was updated. I hear it's good to believe.
cd
gem -v

MySQL server


I use MySQL for Ruby on Rails development (and other things, too). To install:
sudo apt-get install mysql-server
Of course, you should set the MySQL root password to something other than "empty" when prompted. The installer starts mysqld. To be sure it worked, run
ps ax | grep mysql
and look for /var/run/mysqld/mysqld.pid.

After installation, you can stop and start MySQL through System -> Administration -> Services on the main menubar. Alternatively, you can start and stop MySQL from a Terminal window using the commands:
sudo /etc/init.d/mysql start
and
sudo /etc/init.d/mysql stop

MySQL GUI client


To install a gui client for MySQL, choose Applications -> Add/Remove Applications from the main menubar. If prompted to update the list of available packages, do so. Under Show, choose "All available applications". Select "MySQL Administrator" for a GUI client and "MySQL Query Browser" for a GUI tool for running queries. After the installer finishes, the two applications will appear under Applications -> Programming on the main menubar.

Ruby bindings for MySQL


Open a Terminal window and enter the following commands:
cd ~
sudo apt-get install libmysql-ruby libmysqlclient-dev
The package libmysqlclient-dev is a virtual package. When you run the command above, you will see an error message that shows the name of the real package. Substitute it in the command line and run it again. When I ran it, it showed libmysqlclient15-dev, so I ran the command this way:
sudo apt-get install libmysql-ruby libmysqlclient15-dev
Run this command to build it:
sudo gem install mysql

Rails


Use Gems to install Rails. Open a Terminal window and enter the following command
cd ~
sudo gem install rails
If the installation succeeded, the following command will display version information:
rails -v
Verify the installation by generating a trivial rails app:
cd ~
rails myrailsapp -d mysql
or, for sqlite3:
cd ~
rails myrailsapp
In myrailsapp/script, run the command:
./server
If all is well, a WEBrick server will start and will listen on port 3000 for HTTP requests. Try loading the following URL in a browser. You should see a welcome page.
http://localhost:3000

Testing tools


These are tools for test-driven-, behavior-driven-, and storytest-driven-development:
  • Autotest - continuous testing (part of ZenTest)
  • Cucumber - storytest tool
  • RCov - coverage tool
  • rspec - BDD tool
  • Webrat - browser simulator (used by Cucumber)

On Debian variants (like Ubuntu), you have to install a couple of libraries before installing Webrat. They are needed by Nokogiri, the XML libary used in Webrat.
sudo gem install ZenTest
sudo gem install rcov
sudo gem install rspec rspec-rails
sudo gem install cucumber
sudo apt-get install libxslt1-dev libxml2-dev
sudo gem install webrat

Some conveniences


I'm rather slow-witted, so I find it helpful to follow a few conventions even though I'm the only person working on my system or on my little side projects. I put my software development projects under a directory called /projects. I nearly always use Geany for Ruby on Rails development. I use rspec and Cucumber with these projects. So it's convenient to have a quick way to set up a new Ruby on Rails project in the usual directory that includes the elements I usually want and that creates a Geany project file. To that end, I created a shell script in ~/bin that sets these things up for me. The script newrailsapp looked like this as of the time I posted this information:
#!/bin/bash
if [ -z $1 ]; then
echo "usage: newrailsapp appname"
exit
fi
if [ -e $1 ]; then
echo "Can't create $1 because it already exists. Quitting."
exit
fi

# Create the rails app

rails -d mysql $1

# Generate rspec support for the app

cd $1
./script/generate rspec
./script/generate cucumber

# Create a geany project file for the app

echo "Creating geany project file $1.geany"
cd ..
echo "[indentation]" > $1.geany
echo "indent_width=4" >> $1.geany
echo "indent_type=1" >> $1.geany
echo "indent_hard_tab_width=8" >> $1.geany
echo "detect_indent=false" >> $1.geany
echo "indent_mode=2" >> $1.geany
echo " " >> $1.geany
echo "[project]" >> $1.geany
echo "name=$1" >> $1.geany
echo "base_path=/home/dave/projects/$1/" >> $1.geany
echo "make_in_base_path=false" >> $1.geany
echo " " >> $1.geany
echo "[files]" >> $1.geany
echo "current_page=-1" >> $1.geany
Also created these aliases in ~/.bash_aliases:
# Start Geany (IDE)
alias geany='geany &'
# Start and stop any Rails test server
alias server='script/server &'
alias server_stop='~/bin/server_stop'

The alias geany just starts Geany in the background. Saves a couple of keystrokes.

The purpose of server is to start a Webrick server for dev-testing a Rails app. It's meant to be run from a command line when you're in the appropriate project subdirectory. The server script looks like this:
#!/bin/bash
script/server &
The purpose of server_stop is to stop a Webrick server. It looks like this (with the usual caveat about long lines)
#!/bin/bash
kill -9 `ps -o pid,cmd --no-heading -p $(pgrep ruby)
| grep script/server | awk '{print $1}'`
The assumption is that there will only be one Webrick server running at a time. It's a safe assumption because all of this is set up for a single user environment.

Next: Installing support for Java development

1 comment:

  1. Thanks for the Information, thanks for this fine Post. I will subscribe to your feed for updates.

    ReplyDelete