Sunday, December 17, 2006

Let's Get This Started!

One of the many reasons I started to program in Ruby was the sheer beauty of the code. You can get lots done with minimal coding and still have readable, maintainable code. But instead of me jabbering on, why don't we get things rolling, shall we?

Are You Installed???

[Brian and Peter are putting a crib together.]

Brian: Okay, insert rod support A into slot B.

Peter: That's what...

Brian: If you say "that's what she said" one more time, I am gonna pop you.

-Family Guy

I develop my Ruby and Rails applications on a Dell laptop running Ubuntu Edgy. You can still follow along if you are on a Windows or Mac machine, but the screenshots will be different. There are specific installation packages you will have to, um, install, for your platform. Check the end of this article for specific links for your platform.

For you to start ruling the world with Rails, you will need the following:

  • Ruby
  • RubyGems
  • Rails framework
  • MySQL database (can also run on DB2, Oracle, Postgres, SQL Server, and SQLite)

Installing Ruby:
Open a console window and type in the following:

# ruby -v

Most likely you will get a prompt telling you that ruby is an unknown command. However, if you get something like this:


ruby 1.8.5 (2006-08-25) [i686-linux]

. . . then someone loves you upstairs and has already installed Ruby on your distro! For now, let's go over installing Ruby on Ubuntu:


  • Use the apt-get command. For Debian based distros (including Ubuntu!) you can simply type in the following command:

    # apt-get install ruby
    Note: you may have to sudo the apt-get. You will need to belong to the sudoers list in order to execute this. You can find the list in /etc/sudoers. Use the visudo command to add yourself to the list.

  • Compile Ruby from source. This will apply to all Linux distros and will be a little bit more of a heavyweight install as opposed to package managers. The tradeoff is you will have the latest stable version of Ruby. Being that you are on a badass Ubuntu system, let's compile from source and get the full taste of building Ruby just for us!

Before we get started, let's get some tools installed to make everything flow smoothly:


# sudo apt-get install build-essential zlib1g-dev

The build-essential package will provide you with make and the Gnu Compiler Collection (GCC) which is used to compile things from source. Think of them as your screwdrivers, hammers, and nails that we'll use to build our awesome Rails framework (actually, we should think of those tools as hacksaws since we are in a sense hacking, but I digress . . .)

Of course now we could visit our friends at www.ruby-lang.org and get the latest Ruby release, but since you're here already, why not a link? :-)


GET LATEST RUBY HERE!
Note: At the time of this article that was the latest stable release. You may have to visit the site in case I'm a little outdated!



Now that you have the latest Ruby downloaded, let's extract it into a temporary directory:


# tar zxvf ruby-version.tar.gz

You then change to the directory where you extracted the source and run the following commands so you can compile and install Ruby . . .

# ./configure
# make
# sudo make install

Note: Remember the sudo requirements from above. Depending on the speed of your system, the last two commands may take a little while. I recommend a quick game on your Nintendo DS or grabbing a snack to eat at those points.

Are We There Yet????


If everything went well, you should be there. If you get error messages in your make or sudo make install commands, your best bet will be googling those errors. Remember to include the name of your distribution in the search path as well as the actual error message.


However, if you're one of the badass Linux users we all know, fear, and love, then you should be able to type in this and get the following similar output:


# ruby -v
=> ruby 1.8.5 (2006-08-25) [i686-linux]


Congratulation, you now have the latest version of Ruby installed on your system!

Should be easy coasting from here. Our next task is to install RubyGems, which is Ruby's own package manager, similar to Ubuntu's apt-get. You can find the latest gems version over at http://rubyforge.org under the RubyGems project's homepage, or you can just follow the link:



GET THE LATEST RUBYGEMS HERE! ! !
Note: This was the latest version at the time of this article, you may have to go to rubyforge.org if this article is dated.


Now that you have the source file, the installation will be similar to Ruby's - first, extract the package to a temporary directory:


# tar xzvf rubygems-version.tgz

Then change to the temporary directory and - nope! No make or make install here, you have Ruby now! Check this out:


# sudo ruby setup.rb

You should see the following similar results if all goes well:


=> Successfully built RubyGem
=> Name: sources
=> Version: 0.0.1
=> File: sources-0.0.1.gem

Now we verify that all went well:


# gem -v
=> 0.9.0

Booya!



Install Ruby On Rails

Now that we have RubyGems installed, let's wrap things up here and get Ruby On Rails to stop your salivating!


# sudo gem install rails --include-dependencies

After a brief wait as the gems package manager "phones home", you will see a nice little flurry of output letting you know that Rails has been successfully installed. Let's make sure!


# rails -v
=> Rails 1.1.6

Booya again! Aside from your database, you've got a vanilla installation of Rails, read to rock!



But it didn't work! What do I do now???
Nothing, all hope is lost! Just kidding - read over the installation steps and be sure you had all the required build dependencies. Make sure you have sudo working correctly. Once again, if all else fails, be sure to google those error messages, I'm sure your error might have been discovered by someone else and they could help!


MySQL, YourSQL, Everbody's SQL!


For you Ubuntu users out there, you're in luck. The current Ubuntu Edgy 6.10 release should have MySQL 5 in its package manager, giving you all you need to satisfy the database requirement for Rails. Simply enter the following command:


# sudo apt-get install mysql-server

If all goes well the server will be installed and then run as a background process. Let's verify:


# mysql -u root
=> Welcome to the MySQL monitor. Commands end \
=> with ; or \g
=> Your MySQL connection id is 8 to server \
=> version: 5.0.24a-Debian_9-log

=> Type 'help;' or '\h' for help. Type '\c' \
=> to clear the buffer.

=> mysql>

Great! Now let's make sure that Rails can take full advantage of your database!



Let's go for a drive!

You will need to install the MySQL database driver to communicate with Ruby. Prerequisites to this will be the developmental libraries for MySQL which we can find with the following command:


# apt-cache search libmysqlclient

Look for the latest mysql database client library. Since we have MySQL5, the correct version will be:


# sudo apt-get install libmysqlclient14-dev

Note: If you have MySQL4, then you would install libmysqlclient12-dev.



Last, let's install the driver by using gems:


# sudo gem install mysql
=> Successfully installed mysql-2.7


Give yourself a pat on the back, go get a beer, and call your friends over. You have a vanilla installation of Ruby On Rails, ready to rock all your web development needs. Hope this installation tutorial gets you going! Next time - introducing the world to your new Ruby / Rails installation!



If you are running Windows or MacOS, please go here to find out what steps are needed to get Rails onto your system!

No comments: