Installing Rails on Windows (3 years later)
**UPDATED June 2010 – just installed on Windows 7 ;)**
**UPDATED October 2010 – now also added Rails3 commands**
So, a lot has changed from the time when I first wrote a short beginner tutorial for installing Ruby on Rails. This is the reincarnation (essentially I needed to write it up from scratch) of an installation process for Rails. There were quirks in Windows installation the last 3 years I was installing Rails, but this time it went really smooth and quick, so I’ve deleted loads of workarounds and the tutorial is now really simple.
Most of the stuff you will do from the command line, to get the command line in windows, go to start menu/run and type “cmd”. You will also need to have an Internet connection during the installation as most stuff is getting updated via the Internet.
Please note that if you see a command in quotes, “like this”, you should disregard the quotes, ie. type the command without them.
1. Ruby – Download and install Ruby installer here. It’s very easy to install. When installing, click checkboxes that ask you to associate Ruby with .rb files and add Ruby to the PATH. Btw, do remember where you installed Ruby, it will come in handy later.
Once installed, from the command line type
and you should see something like this: ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-mingw32]. If you see “Command not found”, make sure that you reload the command (close & open new) window after the install.
2. Update gems. Just in case, from command line run
gem update --system
(that’s two “-”!). It will take 2-3 mins to update.
3. Rails. Simply type in cmd: gem install rails
It will not show any progress for some time, but if your HDD is working, it’s all fine. In my case it installed rails and other related gems (in total 8 gems). After that it installs ri documentation / RDoc documentation, and it takes quite some time (much longer than gems themselves), so be patient. By the way, chances are you will NOT be using local documentation for Rails, as it’s all on the web anyway, so you can run this command instead: gem install rails –no-rdoc –no-ri
4. Create your first project. Now, create a directory for your project. In Windows VISTA (and 7, I think) I do NOT recommend creating it in your root folder or Program Files, as VISTA has some pain-in-the-ass safety mechanism that will not allow many programs access the files you put there. The best way is to create a folder in your “My Documents” folder, you can call it Rails. In your cmd type “cd path/to/your rails folder” (or you can always use an UI-shell like Windows Commander).
Type “rails hello” (in Rails 3 it should be “Rails new hello”) and it should output a bunch of lines “create app/controllers”, etc.
5. Run webserver. Good thing is that Rails comes with its own web server, so no need to set up apache. The default web server is WEBrick, but you can at any point upgrade to Mongrel, which is a production-level server. For now – don’t worry! Just remember, that in order to restart the web server, you need to press Ctrl-Break and retype the “ruby script/server” command (in Rails 3 you now need to type “rails server” instead!).
In step 4 you have created the project called hello. You will see that within your chosen directory for rails projects a directory “hello” was created and a bunch of subdirectories as well (like “app”, “config”, “db”, etc.). Go into the “hello” directory (still in your cmd) and run the webserver command “ruby script/server” (or in Rails3 use “rails server” . You should see something like “Booting WEBrick”.
6. Now, test the installation in your browser. Use your favorite browser and type in the address: http://127.0.0.1:3000
You should see a standard Rails splash screen, it will say “Welcome aboard”. Rails is installed!
7. Finally, install the database. For your dev box sqlite3 is the best choice, it’s lightweight (like, less than 1mb!) and easy to install. Go to the project’s site and get 2 zip archives precompiled binaries for Windows section. You need to download sqlite3 command line program (here’s the current version AT THE TIME OF WRITING) AND sqlite dlls (you do not need TCL bindings) – here’s the current version AT THE TIME OF WRITING. Download and extract the files to your Ruby bin directory (!).
Test that it works correctly – run sqlite3 in the command line. It should say something like “SQLite version 3.6.23″ and show you a prompt (you can type SQL commands there, but don’t do it now). Now just type “.exit” or press Ctrl-Break to exit.
7.1. Now, you need to install the sqlite3 gem. Simply run “gem install sqlite3-ruby”.
8. Write code. The installation is now complete, but why stop where all the fun should begin? Let’s create our first program in Rails now!
The good thing about sqlite3 is that it doesn’t require passwords or setting up of your database, so you can start coding almost straight away. Now let’s try out the (in)famous Rails scaffold script. Run the following command in your “hello” directory:
ruby script/generate scaffold post title:string description:text
Or in Rails3:
rails generate scaffold post title:string description:text
This command creates a Post scaffold, a model/controller/view ready to be updated. Note – from now on majority of commands that you run, you should run in cmd from within your “hello” project root directory. If you go to sub-directory or a directory above your project’s root, commands will not work.
The post model will have 2 fields – a title which is a single line of text and a description, which is multi-line text. Note, that it’s a Rails notation that models should be written in singular.
Now, in your browser go to the http://127.0.0.1:3000/posts. It should give you an error ActiverRecord::StatementInvalid in PostsController#index.
9. Run the database migration. This is because after creating the scaffold, the database is not automatically updated with the new blog model, you need to do it manually by running a rake db:migrate command. Do it now.
A note on using SQL tools. In my previous post on working with Rails on Windows I’ve suggested to use MySQL Query Designer or phpadmin or something similar for creating tables. Now, with the updated tools of Rails 2.3.x you should NOT work with the database at such low level, you should work with migrations and migrations only! Migrations are easy to understand, and once you understand them, you will never want to create tables in any other ways. It also gives you other benefits, like ability to migrate up or down, etc.
Ok, so a successful rake db:migrate command will show something like CreatePosts: migrating, create_table(:posts) and show time it took to run the migration.
Before going to the next step, kill your command line window that runs the web server and restart it again by typing “ruby script/server” (in Rails 3 “rails server”). You will need to do this every time you install a new gem, as the gems are loaded at the start. If you don’t restart your server, you will see an error (We’re sorry, but something went wrong error) on the next step. These are errors that are difficult to catch, even by looking at the logs (online logs are shown in the cmd window of your web server). So, don’t forget to try and reload the server if you’re lost.
10. Show me the money… Ok, lets check the browser again – you should now see the page saying “Listing posts” and you can add new posts, edit them and delete. If you want to play around with the code, you will find the code in the hello/app directory.
11. Git. Definitely a requirement if you want to use Rails seriously. Git is a version-control system that has become a de facto standard for rails developers. Most of the latest plugins are hosted on Git. In order to use Git hosted plugins you will need to install a Git client. It’s easily installable and is available from here (you need to download a full non-portable installer, don’t install the installer that says “download if you want to hack on Git”). When installing Git, you can select different “types” of installation, select the one that says “Bash only”.
12. Learning Ruby. Now, it’s probably time to learn Ruby and Ruby on Rails. I’ve learned Ruby and Rails from 3 great books that I link to on my blog. If you are looking for free Rails web resources, I posted about them here.
13. Next, you should think about your development environment You should select the best tools for editing your rails files, running scripts, etc. straight away. You can check my dev environment here.
14. Create you first project. Now that the technical part is over, you should be dying to do something useful in Rails. As a very basic example you can follow this 5 minute Rails tutorial.
15. Plugins Finally, if you are wondering which plugins / extensions you should use, I wrote about Rails plugins here.