VirtualBox, Vagrant and Mac OS X: instant linux development environment

Vagrant is a Ruby project that lets you set up a Linux virtual machine very quickly, using VirtualBox. Ideally, at least.  Getting it set up the first time on a MacBook can be tricky.  Here’s how I got it running.

This assumes you’re running Vagrant in your home directory.  Also, I already had VirtualBox installed.

Updated 5/30/2011: The previous instructions added ~/.rvm/scripts/rvm to ~/.bash_profile, which caused RVM’s settings to override anything in your ~/.profile.  See “Gotchas” for more info.

1) Install RVM (trust me here, Mac OS X’s built-in Ruby Gems doesn’t cut it, and using MacPorts in this case will cause you grief — see the “Gotchas” as the end for more).

$ bash < /tmp/.profile && mv /tmp/.profile ~/.profile
$ source .bash_profile
$ rvm use 1.9.2 --default

2) Install Vagrant.

$ gem install vagrant
$ vagrant box add base http://files.vagrantup.com/lucid32.box
$ vagrant init

Now you need to configure Vagrant to use a different name for your dotfile:

$ vim Vagrantfile

Add the following line anywhere in the config:
config.vagrant.dotfile_name = ".vagrantf"
Save, quit

$ vagrant up

3) Done!

Ta da! This will show you a new VM running:

$ vboxmanage list runningvms
"gabrielk_1306348374" {75a25ecf-519a-46bc-b4fc-53fd1075464e}

Take the part in quotes to get your IP address:

$ VBoxManage guestproperty enumerate "gabrielk_1306348374" | grep "V4/IP" | cut -d"," -f2 | cut -d":" -f2 | tr -d " "

Set up your ssh config file so that you can use something like Transmit to SCP your files:

$ vagrant ssh-config >> ~/.ssh/config

Now you can set up your SCP/SFTP client to use the host “vagrant” (no username/password needed) to transfer files.

And you can ssh into your vagrant machine using either the “vagrant ssh” command or the more-familiar “ssh vagrant” command:

Gotchas

The steps at the beginning of this post got me around all these issues.

The version of Ruby Gems on my computer is too low to install Vagrant

$ gem install vagrant
ERROR: Error installing vagrant:
vagrant requires RubyGems version >= 1.3.6

Solution: Use RVM to install Ruby and Gems.

After installing MacPorts version of Ruby and Ruby Gems I was able to install Vagrant, but when trying to add a box I got deprecated function notices and “cross-thread violation on rb_gc() (null) Abort trap” error

$ vagrant box add lucid32 http://files.vagrantup.com/lucid32.box
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /opt/local/lib/ruby/gems/1.8/specifications/rubygems-update-1.7.2.gemspec:11.
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/gabrielk/.gem/ruby/1.8/specifications/archive-tar-minitar-0.5.2.gemspec:10.
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/gabrielk/.gem/ruby/1.8/specifications/erubis-2.6.6.gemspec:10.
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/gabrielk/.gem/ruby/1.8/specifications/json-1.5.1.gemspec:10.
NOTE: Gem::Specification#default_executable= is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem::Specification#default_executable= called from /Users/gabrielk/.gem/ruby/1.8/specifications/rake-0.8.7.gemspec:10.
[BUG] cross-thread violation on rb_gc()
(null)

Abort trap

Solution: Use RVM to install Ruby and Gems.

After removing the MacPorts version of Ruby and switching to RVM, Vagrant wouldn’t let me start the VM

$ vagrant up
The local file Vagrant uses to store data ".vagrant" already exists
and is a directory! If you are in your home directory, then please run
this command in another directory. If you aren't in a home directory,
then please rename ".vagrant" to something else, or configure Vagrant
to use another filename by modifying `config.vagrant.dotfile_name`.

Solution: Add the following line anywhere in the Vagrantfile (located wherever you ran vagrant init):
config.vagrant.dotfile_name = ".vagrantf"
“.vagrantf” can be anything, as long as it’s not “.vagrant”.

MacPorts stopped working, and none of the aliases, functions or paths in my ~/.profile are working.

Don’t use RVM’s suggested command for adding ~/.rvm/scripts/rvm to your .bash_profile file, since they’ll override anything you’ve already got in ~/.profile.  Instead use this:

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' | cat - ~/.profile > /tmp/.profile && mv /tmp/.profile ~/.profile

Bash will only load one profile script, and it checks them in order; so if you have a ~/.bash_profile script (with content — it will ignore an empty one) it will not read your ~/.profile script. ~/.bash_profile is apparently the normal place to put your profile scripts, but I’m in the habit of using ~/.profile since that’s where MacPorts decided to put its stuff and I just kept adding to it.

Enhanced by Zemanta
Advertisements

2 thoughts on “VirtualBox, Vagrant and Mac OS X: instant linux development environment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s