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!

Saturday, December 16, 2006

Great Time To Ride the Rails!

Believe it or not, my Rails journey started with a Linux Auditor Live DVD. On that DVD was the open-source exploit framework Metasploit which was written in Perl at the time. Curious, I visited the site, read through the documentation, and found out that the whole project was being redone in Ruby.

What struck me about the developer's documentation was how easy it was to read. The code read pretty much like a book and I could visually and conceptually link everything together in my mind.

Ruby code sure was clear.

So then I decided to go browsing at my local Barnes & Nobles for some books to further fuel the fire. After flipping through the very limited selection (at the time) of Ruby books, I came home with Agile Web Development with Rails and Ruby For Rails. What followed afterwards was a blur.

Literally within the same day I had a database-backed web site where I could create a blog or shopping cart and keep orders and transactions. Even though they were both VERY rough and in development mode, it said a lot for someone like me who had mostly front end web design experience. The process got me psyched and made me want to learn more Ruby and Rails.



Ruby For You (and me)!


Since then I've participated in and joined the NY Ruby Brigade and I'm also very proud to be a part of the New Jersey Ruby Group. After much coding and researching and learning and studying, I've realized something . . .

My mind is having trouble keeping up with my passion for learning everything (as corny as that sounds)! Quite literally, I think the biggest problem I'm having right now is that there aren't enough hours in the day. Plus, for some weird reason, we have to sleep.

Well, being that I'm no superman, I obviously forget. Or, there are times while I'm coding that I just can't seem to recall a certain method, or regex, or what relationship to put in my models.



Mission Statement


This [blog] will be my savior. My martial arts instructor used to tell us to keep a fighter's journal of all the techniques and philosophy we absorb during our time training, because there is such a wealth of information to be absorbed. The same applies to learning Ruby, and that is where the intention of all the future blog articles will lie:

This blog is mainly for you and me. For me, it's a record of all my do's and don'ts, heartaches and joys of learning and programming in Ruby. For you, it's a chance for any Ruby or Rails or even coding newbie to join forces with me, still a relative newbie, in the fast growing world of Ruby and Rails development.

For many of you, you have just heard of this Rails thing. Or maybe you are one of the few like me who had just stumbled upon some Ruby code and would like to see some more. Let me tell you something - it's an awesome time to be a Ruby n00b.

I will make it my job to make sure your journey through learning Ruby will not only be one of the most rewarding experiences but also fun.

Welcome to the world of Ruby / Rails n00bness, we're all in this together!!!

Feel free to comment me with questions. I frequent the #nyc.rb, #rubyonrails, and #ruby-lang channels on irc.freenode.net under the nickname hookET. Looking forward to hearing from y'all!

Next up . . . getting a vanilla installation of Ruby on Rails onto your system!