SqliIte3 error when starting rails server on Windows

In my previous post, I made it sound like the Rails installation was painless, and it was apart from one point. When starting ‘rails server’, it complained that it couldn’t find sqlite3-ruby. I would have expected this to be installed as part of the rails gem, but it’s a simple installation anyway:

gem install sqlite3-ruby

However, when attempting to start the server again, I received the following message dialog.

ruby.exe – System Error
The program can’t start because sqlite3.dll is missing from your computer. Try reinstalling the program to fix this problem.

With the stacktrace of:

C:/Ruby192/lib/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.1-x86-mingw32/lib/sqlite3.rb:6:in `require’: no such file to load –
– sqlite3/sqlite3_native (LoadError)
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.1-x86-mingw32/lib/sqlite3.rb:6:in `rescue in uired)>’
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.1-x86-mingw32/lib/sqlite3.rb:2:in `
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in `require’
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.0/lib/bundler/runtime.rb:64:in `block (2 levels) in require

To solve the problem (and considering this is only my local dev machine) I downloaded sqlite3.dll from http://www.sqlite.org/sqlitedll-3_7_2.zip. I then copied the two files into C:WindowsSystem32. 

I could then happily execute rails server. Admitted, not the best way but it was the quickest 🙂

Installing Rails 3.0, Ruby 1.9.2 and Pik on Windows

As you may have heard, Rails 3.0 final and Ruby 1.9.2 have been released. These have got a large number of features, with both representing the next stage of Ruby development.

While Rails 3.0 won’t cause too many problems, it does require Ruby 1.8.7 or higher. As Ruby has grown, multiple versions have been released which can live happily side-by-side. Sadly, command lines don’t make it easy to specify which version you want to execute.

For example, I have two versions of MSBuild on my machine with my command prompt being aware of both:

>where msbuild

However, by default, it will execute the one it finds first when scanning the directories set in the PATH:

Microsoft (R) Build Engine Version 4.0.30319.1

The same is true with Ruby. Yet, this is complicated by the fact that there are multiple different executables (gem, rake, cucumber, spec, rails etc) together with different gems depending on the core Ruby version – 1.8 and 1.9 will have different gems installed even on the same machine. 

To help manage this, the Ruby Version Manager (RVM) was created. “RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems”

Sadly, RVM doesn’t work on Windows. Thankfully, Pik does – “Pik is a tool to manage multiple versions of ruby on Windows”.

This means we can do the following:

>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]

>pik switch 192

>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

Below is how I installed Pik, along with Ruby 1.9.2 and Rails 3.0 to start the next part of my Ruby development journey.

Installing Pik

My machine already has Ruby 1.8.6 (ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]) installed which means I can install pik via RubyGems. If you don’t already have Ruby, the README file on GitHub has a section called “Install pik using the installer“ which I recommend you follow.

gem install pik

After downloading the gem, you need to install pik into a directory on your machine. This can be anywhere, apart from where you already have Ruby installed (C:rubybin).

>mkdir C:pik

You need to include the directory used above in your %PATH% environment variable before the location of any existing Ruby installation. I put it at the start.

When installing, simply specify the directory you picked.

>pik_install C:pik
Thank you for using pik.

After which, you will have three files in the folder. This is everything required for pik.

29/08/2010  17:08               119 pik.bat
29/08/2010  17:08               145 pik.ps1
29/08/2010  17:08           694,272 pik_runner.exe

Using Pik

I can now start working with Pik. Executing the pik command will locate all existing Ruby installations and configure itself.

** Adding:  186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
Located at:  C:Rubybin
Usage: pik command [options]

Executing pik list outputs all the ruby installations it knows about. 

>pik list
* 186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]

As Rails 3.0 requires 1.8.7 or higher, let’s install the latest version of Ruby.

>pik install ruby
** Downloading: 
   to:  C:UsersBen Hall.pikdownloadsruby-1.9.2dev-preview3-i386-mingw32-1.7z

Sadly, this still tried to install a dev preview, even with the latest version released. Luckily, it can be installed manually.

Manually installing Ruby 1.9.2

By default, installing Ruby on Windows can be somewhat difficult. Thankfully, a 1.9.2 one click installer has been released which you can download here – rubyinstaller-1.9.2-p0.exe

After clicking next a few times, I installed Ruby into C:Ruby192.

I then need to tell pik about my installation by pointing it at the bin directory. That’s it.

>pik add C:Ruby192bin
** Adding:  192: ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
Located at:  C:Ruby192bin

If you list pik, then you can see all the different versions installed:

>pik list
* 186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
  192: ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

You can then switch to the particular version you want, for example originally I was running 1.8.6.

>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]

But I can then switch to 1.9.2 with a simple command.

>pik switch 192

>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

Installing Rails 3.0

That’s the hard bit done. I didn’t have install pik but during the transition between 1.8.6 and 1.9.2 I *think* this will be invaluable. With 1.9.2 now configured and set as my current pik environment I can just install rails.

>gem install rails

Yep, that’s it. 

Rails 3.0 Hello World!!

To create a new rails app, let’s just execute the command:

>rails new HelloWorld3 

Followed by:

>rails server
=> Booting WEBrick
=> Rails 3.0.0 application starting in development on
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-08-29 18:01:27] INFO  WEBrick 1.3.1
[2010-08-29 18:01:27] INFO  ruby 1.9.2 (2010-08-18) [i386-mingw32]
[2010-08-29 18:01:27] INFO  WEBrick::HTTPServer#start: pid=7472 port=3000

I now have our Rails 3.0 application running on top of Ruby 1.9.2 on Windows.


If I ever need to go back to Rails 2.3.5, I just type the following:

>pik default

>rails -v
Rails 2.3.5

Capistrano – sudo: must be setuid root sudo: must be setuid root sudo: must be setuid root

I’m in the process of trying to automate the deployment of a rails application. For this I’m using a framework called Capistrano. However, when I tried to deploy onto the remote server I was given the following error:
pretty:railsapp Ben$ cap deploy:setup
* executing `deploy:setup'
* executing "sudo -p 'sudo password: ' mkdir "
servers: ["attendoo.com"]
[attendoo.com] executing command
** [out :: attendoo.com] sudo: must be setuid root
command finished

By default, Capistrano uses sudo by default, as such in the deploy.rb file you need to set it to not use sudo

set :use_sudo, false

After this, when you deploy it will not attempt to issue the commands as sudo, instead just as the user you are logged in as.

MySQL – Creating a new user

It’s funny how you forget the simple things when you haven’t done them in a while.

Tonight I wanted to setup MySQL on my Windows 7 machine. The installation was simple and I happily had my root account created. However, I didn’t want to use my root account for development – mainly because I didn’t want the password to be sorted in clear text.

In order to create a new user, I used the command line MySQL client tool which allows me to execute commands against the server. You enter this via the command.

mysql --user="root" ––password

This will then prompt you for the root password. I could have entered this on the command line as well, but again it would be in the clear.

From the tool, I enter the following two commands. The first creates the user, the second assigns permissions.

CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password';

GRANT ALL ON *.* TO 'new_username'@'localhost';

I can then happily use this new user with my application. I wanted to post this in case anyone else keeps forgetting like me…