Installing ruby 1.9.2p0 and Rails 3.0.3 and getting it to work with MySQL on Windows

Installing ruby 1.9.2 on Windows took several false starts before I got on the right track.

My development machine is a dual-boot Windows and Linux monster with pre-existing versions of Ruby, Rubygems, Rails, and MySQL. On the Windows side, I have a working version using ruby 1.86, rubygems 1.3.5, rails 2.3.8, and MySQL 5.1. The oldest version of ruby was sitting in C:\ruby\bin, with newer versions in subdirectories like C:\ruby\1.9.2-rc2. I get some warning messages because the ruby version is older than the rubygems and rails, but the bottom line is the programs work. So, writing over a working version of ruby or MySQL was not an option.

The rvm gem is not available on Windows, so to switch ruby versions I run a batch script that prepends the desired ruby bin to the path. I tried to install ruby version 1.9.2-p0 in the same way that I had 1.8.7, but got flaky errors. For whatever reason, the 1.9.2 ruby version had the 1.8 gems on the path. This meant that I could install gems on 1.9.2, and gem list would report the new gems, but programs used gems from 1.8. This was not good. I finally installed ruby 1.9.2 in a directory that was not a subdirectory of C:\ruby, made sure the path had only one ruby entry, etc. Finally I chose to regedit and revise any references to ruby 1.8 executables to ruby 1.9.2. Now ruby 1.9.2 programs were using the gems I had installed. But programs using MySQL still didn’t run.

One error I frequently (but not always) saw was segmentation fault. Luis Lavena says that is always a MySQL dll issue. He was right. Luis Lavena is the expert on ruby and Windows. He said that you need to run MySQL 5.0, as the ruby bindings use that version of libmysql.dll. Plus, the documentation on the web references the mysql gem but not the mysql2 gem. Luis Lavena is correct, but incomplete.

First, the mysql2 gem now works on Windows with ruby 1.9.2, and the rails s command expects the mysql2 gem if you specified -d mysql when creating your rails project. The solution was to have the libmysql.dll from MySQL 5.0.89 as the first/only libmysql.dll in the path. But you don’t need to install an old version of MySQL to do this. The best solution is on http://www.fuyun.org/2010/01/ruby-mysql-adapter-on-windows/. Download mysql-noinstall-5.0.89-win32.zip, which can currently be found at http://dev.mysql.com/downloads/mysql/5.4.html, and extract only libmysql.dll from there, using your favorite unzip program (mine is 7-Zip). Then, place libmysql.dll in your path. I have it in my ruby bin; your path may differ.

To complete the solution, I’ve taken both my ruby bin and MySQL Server 5.1\bin off my default path and added two batch files to add one or the other back in. There is a ruby gem patheditor that you may want to try out, but of course it requires that ruby be in your path.

Addendum: April 6, 2011 With the latest versions of Rails (3.0.5) and the mysql2 gem (0.2.6 and 0.2.7), you need to use the MySQL 5.1 version of libmysql.dll instead of the MySQL 5.0.89 version. Either have the MySQL 5.1 bin in your path or a copy of the MySQL 5.1 version of libmysql.dll somewhere in your path.

Adding Rails/Passenger to Debian Using Ruby Gems

I needed to serve both static and Ruby on Rails (RoR) pages on my rubyat.com site running Debian gnu/Linux stable (lenny). The apt-get/Debian way is unsatisfactory, as, among other issues, Debian does not yet have Passenger in its stable (lenny) branch. After doing some research, and some trial and error, this is what worked for me.

1. Uninstall any gems. I’d installed  rake and Rails via Debian apt-get. Unfortunately, most Ruby gems aren’t available that way.

2. Uninstall rubygems via apt-get. Same logic.

3. Install ruby development headers.
sudo apt-get install ruby1.8-dev

4. Install rubygems from source. This gets me the newest version of rubygems, and also makes all future gems available.

sudo wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
sudo tar -xzvf rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo ruby setup.rb

5. Install Rails.
sudo gem install rails (Installs the latest stable version of Rails. Today it is 2.3.8).

6. Install Passenger.
6a. Install the Passenger Ruby Gem.
sudo gem install passenger

6b. Install the Apache Passenger module.
sudo apt-get install apache2-prefork-dev
sudo apt-get install libapr1-dev (Note: already installed on my machine.)
sudo apt-get install libaprutil1-dev (Note: already installed on my machine.)
sudo passenger-install-apache2-module

6c. Edit my Apache configuration file. In Debian, this is /etc/apache2/apache2.conf.
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.15
PassengerRuby /usr/bin/ruby1.8

Now, some of my web sites use an older version of rails. So:

7. Install older version of rails.
sudo gem install -v=2.1.0 rails

8. Restart Apache.
sudo /etc/init.d/apache2 restart

At this point, Ruby on Rails pages get served, but other content (such as this WordPress blog) does not.

9a. Edit my site Apache configuration.
sudo nano /etc/apache2/sites-available/mysite
inside <VirtualHost> add the following three lines:
<Location /blog>
PassengerEnabled off
</Location>

And these three lines:
<Location /mail>
PassengerEnabled off
</Location>

9b. Restart Apache.
sudo /etc/init.d/apache2 restart

9c. Test the configuration. Make sure everything works. For me, it does.

Rails 2.3.8 - Segmentation Fault

I just upgraded to Rails 2.3.8 on my development machine. I created a standard Rails app, started up WEBrick, and got a Segmentation fault. Not enough code to be my error, so I searched for the core problem and possible solutions.

Turns out the issue is known, and a solution exists. First, I updated to the latest version of Ruby 1.8.6. Not 1.8.7, and certainly not 1.9.2. Too many people reported unresolved problems with Ruby 1.8.7, and Ruby 1.9.2 hasn’t yet gone stable. As of 16 June 2010, the latest version available as an exe for Windows is ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32].

Next, did a gem install rails (version 2.3.8). Third, a gem install mysql (2.8.1 x86-mingw32). This gave errors for the documentation, but the gem installs.

Finally, I copied libmySQL.dll (1484 KB) from http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll as recommended at http://forums.aptana.com/viewtopic.php?f=20&t=7563&p=27407&hilit=libmysql.dll#p27407 . After that, everything just works.

Rails 3.0.0.beta install

Installed Rails 3.0.0.beta on my test machine. Here’s a few notes on what was easy, what was not.

My test machine uses Debian squeeze (testing), ruby 1.9.1 and rubygems 1.3.5. You need to have ruby 1.8.7 or newer. Ruby 1.8.6 will not work.

To find out what version of ruby you have, enter: ruby -v.

For a full rubygems environment listing, enter: gem env

To find out what paths ruby is looking for, enter: irb

followed by: puts $LOAD_PATH

My first attempt in installing Rails 3 failed, with this error message:

sudo gem install rails –pre
ERROR:  Error installing rails:
actionpack requires rack-mount (~> 0.4.0, runtime)

So, I get to go a bit deeper:

sudo gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
sudo gem install rack-mount –version 0.4.0
sudo gem install rails –pre

But this isn’t enough to create a new project. So:

sudo gem install abstract
rails rubyat.com -d mysql

Now, there’s still more to be done in order to use rails console:

sudo apt-get install libmysql-ruby1.9
sudo apt-get install libmysqlclient-dev
sudo gem install mysql — –with-mysql-dir=/usr/bin –with-mysql-lib=/usr/lib/mysql
sudo gem install mime-types
sudo gem install text-hyphen

Finally, enter: rails console and we have the familiar irb console.

SquirrelMail – change_sqlpass plugin

Found a bug in the squirrelmail change_sqlpass plugin. Made the ugly fix; gonna dive in and write a patch later.

The bug occurs in functions.php

There is a mismatch between:
function get_password_salt, where the program reasonably returns the value of the password salt when $csp_salt_static is empty and $csp_salt_query is not empty.

and:
function get_password_encrypt_string, in the switch statement,
it uses the value of the password salt as the name of the password salt field, and the SQL fails.

It’s a one line fix:
– return ‘encrypt(“‘ . $password . ‘”, ‘ . $salt . ‘)’;
+ return ‘encrypt(“‘ . $password . ‘”, “‘ . $salt . ‘”)’;

Rubyat.com

Just finishing setting up four web sites, including rubyat.com.