eddorre

Found 3 posts tagged with 'mac'

Using Ruby Enterprise Edition and Passenger on OS X with RVM

July 21, 2010 — 6 Comments

Ever since I installed Ruby 1.9.2-head with RVM (Ruby Version Manager) I’ve become a convert of using it to manage all of my Ruby installs.

Last week I decided to install Ruby Enterprise Edition on my development environment as my default Ruby install. My motivation for this was two-fold; better memory management and a comment by Laurent Sansonetti (one of the authors of MacRuby and works at Apple) on an article written by Robby Russell titled Installing Ruby on Rails, Passenger, PostgreSQL, MySQL, Oh My Zsh on Show Leopard, Fourth Edition.

The comment left by Laurent suggested not to rename the default Ruby install (and then symlinkling the Ruby installed via Ports) but to manage it by setting the load path.

Using RVM, we can install any Ruby version we want and have it set as the default Ruby instance easily. Let’s see how it’s done.

Note: The following documentation worked on my install of OS X. When encountering an error with the following instructions refer to the RVM documentation.

Installing the RVM Gem

There are multiple ways to install RVM, but I chose the gem install method (even if it is listed as not recommended in the documentation).


sudo gem install rvm

Installing RVM and Adding Hooks to Your Shell (bash, zsh, etc.)

Once the gem has been installed, you can now run the rvm-install command to add the hooks to your shell by running the following:


rvm-install

After RVM has been installed, you’ll be prompted to change something in your shell profile (the end of the install process will provide you with a code snippet). Since I’m using Oh My Zsh, and therefore the z shell (zsh), I opened up my .zshrc file located at /Users/carlos/.zshrc and pasted the code snippet at the bottom of the file.

Here is an example (the code snippet might be different depending on your shell and the version of RVM that was installed):


if [[ -s /Users/carlos/.rvm/scripts/rvm ]] ; then source /Users/carlos/.rvm/scripts/rvm ; fi

Updating RVM to the Latest Version

The gem version of RVM seems to be a little behind (this may be why it’s not recommended) so we’re going to update it. This updates RVM to the latest, greatest version.


rvm update --head

Installing Ruby Enterprise Edition and Dependencies

As the title of this post suggests, we’re going to be installing Ruby Enterprise Edition. According the web site, using Ruby Enterprise Edition has the potential to reduce Ruby memory consumption by 33% (on average) when used in combination with Phusion Passenger.

Before we can start installing Ruby Enterprise Edition we need to install Readline.


rvm package install readline

Once that’s done, we’re ready to get to the main event; installing Ruby Enterprise Edition.


rvm install ree -C --with-readline-dir=$HOME/.rvm/[yourusername]

Usually it will take some time for Ruby to be downloaded, compiled and properly installed with RubyGems support. When that’s done, you can switch over to your new RVM Ruby interpreter:


rvm use ree

Installing RubyCocoa for Use with the Passenger Preference Pane

Passenger is easiest to administer with the Passenger Preference Pane but it has some dependencies that we’ll have to install in order for it to work. If you don’t wish to manage virtual hosts with the Passenger Preference Pane, you can skip down to the next section.

Installing RubyCocoa

At the time of this writing, RubyCocoa 1.0.1 has been released but it’s broken at this time so we’ll have to rely on RubyCocoa 1.0.0.

Let’s download this from source and build it:


tar xzf RubyCocoa-1.0.0.tar.gz && rm RubyCocoa-1.0.0.tar.gz && cd RubyCocoa-1.0.0

ruby install.rb config --build-universal=yes
ruby install.rb setup
sudo ruby install.rb install

To make sure that RubyCocoa you’ll need to pop open an IRB session:


irb
require 'osx/cocoa'

If everything has gone well, you’ll see the shell return ‘true’ and you’re ready to install the Passenger Preference Pane

Installing and Configuring Passenger

Note: If you already had a development environment up and running, you’ll need to re-install your gems into the new RVM’d environment. When installing a gem into an RVM’d environment, do not prepend the command with sudo.

Before we can install Passenger, we’ll need to generate some wrapper scripts. This is done with the following:


rvm ree --passenger

Since the Ruby Enterprise Edition suggests using it with Passenger, we’ll go ahead and install the gem now.


gem install passenger

Once the gem has been installed, we need to to make sure that Apache is running; open up the System Preferences and then the Sharing applet. Make sure that Web Sharing is checked. Doing so will start the Apache web server.

Since Apache has been installed we need to have Passenger and Apache be friends. Start off by adding the Passenger module to Apache:


rvmsudo passenger-install-apache2-module

Towards the end of the installation process, you’ll be prompted to copy some information to put into your Passenger configuration file. There is a catch though, the Passenger install has no idea that you’re using RVM so the last two lines have inaccurate information (they are most likely referencing your system Ruby instance). The fix for this is easy though, you’ll just have to change the references to point to your RVM’d instance instead.

Here is an example from my install (obviously you’ll want to replace [yourusername] with your actual username:


LoadModule passenger_module /Users/[yourusername]/.rvm/gems/ree-1.8.7-2010.01/gems/passenger-2.2.11/ext/apache2/mod_passenger.so
PassengerRoot /Users/[yourusername]/.rvm/gems/ree-1.8.7-2010.01/gems/passenger-2.2.11
PassengerRuby /Users/[yourusername]/.rvm/bin/passenger_ruby

As mentioned, the above code snippet needs to be added to a passenger configuration file which I normally put in /private/etc/apache2/other and I call the file passenger.conf. I should note that this directory is owned by root, so when you create the file and save it you’ll have to provide your system password in order to make the changes.

Here is a sample of my configuration file:


LoadModule passenger_module /Users/carlos/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.14/ext/apache2/mod_passenger.so
PassengerRoot /Users/carlos/.rvm/gems/ree-1.8.7-2010.02/gems/passenger-2.2.14
PassengerRuby /Users/carlos/.rvm/bin/passenger_ruby

#Set the default environment to development
RailsEnv development

# Which directory do you want Apache to be able to look into for projects?
<Directory "/Users/carlos/work">
	Order allow,deny
	Allow from all
</Directory>

Finishing Up

If you’d like to make Ruby Enterprise Edition your default Ruby Interpreter, just use the following command:


rvm ree --default

Once Passenger has been configured, all that’s left to do is restart Apache. You can restart Apache by going to the System Preferences Pane and selecting the Sharing applet. Uncheck and re-check Web Sharing.

You should now be able to add Rails and Rack applications with the Passenger Preference Pane. Note: When opening the Passenger Preference Pane, you might see this warning:

Passenger Preference Pane 32bit Warning

This is normal if you have a 64-bit machine. Since RubyCocoa is 32-bit, it just has to relaunch the preference pane.

RVM is a fast moving target and as such these installation instructions may be out of date by the time you read them. I try to update my older posts when I get new information, but if you stumble across something that doesn’t work or if you know of a better way of doing something, please let me know.

Navigating the Command Line in Terminal.app

December 21, 2008 — 0 Comments

I recently switched back to using Apple’s built in Terminal.app (I was using iTerm previously) program for all of my command line needs. There are tons of keyboard shortcuts when you’re using bash but there are a few that are especially handy to have memorized.

  • CTRL-A: Goes to the beginning of the line
  • CTRL-E: Goes to the end of the line

Sometimes you want to get to the middle of the line instead of heading to the beginning or to the end. These will help:

  • OPTION/ALT-B: Jumps cursor back
  • OPTION/ALT-F: Jumps cursor forward

There is one caveat though. The first time I tried the OPTION key trick, the Terminal would just type in strange characters. In order to get Terminal to recognize the OPTION/ALT key as a command key you have to check the option for “Use option as meta key” in the preferences pane of the Terminal app. Once that is done, the Option key shortcuts will work.

Terminal Preferences

Thanks to Alex (aka @demonbane) for showing me this trick.

There are ton of shortcuts for the bash shell but these are among my favorites/most used.

Using SSH Keys for Remote Logins

March 27, 2008 — 0 Comments

When I had Windows servers at home, I would use the Remote Desktop tool to remotely login and manage everything from my Windows workstation.

Now that all of my servers have been converted over to Linux, I use the time honored tradition of using SSH to login and manage everything from my Macs (iMac and MacBook). Although with 5 Linux servers, it gets quite tiring typing in your password to authenticate each and every time I want to connect. Above and beyond that, password-less SSH logins come in handy when you have backup scripts running or when you’re deploying applications using Capistrano.

You can avoid pitfall by using SSH keys that are generated on your workstation and then stored on your servers.

I should note that this method doesn’t make your systems more secure, in fact it makes security weaker. If someone were to get a hold of your private keys, they could automatically login to any servers that use this method! Use at your own risk.

As I’ve said, I’m using my Mac as a client (specifically OS X Leopard) so these instructions are written with that client in mind. With that being said, I’m sure that the instructions can easily be ported to Linux or *BSD with minimal effort.

Open up a terminal window and type in “ssh-keygen -t [rsa|dsa]”. You can choose either RSA encryption or DSA encryption. As to which one is better, that’s open for debate. For my application, either one is equally suitable. One thing seems to be certain in my cursory research; RSA key generation is slower that DSA, but RSA is faster when verifying. For the rest of the example, I’ll be using an RSA key pair.

Once you run the ssh-keygen command, it will start generating a public and private key pair. You’ll be prompted to save the private key, I would choose the default which would normally be something like this: /Users/[username]/.ssh/id_rsa.

After choosing the directory where the private key is stored, you’ll be prompted for a passphrase. This is the passphrase for the private key and as such you’ll want to choose a strong passphrase. Something like, “I wouldn’t vote for Hillary for all of the tea in China!” Again, make this as strong as possible and DO NOT FORGET IT!

Once that has been completed, you should have two files in the directory that you chose earlier; a public key (id_rsa.pub) and a private key (id_rsa).

This public key now has to be copied to the server. You can do this with one command: "cat .ssh/id_rsa.pub | ssh [username]@[servername] “cat >> .ssh/authorized_keys”. Since the public key hasn’t been copied, you’ll have to enter in your password for the remote server. This should be the last time that you’ll need to do that if everything has gone well.

One last thing, if you want a deeper understanding of encryption, decryption and public key cryptography the book, Cryptography Decrypted, is an excellent resource.