Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Agile Web Development With Rails, 1st Edition (2005).pdf
Скачиваний:
28
Добавлен:
17.08.2013
Размер:
7.99 Mб
Скачать

Chapter 3

Installing Rails

Before you can start writing a Rails application, you’ll need to download the Rails framework and install it on your computer. All you need to run Rails is a Ruby interpreter (version 1.8.2 or later) and the Rails code. However, things go easier if you also have the RubyGems package management system available, so we’ll talk about getting that installed too. Finally, if you use a database other than MySQL, you may need to install the appropriate Ruby libraries to interface with it.

Fair warning: this is a tedious chapter, full of “click that” and “type this” instructions. Fortunately, it’s short, and we’ll get on to the exciting stuff shortly.

Let’s look at the installation instructions for Windows, OS X, and Linux.

3.1 Installing on Windows

1.First, let’s check to see if you already have Ruby installed. Bring up a command prompt (using Start > Run > cmd, or Start > Programs > Accessories > Command Prompt), and type ruby -v. If Ruby responds, and if it shows a version number at or above 1.8.2, we may well be in business. One more check—let’s see if you have RubyGems installed. Type gem - -version. If you don’t get an error, skip to step 3. Otherwise, we’ll install a fresh Ruby.

2.If Ruby is not installed, there’s a convenient one-click installer at http://rubyinstaller.rubyforge.org. Follow the download link, and run the resulting installer. You may as well install everything—it’s a very small package, and you’ll get RubyGems as well.

Prepared exclusively for Rida Al Barazi

INSTALLING ON MAC OS X 20

3.Now we’ll use RubyGems to install Rails and a few things that Rails needs.

C:\> gem install rails --include-dependencies

Congratulations! You’re now on Rails.

3.2 Installing on Mac OS X

1.OS X version 10.4 (Tiger) ships with Ruby 1.8.2. You can verify this by starting the terminal application (use the Finder to navigate to

Applications Utilities and double-click on Terminal) and entering ruby -v at the prompt. (If you’re not running Tiger, you’ll need to install Ruby 1.8.2 or later yourself. The Unix instructions that follow should help.)

2.Next you have to install RubyGems. Go to http://rubygems.rubyforge.org and follow the download link. OS X will typically unpack the archive file for you, so all you have to do is navigate to the downloaded directory and (in the Terminal application) type

dave> tar xzf rubygems-0.8.10.tar.gz dave> cd rubygems-0.8.10 rubygems-0.8.10> sudo ruby setup.rb Password: <enter your password>

3.We’ll now use RubyGems to install Rails. Still in the Terminal application, issue the following command.

dave> sudo gem install rails --include-dependencies

Congratulations! You’re now on Rails.

3.3 Installing on Unix/Linux

You’ll need to have Ruby 1.8.2 (or later) and RubyGems installed in order to install Rails.

1.Many modern distributions come with Ruby installed. Bring up your favorite shell and type ruby -v. If Ruby responds, and is at least version 1.8.2, skip to step 3.

2.You’ll probably be able to find a prepackaged version of Ruby for your distribution. If not, Ruby is simple to install from source.

a)Download ruby-x.y.z.tar.gz from http://www.ruby-lang.org/en/.

b)Untar the distribution, and enter the top-level directory.

c)Do the usual open-source build.

Prepared exclusively for Rida Al Barazi

Report erratum

RAILS AND DATABASES 21

Ruby on Mac OS X Tiger

It’s good that Apple includes Ruby in OS X. Unfortunately, Ruby isn’t configured particularly well in OS X version 10.4 (Tiger). Support for the readline library isn’t configured, making interactive tools such as irb a lot harder to use. The Ruby build environment is also incorrect, so you can’t build extension libraries until you fix it. And, just to make matters worse, we have reports that the Ruby MySQL extension library doesn’t work properly.

One way of addressing both issues is to follow Lucas Carlson’s instructions at http://tech.rufy.com/entry/46 (you’ll need the developer tools installed). Once you’ve done this, you should be able to reinstall the Ruby MySQL gem and things should start working.

An alternative for the adventurous is to reinstall Ruby using fink or Darwin

Ports. That’s a pretty big topic, and not one that we’ll cover here.

dave> tar xzf ruby-x.y.z.tar.gz dave> cd ruby-x.y.z ruby-x.y.z> ./configure ruby-x.y.z> make

ruby-x.y.z> make test ruby-x.y.z> sudo make install

Password: <enter your password>

3.Install RubyGems. Go to http://rubygems.rubyforge.org, and follow the download link. Once you have the file locally, enter the following in your shell window.

dave> tar xzf rubygems-0.8.10.tar.gz dave> cd rubygems-0.8.10 rubygems-0.8.10> sudo ruby setup.rb Password: <enter your password>

4.We’ll now use RubyGems to install Rails. Still in the shell, issue the following command.

dave> sudo gem install rails --include-dependencies

And (one last time), congratulations! You’re now on Rails.

3.4 Rails and Databases

If your Rails application uses a database (and most do), there’s one more installation step you may have to perform before you can start development.

Prepared exclusively for Rida Al Barazi

Report erratum

RAILS AND DATABASES 22

Rails works with the DB2, MySQL, Oracle, Postgres, SQL Server, and SQLite databases. For all but MySQL, you’ll need to install a database driver, a library that Rails can use to connect to and use your database engine. This section contains the links and instructions to get that done.

Before we get into the ugly details, let’s see if we can skip the pain altogether. If you don’t care what database you use because you just want to experiment with Rails, our recommendation is that you try MySQL. It’s easy to install, and Rails comes with a built-in driver (written in pure Ruby) for MySQL databases. You can use it to connect a Rails application to MySQL with no extra work. That’s one of the reasons that the examples in this book all use MySQL.1 If you do end up using MySQL, remember to check the license if you’re distributing your application commercially.

If you already have MySQL installed on your system, you’re all done. Otherwise, visit http://dev.mysql.com, and follow their instructions on installing a MySQL database on your machine. Once you have MySQL running, you can safely skip ahead to Section 3.6, Rails and ISPs.

If you’re still reading this, it means you’re wanting to connect to a database other than MySQL. To do this, you’re going to have to install a database driver. The database libraries are all written in C and are primarily distributed in source form. If you don’t want to go to the bother of building a driver from source, have a careful look on the driver’s web site. Many times you’ll find that the author also distributes binary versions.

If you can’t find a binary version, or if you’d rather build from source anyway, you’ll need a development environment on your machine to build the library. Under Windows, this means having a copy of Visual C++. Under Linux, you’ll need gcc and friends (but these will likely already be installed).

Under OS X, you’ll need to install the developer tools (they come with the operating system, but aren’t installed by default). Once you’ve done that, you’ll also need to fix a minor problem in the Apple version of Ruby (unless you already installed the fix from Lucas Carlson described in the sidebar on the preceding page). Run the following commands.

dave> # You only need these commands under OS X "Tiger" dave> sudo gem install fixrbconfig

dave> sudo fixrbconfig

1Having said that, if you want to put a high-volume application into production, and you’re basing it on MySQL, you’ll probably want to install the low-level MySQL interface library anyway, as it offers better performance.

Prepared exclusively for Rida Al Barazi

Report erratum

RAILS AND DATABASES 23

Databases and This Book

All the examples in this book were developed using MySQL (version 4.1.8 or thereabouts). If you want to follow along with our code, it’s probably simplest if you use MySQL too. If you decide to use something different, it won’t be a major problem. You’ll just have to make minor adjustments to the DDL we use to create tables, and you’ll need to use that database’s syntax for some of the SQL we use in queries. (For example, later in the book we’ll use the MySQL now( ) function to compare a database column against the current date and time. Different databases will use a different name for the now( ) function.)

The following table lists the available database adapters and gives links to their respective home pages.

DB2

http://raa.ruby-lang.org/project/ruby-db2

MySQL

http://www.tmtm.org/en/mysql/ruby

Oracle

http://rubyforge.org/projects/ruby-oci8

Postgres

http://ruby.scripting.ca/postgres/

SQL Server

(see note after table)

SQLite

http://rubyforge.org/projects/sqlite-ruby

There is a pure-Ruby version of the Postgres adapter available. Download postgres-pr from the Ruby-DBI page at http://rubyforge.org/projects/ruby-dbi.

MySQL and SQLite are also available for download as RubyGems (mysql and sqlite respectively).

Interfacing to SQL Server requires a little effort. The following is based on a note written by Joey Gibson, who wrote the Rails adapter.

Assuming you used the one-click installer to load Ruby onto your system, you already have most of the libraries you need to connect to SQL Server. However, the ADO module is not installed. Follow these steps.

1.Find the directory tree holding your Ruby installation (C:\Ruby by default). Below it is the folder \Ruby\lib\ruby\site_ruby\1.8\DBD. Inside this folder, create the directory ADO.

2.Wander over to http://ruby-dbi.sourceforge.net and get the latest source distribution of Ruby-DBI.

3.Unzip the DBI distribution into a local folder. Navigate into this folder, and then to the directory src\lib\dbd_ado. Copy the file ADO.rb from

Prepared exclusively for Rida Al Barazi

Report erratum