Setting Up a New Vagrant Box

Vagrant is free and open-source software for creating and configuring virtual development environments. To set up a new vagrant instance just make a new directory to keep your virtual machine in and change into that directory:

Once you’re in the directory where you’d like to create your virtual machine, initiate the vagrant instance:

The initiation process will create a file called Vagrantfile in the directory. This is your configuration file for your vagrant instance. Edit the file and replace it with the following:

If you prefer to set up a 32-bit install of Ubuntu Precise, you can use the URL instead.

Next you’ll need to set up the file, a simple Bash script used for setting up new packages and running any Linux commands you’d like to run each time you provision the vagrant instance. Create a file and enter the following:

Developer Scripts

Before we can start up vagrant we’ll need to set up a couple of extra bash scripts we referenced in the above configuration. First, create a file called addwebsite and enter the following contents:

The addwebsite command uses a skeleton file to create new virtual hosts, so create this file as well:

Optionally, you may also want to create a couple of scripts to make the command to create a new database and import a sql file simpler.

Create the /usr/local/bin/adddb file:

Create the /usr/local/bin/importdb file:

The above scripts provide new commands for common developer actions:

Vagrant Commands

We’re now ready to start up our vagrant instance:

The first time a vagrant instance is started it will be provisioned. You can provision your vagrant instance, which will run the script again, using the following command:

To SSH into the vagrant instance, use the ssh command:

When you’re ready to stop the vagrant VM, you can issue the following command:

One interesting feature of vagrant is you can optionally remove the virtual machine entirely from disk space until you next run vagrant up again. To delete the vagrant VM issue the following command:

As you can see, with a little work up front vagrant can save a lot of time in the future. See the official vagrant documentation and tutorials for more information and configuration options. Please be sure and share this tutorial if you found it useful.