Installing Ruby 1.9.1, Rails and mysql-ruby 2.8.1 on Windows 7 RC and MySQL 5.0

Posted: June 18th, 2009 | Author: | Filed under: Test Lab | Tags: , , , , , , , , , | 9 Comments »

This is the short story or even a saga of all rises and falls during installation of Ruby 1.9.1, latest Rails and frustration to get MySQL work with Ruby/Rails via mysql-ruby Gem.

The Vision

I really haven’t been Ruby/Rails coding since 2007. And last weekend I just got frustrated with .NET MVC, so I started to switch to Ruby/Rails. Mainly, I was interested in Ruby 1.9 new features, so logically I tried to install Ruby 1.9. Since I got no Mac (sorry to say, but is so expensive that I can’t afford it), and I really have serious troubles with my HP 6715b (crappy laptop) and Linux distribution and also I can’t install Mac on him, mainly because its catastrophically poor choices in BIOS.

And so there’s only me and my buddy Windows 7 RC. Yep, on Ruby main site there is a One-Click installer but for version 1.8.6, but they have link on Ruby download page binary for 1.9.1-p0 which is old. No problem, you can go to Ruby FTP site and find latest Ruby 1.9.1-p129 binary.

The Problem and “FIX”!?

With this version I couldn’t even install RubyGems which is preliminary for installing Rails. What to do next, you guess it, ask my friend Google, and he is one of my best friends (in need). In whole bunch sites in search I found this one blog “The Cabin” with instructions for installing Ruby dependencies. After applying all of the dependencies I could install Gem and Rails with no problem. But, when I try to generate Rails application, guess it, I had an error, missing libiconv2.dll, and I found it, install it, life is good, everything worked.

RubyMine Idyll

I heard a lot of good words for the new JetBrains product RubyMine IDE for Ruby/Rails, since my must have tool is also JetBrains ReSharper (R#). It was a good reference to try it, and I can proudly say that RubyMine is great IDE. There was attempts for creating Ruby/Rails IDE but they are all very frustrating and disappointing. And also I have to mention the RubyMine price of 100$ is so affordable for all features given with it like refactoring, code completion, and support for my favorite Rails view engine Haml now only with syntax highlighting but with more to come.

So close, no matter how far

And everything is good, perfect I could say, but… I was getting to know RubyMine IDE, and after two and half years i start to create Rails application. Scaffolding was my primary choice, and it generate all the files for my entity “club” with few attributes like name, address, location… I was so excited, and all I have to do is run command rake db:migrate, and Rails shall create my SQLite (Since 2.2.1 default database for Rails) file with table “club” and all its given attributes.

The Frustration

I was so near, but I got error, the one reasonable. I haven’t installed Gem sqlite3-ruby, installing Gem, piece of cake, all I have to do is:

gem install sqlite3-ruby

In my command prompt there was some cryptic errors, hell with it, I don’t even need SQLite, MySQL is my database of choice, but I also should have to install mysql Gem

gem install mysql

can you guess, the cryptic error is all I got. Optimistically or naively I tried to execute rake db:migrate, and it fails, and for some unknown reason it prompt me with missing msvcrt-ruby18.dll. Why, I have msvcrt-ruby191.dll, why the hell it searches for old Ruby 1.8 dll.

The War

Now, I was really mad. The War is declared. I needed help, and I have to ask my old friend Google to find the answer to this mystery. I wasn’t the only one mad, and frustrated, many were defeated and get back to Ruby 1.8.6, but not me, I never give up, when there is hope – there is a will!

I stumble upon this Ruby forum post and found instructions for mysql-ruby in last but one reply. I did everything told, but with mysql-ruby-2.8 (in post he speaks of 2.7.3) version. I have made my own mysql-ruby Gem (I was happy) but when I tried to install it, same cryptic error.

The Light

I saw spark of light in this darkness when I stumble upon this post (and again on Google Group). Read the solution from Roger Pack, but at first I haven’t understand it, knew that I really missing MinGW but didn’t know what was really necessary to install to get it work properly.

And on the same post I found link to this blog post, and in it there is all I need to understand how to set up MinGW, and most importantly the link to the RubyInstaller downloads page.

RubyInstaller downloads page have two links the first is the binary of Ruby 1.9.1-p129 but not compiled as win32 (downloadable from main Ruby download page), but as mingw32 (both for i386 platform). To be more accurate about version:

ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]

And important one link to the Development Kit (MinGW 3.4.5 + MSYS 1.0.11), very much needed to complete the Robert Pack solution.

Note: Both links are 7zip archived files. It can be uncompressed with WinRAR.

Only I have to do is uncompress both files in C:\ruby directory, and have this directory structure:

  • bin
  • lib
  • share
  • devkit

To use Ruby easily in Command prompt, we have to put his bin directory in windows PATH. In Environment Variables for path add “C:\ruby\bin” and then open Command prompt and write this command:

C:\>ruby -v
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-mingw32]

The Solution

Now is coming Robert Pack solution in tasks assuming that step before is done (Ruby and MinGW in same directory):

  1. Download MySQL 5.0 (not 5.1) for Windows “without installer (unzip in C:\)”.
  2. Unzip, put it in c:\mysql
  3. Download latest mysql-ruby source as version 2.8.1. Unzip it, and I put it in C:\ruby directory.
  4. Edit extconf so that anywhere it says mswin32 it now says mswin32|mingw.
  5. Make sure that mysql bin is *not* in your Windows PATH.
  6. run in Command prompt ruby extconf.rb –with-mysql-include=c:/mysql/include –with-mysql-lib=c:/mysql/lib/opt.
  7. run in Command prompt make, and then make install.
  8. Add libmysql.dll to your windows PATH. The path is “c:\mysql\lib\opt”.
  9. Exit from Command prompt, for PATHs to be reset, open again Command prompt and run ruby -e ‘require “mysql”‘

The Victory and the Final Touch

Now, everything should work, but we have two problems:

  1. Give to the MySQL root user a password (default is blank password).
  2. Set MySQL as windows service to start up automatically.

For giving root user a password run this command in Command prompt.

C:\>mysqladmin -u root password lida

Change “lida” to your favorite and top secret password.

For setting MySQL as Windows Service requires adding to Windows PATH a path to the bin directory of MySQL – “c:\mysql\bin”. In Command prompt then run:

C:\>mysqld-nt –install MySQL –defaults-file=”C:\mysql\my.ini”
Service successfully installed.

Note: The problem of downloaded MySQL is that have many my-*.ini files. And there is no my.ini file. So above command couldn’t be executed, only if you renamed one. Here is mine my.ini:

[client]
port=3306
[mysqld]
port=3306
basedir=”c:/mysql”
datadir=”c:/mysql/data/”
default-character-set=latin1
default-storage-engine=INNODB
max_connections=100
query_cache_size=0
table_cache=256
tmp_table_size=5M
thread_cache_size=8
myisam_max_sort_file_size=100G
myisam_max_extra_sort_file_size=100G
myisam_sort_buffer_size=8M
key_buffer_size=8M
read_buffer_size=64K
read_rnd_buffer_size=256K
sort_buffer_size=212K
innodb_additional_mem_pool_size=2M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=1M
innodb_buffer_pool_size=8M
innodb_log_file_size=10M
innodb_thread_concurrency=8

Copy this and create my.ini file and paste it in MySQL directory. Be sure that MySQL is in C:\mysql directory!

Now in Windows Service should be new service with name MySQL.

Note: If something goes wrong and you are stuck with not properly working service. Remove it with this command in Command prompt:

C:\>mysqld –remove
Service successfully removed.

The Beauty in Red (as Ruby)

Now is time for beauty, after all the pain and frustration. We have to test all the glory of the Ruby and it web framework Rails. You can follow me, and Command prompt is your only friend now. Save the bliss of RubyMine for later time!

C:\>cd ruby

C:\ruby>mkdir railsapps

C:\ruby>cd railsapps

C:\ruby\railsapps>rails testapp

You’ll get listing of all files generated, but we have to set up our new application to find our MySQL database. The directory that we are interested in is in your new application directory (C:\ruby\railsapps\testapp). Here you will find config directory, and inside database.yml YAML file. It is now configured for SQLite3 but we have to adjust it to MySQL, and here it is.

development:
adapter: mysql
database: test
user: root
password: lida
test:
adapter: mysql
database: test
user: root
password: lida
production:
adapter: sqlite3
database: test
user: root
password: lida

Of course, change password to suite your needs!

Note: That I have one database for development, test and production. This is not wise but for the test application it is OK!

First make sure that you are in your application directory. Then we shall do some scaffolding like this:

C:\ruby\railsapps>cd testapp

C:\ruby\railsapps\testapp>ruby script\generate scaffold Club name:string adress:
string location:string contact_person:string phone:string

You should see yet again whole bunch of generated files. Now, the great finale, migrate our model to the MySQL database with help of rake:

C:\ruby\railsapps\testapp>rake db:migrate
(in C:/ruby/railsapps/testapp)
== CreateClubs: migrating ==================================================
– create_table(:clubs)
-> 0.0910s
== CreateClubs: migrated (0.0920s) ===========================================

Only thing is now is left, to start server and test our new web application:

C:\ruby\railsapps\testapp>ruby script\server
=> Booting WEBrick
=> Rails 2.3.2 application starting on
http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2009-06-18 13:34:48] INFO WEBrick 1.3.1
[2009-06-18 13:34:48] INFO ruby 1.9.1 (2009-05-12) [i386-mingw32]
[2009-06-18 13:34:48] INFO WEBrick::HTTPServer#start: pid=4836 port=3000

Note: In starting of server I got error again missing msvcrt-ruby18.dll, but on click OK server is started and everything works perfectly.

Open your favorite browser and write or copy or run it from here http://localhost:3000/Clubs

club_list

And now we could add new club:

club_add

And we have first club:

club_show

In listing we have one club.

club_list1

And that’s all folks. I think is worth trying Ruby and Rails even though they are not good and flexible in Windows as in Linux and Mac. Next post will be for setting Git source code management for source control and for downloading many Rails plugins.

Update: The complete working Ruby 1.9.1 directory with MinGW Development Kit, Rails and mysql-ruby 2.8.1 you can download it from ruby.rar.