Blog

Installing Ruby on Rails 3.1.1 with MySql on Windows-7 64-bit

I recently upgraded my Ruby on Rails installation from Ruby 1.8.7 and Rails 2.3.2 to Ruby 1.9.3 and Rails 3.1.1

There are tons of examples on the internet to make Rails 3.1 work with SqlLite on Windows, but it was constantly failing with mySql for me. I figured out that the following sequence of steps works reliably and all the time.

Step1: Install Ruby 1.9.3

1. Download Ruby Installer (v. 1.9.3-p0) for windows from http://rubyinstaller.org/downloads/ image
2. Run the Installation program
3. Accept the license agreement clip_image004
4. Select the installation path. I chose the default c:\Ruby193.
Chose to add Ruby executable to the environment PATH variable.Light bulb This step is very important.Click Install
clip_image006
5. Let the Installation finish clip_image008
6. Click “Finish” to complete the installation clip_image010
7. Open a command prompt and type ruby –vto verify right version of ruby is installed.8. Type gem –v to ensure gem utility is installed9. Run “gem update –system” to ensure that the latest version of gem is installed. clip_image012

Step 2: Install Ruby Dev Kit.

Download and Install the ruby development kit. DevKit installer is available on the same page where you downloaded Ruby Installer from – http://rubyinstaller.org/downloads/.

clip_image014

Follow the installation instructions from here – https://github.com/oneclick/rubyinstaller/wiki/Development-Kit

Step 3: Install MySQL

Download and Install 32bit version of MySQL community server for Windows from here – http://dev.mysql.com/downloads/mysql/

Light bulb I downloaded the 32 bit version of MySQL Server even though my operating system is 64bit. Iwas not able to get the 64bit version work with Rails 3.1’s mysql2 gem.

1. Click “Next” on the first installation wizard screen clip_image018
2. Accept the EULA and click “Next” clip_image020
3. Click “Complete” clip_image022
4. Click “Install” clip_image024
5. Let the installation finish clip_image026
6. Click “Finish” clip_image028

Step 4: Configure MySQL

1. Run the configuration wizard and click “Next”

Light bulbThe configuration wizard should automatically open after the installation wizard finishes.

Light bulbYou can also run the configuration wizard manually from C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\MySQLInstanceConfig.exe

 

<Change the path above if you have a different installation location>

clip_image030
2. Select “Detailed configuration” and click “Next” clip_image032
3. Select defaults and click “Next” clip_image034
4. Select default and click “Next” clip_image036
5. Select default and click “Next” clip_image038
6. Select default and click “Next” clip_image040
7. Select default and click “Next” clip_image042
8. Select “Best support for multilingualism” and click “Next” clip_image044
9. Select the option to include the directory in windows PATH and click “Next”

Light bulbIt is important to select both check-boxes on this page. Do not forget to include the bin directory in Windows PATH.

clip_image046
10. Select a password for the “root” account and click “Next”. clip_image048
11. Click “Execute” clip_image050

Light bulbIf the configuration wizard hangs without showing any progress for more than a minute, kill it and rerun it by executing C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\MySQLInstanceConfig.exe. It usually runs fine the second time.

If you see a screen like the one shown below. Everything has worked well so far !

clip_image052

Step 5: Copy libmysql.dll

Light bulb This is the most important step in the entire process… Copy “C:\Program Files (x86)\MySQL\MySQL Server 5.5\lib\libmysql.dll” to “C:\Ruby193\bin\libmysql.dll”

Step 6: Install rails 3.1.1

Open an “Administrative Command Prompt”

Run “gem install rails –v 3.1.1” command to install the rails version 3.1.1. Note that the “v 3.1.1” is very important.

At the time of writing this blog post, Rails version 3.1.2 was out and 3.1.2 had an incompatibility with sprokets on Windows 7. Ensure that you specify version 3.1.1 in the “gem install” command

clip_image054

Run “rails –v” to ensure that correct version of rails is installed

clip_image056

Step 7: Install mysql2 gem

Type the following command as-is from an elevated command prompt. If needed tweak the installation directories to match the paths on your PC.

Light bulbNote the single quotes around the entire arguments list. It is important. Also note the double quotes around the mysql installation path. They are also important if the mysql installation path has a white space in it.

gem install mysql2 — ‘–with-mysql-lib=”c:\Program Files (x86)\MySQL\MySQL Server 5.5\lib” –with-mysql-include=”c:\Program Files (x86)\MySQL\MySQL Server 5.5\include”‘

clip_image058

Step 8: Test Rails Installation.

1. Create a directory for a test project. Say %userprofile%\Documents\RailsTest

2. Run the following command to create a new rails project called “demo” which binds to the mysql server. You should see the following output on the screen

rails new demo –d mysql

clip_image060

3. The demo directory should have the typical Rails application directory structure that looks like the following.

clip_image062

4. Update the …\demo\config\database.ymlfile to match the mysql user name, password and host infomration

Here is a sample configuration.

development:

adapter: mysql2

encoding: utf8

reconnect: false

database: demo_development

pool: 5

username: root

password: root

host: localhost

5. Run rake db:createto create the demo database.

NOTE:  rake db:create in most cases should work. However, I have experienced in one case where rake db:create used to mess-up MySql’s user password. If you suddenly see that the mysql user is not able to authenticate after running rake db:create, re-install mysql and create the database manually on the mysql prompt instead of running the rake task.

6. Run the web server using the following command “rails server”. The inbuilt ralis development server (WEBrick) will start up and start listening for connections on port 3000.

clip_image064

7. Open a web-browser and point it to http://127.0.0.1:3000 or http://localhost:3000. You should see a webpage that looks like the following.

clip_image066

8. Click on the “About your application’s environment”. The environment should not show any errors.

clip_image068

Step 9: Test a sample Rails Scaffold

Lets create a sample rails scaffold to test if the server is working en-to-end.

1. Generate a scaffold by running the following command

rails g scaffold product name:string description:text price:decimal

clip_image070

2. Run rake db:migrateto create the database tables for the products resource.

clip_image072

3. Open a web browser and navigate to http://127.0.0.1/products.

4. You should see a products listing page – feel free to play around with it – create new products, delete them, edit them, etc.

 

Hope this blog post gives you a clear guide to go about installing rails on Windows.

Happy Coding !!!

, ,

4 Responses to Installing Ruby on Rails 3.1.1 with MySql on Windows-7 64-bit

  1. Mihir January 4, 2012 at 11:15 am #

    I was setting up my other dev box and I hit another issue today — C:/Ruby193/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11-x86-mingw32/lib/mysql2/mysql2.rb:2:in `require’: Incorrect MySQL client library version! This gem was compiled for 6.0.0 but the client library is 5.5.17. (RuntimeError) I also found a workaround – open the gemspec file for mysql2 gem – for me it was under C:\Ruby193\lib\ruby\gems\1.9.1\specifications\mysql2-0.3.11-x86-mingw32.gemspec. Locate the line that starts with s.post_install_message =. the line has a long message and it also specifies which mysql connector the gem was compiled against. it also has a URL to download the right library – download libmysql.dll from that path and place it in your ruby\bin folder. the download URL for me was — http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick.

  2. Mihir January 5, 2012 at 4:33 am #

    Found another solution to the mysql2 gem problem ion stackoverflow — http://stackoverflow.com/questions/5841406/cannot-install-mysql2-gem

    using: Win7 64 / Mysql 5.5 64-bit / Ruby 1.9.2
    download libmysql.dll from here – http://www.vertstudios.com/blog/libmysqldll-32-bit-download/
    copy above libmysql.dll to C:\Ruby193\bin AND C:\Ruby193\lib (or wherever your lib and bin are)
    gem install mysql2 -v 0.3.10 — ‘–with-mysql-lib=”c:\Program Files\MySQL\MySQL Server 5.5\lib” –with-mysql-include=”c:\Program Files\MySQL\MySQL Server 5.5\include” –with-mysql-dir=”c:\Program Files\MySQL\MySQL Server 5.5″‘

    • Zariel January 10, 2012 at 12:00 pm #

      Was totally stuck until I read this, now back up and rnunnig.

  3. Aparna June 13, 2012 at 3:56 pm #

    if you are hitting this error
    Can’t connect to MySQL server on ‘localhost’ (10061)

    One solution that worked for me was to uncomment the line
    127.0.0.1 localhost
    in %windir%\system32\drivers\etc\hosts file on windows-7 32bit

Leave a Reply

Read more:
Key Takeaways from Inbound Marketing Summit San Francisco

What a congruous name for a digital conference with social marketing highlights! A room full of digital marketing experts who...

Recognition and Empathy – The Two Key Ingredients for Success in Social Marketing

I attended the Enterprise 2.0 conference in Santa Clara, CA (#e2conf) this week, which focuses primarily on internal social business...

Do You Know Your Brand Advocates?

With more and more people relying on word of mouth referrals and recommendations from friends to make purchasing decisions, it...

Close