[RSS]

Installing your own private copy of Ruby on Ubuntu

Note: See also Dinel's blog on Installing Ruby Gems in NetBeans IDE 6.0 (Ubuntu 7.10).

Here are the excellent resources I used to install my private copy of ruby:

  • http://youarenotexpectedtounderstandthis.blogspot.com/2007/03/howto-ubuntu-edgy-eft-610-ruby-185-gem.html
  • http://www.rubywizards.com/viewtopic.php?pid=19
  • http://www.nabble.com/Gems-zlib-problem-t992896.html

I've summarized the steps here:

  • Bring up the terminal, create a temporary directory to store downloaded files.
   [~/] mkdir tmp
   [~/] cd tmp
  • Download official source distribution. This will create a file named ruby-1.8.5.tar.gz in your tmp directory.
    [~/tmp] wget ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.5.tar.gz
  • Extract all the files from downloaded archive. This will create tmp/ruby-1.8.5 subdirectory that stores extracted files.
    [~/tmp] tar xvfz ruby-1.8.5.tar.gz
  • Install GNU C++ compiler you'll need to build Ruby from source.
    [~/tmp] sudo apt-get install build-essential
  • Install zlibc.
    [~/tmp] sudo apt-get install zlibc zlib1g zlib1g-dev

Let's say we want to install our own private copy of ruby in ~/ruby-1.8.5, i.e. /home/you/ruby-1.8.5

  • Run configure utility to determine your system configuration.
   [~/tmp] cd ruby-1.8.5
   [~/tmp] ./configure --prefix=/home/you/ruby-1.8.5
  • Run make command to compile and build Ruby.
   [~/tmp/ruby-1.8.5] make
  • Install Ruby onto your system. This will move Ruby executable and utilities to ~/ruby-1.8.5/bin and standard Ruby libraries to ~/ruby-1.8.5/local/lib/ruby.
   [~/tmp/ruby-1.8.5] make install
  • Install Ruby documentation. This will compile Ruby documentation in format required for ri command.
   [~/tmp/ruby-1.8.5] make install-doc
  • Build and install zlib support using your private ruby installation.
   [~/tmp/ruby-1.8.5] cd ext/zlib
   [~/tmp/ruby-1.8.5/ext/zlib] ~/ruby-1.8.5/bin/ruby extconf.rb
   [~/tmp/ruby-1.8.5/ext/zlib] make
   [~/tmp/ruby-1.8.5/ext/zlib] make install
   [~/tmp/ruby-1.8.5/ext/zlib] cd ~/tmp
  • Download RubyGems and extract archive.
   [~/tmp] wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
   [~/tmp] tar xvfz rubygems-0.9.4.tgz
  • Install RubyGems using your private ruby installation.
   [~/tmp] cd rubygems-0.9.4
   [~/tmp/rubygems-0.9.4] ~/ruby-1.8.5/bin/ruby setup.rb

That should do it. Now you can fire up netbeans and point to your new Ruby installation in Tools/Options/Miscellaneous/Ruby Installation

Use the Ruby Gems manager (Tools/Ruby Gems) to install gems for your private ruby:

  • If you want fast-debugger support install ruby-debug-ide
  • If you want rspec support install rspec

Adding MySQL Support

  • Install MySQL and the client development libraries from the repository. (NoteL: The package libmysql-ruby may not be required, I had it as a hangover from my initial install which used the packaged ruby and rails, feel free to try the process without it first)
    [~/tmp] sudo apt-get install mysql-server libmysql-ruby libmysqlclient15-dev
  • First download the MySQL ruby source and extract in your temporary directory.
   
   [~/] cd ~/tmp
   [~/tmp] wget http://tmtm.org/downloads/mysql/ruby/mysql-ruby-2.7.4.tar.gz
   [~/tmp] tar xvfc mysql-ruby-2.7.4.tar.gz
   [~/tmp] cd mysql-ruby-2.7.4/
  • Now configure the source ready to build using your personal ruby install.
 
   [~/tmp/mysql-ruby-2.7.4] ~/ruby-1.8.5/bin/ruby extconf.rb 
  • Now make the package and then install it
   [~/tmp/mysql-ruby-2.7.4] make
   [~/tmp/mysql-ruby-2.7.4] make install
  • Done now MySQL support will work a treat.

Adding Rails support

To install rails just use the Gems Manager in the IDE and add rails gems and optionally the mongrel gem. Getting MySQL support up and running is slightly harder (but no harder than the previous steps you have already done).

  • To get script/console to work you need to install readline support into your custom ruby install. To do this you will need to install the development package for GNU readline.
    [~/tmp] sudo apt-get install libreadline5-dev
  • Now compile the readline support in ruby
   
   [~/] cd ~/tmp/ruby-1.8.5/ext/readline
   [~/tmp/ruby-1.8.5/ext/readline] ~/ruby-1.8.5/bin/ruby extconf.rb
   [~/tmp/ruby-1.8.5/ext/readline] make
   [~/tmp/ruby-1.8.5/ext/readline] make install

Creating a private Gem repository on Ubuntu

Note: See also Dinel's blog on Installing Ruby Gems in NetBeans IDE 6.0 (Ubuntu 7.10).

(This is a summary of http://wiki.rubyonrails.org/rails/pages/HowToUseMultipleGemRepositories, with minor adaptations for Netbeans.)

This assumes that you already have the stock Ubuntu ruby and rubygems packages installed. If you do, your Gem repository is at /var/lib/gems/1.8. Make a note of this because it will be important later.

First you need to decide where you'll put your private repository. I'll use /home/david/.rubygems. Whenever you see this path, substitute your own for it.

tasmania:~$ mkdir .rubygems

Next download rubygems. I believe it's best to download the same version that you have installed on your system (currently 0.90.0 on Feisty) to prevent compatibility problems.

You can download the distribution here: http://rubyforge.org/frs/?group_id=126

After you download the file, unpack them somewhere. I'll assume you downloaded it to /tmp.

tasmania:/tmp$ tar xvf rubygems-0.90.0.tgz
tasmania:/tmp$ cd rubygems-0.90.0
tasmania:/tmp$ GEM_HOME=/home/david/.rubygems ruby setup.rb all --prefix=/tmp/gems

I used GEM_HOME to tell setup.rb where it should initialize the repository. If you now look inside the repository location, you should see something like this:

tasmania:~/.rubygems$ ls
cache  doc  gems  specifications

Congratulations! You now have your own private, working, Gem repository.

(I'll let you in on a secret. I won't be using this repository. It's there only to keep Netbeans happy so it will stop bugging me about not finding my rails installation. Of course, this means I won't be using Netbeans to manage my gems.)

Now we have a problem. Since Netbeans looks for Gem executables (e.g., the rails executable) inside $GEM_HOME/bin, you need to symlink the entire /var/lib/gems/1.8/bin dir to $GEM_HOME/bin like so:

tasmania:~/.rubygems$ ln -s /var/lib/gems/1.8/bin .

Or, if you plan to let Netbeans manage your private repository, you need to create a bin/ directory and manualy symlink all the global Gem executables:

tasmania:~/.rubygems$ mkdir bin 
tasmania:~/.rubygems$ cd bin
tasmania:~/.rubygems/bin$ for f in $(find /var/lib/gems/1.8/bin -type f); do ln -s $f .; done

Now we just have to let Netbeans know where to find the writable repository, and where to find all other gems. We do this using (again) GEM_HOME and a new environment variable: GEM_PATH.

tasmania:/opt/nbrubyide/bin$ export GEM_HOME=/home/david/.rubygems
tasmania:/opt/nbrubyide/bin$ export GEM_PATH=/var/lib/gems/1.8:/home/david/.rubygems
tasmania:/opt/nbrubyide/bin$ ./nbrubyide &

Presto! Put those exports in a wrapper script, and start hacking. You shouldn't put this in your .bashrc (or your shell's equivalent) as that will confuse you global gem command.