How to use VirtualBox with Vagrant

In the previous post, we saw how to use homebrew to install VirtualBox & Vagrant. In this post, we’ll try to use vagrant to spin up virtual development environment using VirtualBox.

  • Create a folder in your user home:

$ mkdir -p project/vagrant
$ cd ~/project/vagrant
$ pwd
/Users/xxxxx/project/vagrant

  • To add a vagrant box, type the following command:

$ vagrant box add ubuntu/trusty64
==> box: Loading metadata for box ‘ubuntu/trusty64’
box: URL: https://vagrantcloud.com/ubuntu/trusty64
==> box: Adding box ‘ubuntu/trusty64’ (v1.0.0) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/hashicorp/boxes/trusty64/versions/1.0.0/providers/virtualbox.box
box: Progress: 5% (Rate: 100k/s, Estimated time remaining: 0:25:15)

Search here to find a box suitable for your need.

  • Once the box is successfully added, you can initialise the box:

$ vagrant init trusty64

It will generate a vagrantfile & .vagrant folder
$ ls -la
drwxr-xr-x 3 xxxxx staff 102 mmm 13 22:16 .vagrant
-rw-r–r–     1 xxxxx staff 3024 mmm 13 22:40 Vagrantfile

  • To fire up the box:

$ vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: This machine used to live in /Users/juhfa/projects/chef but it’s now at /Users/juhfa/projects.
==> default: Depending on your current provider you may need to change the name of
==> default: the machine to run it as a different machine.
==> default: Clearing any previously set forwarded ports…
==> default: Clearing any previously set network interfaces…
==> default: Preparing network interfaces based on configuration…
default: Adapter 1: nat
==> default: Forwarding ports…
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running ‘pre-boot’ VM customizations…
==> default: Booting VM…
==> default: Waiting for machine to boot. This may take a few minutes…

  • Once the machine is successfully started, you’ll be able to see it added in the VirtualBox GUI. By default, port 2222 of your local machine is used to connect to port 22 of the virtual machine
  • You can then ssh to the box using:

$vagrant ssh
Welcome to Ubuntu —–
———————–
vagrant@trusty64:~$

Advertisements

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