Building vagrant boxes

Building vagrant boxes

- 6 mins

How to Create and Share a Vagrant Box on Vagrant Cloud

Vagrant is a powerful tool for creating reproducible dev environments, and sharing your custom Vagrant boxes on Vagrant Cloud makes it easy for other people to use them. Many times there are popular boxes, like ubuntu or red hat boxes, that are hard to find or disappear. In this post I want to walk through creating a Vagrant box from a VirtualBox vm.

Prerequisites


Create a Vagrant Box

Set Up the Base vm

Start by creating a vm in VirtualBox:

Configure the VM for Vagrant

Log into the VM and configure it to meet Vagrant’s requirements:

Package the VM into a Vagrant Box

On your host machine:

Test the Box Locally (kinda Optional but worth checking)

To ensure the box works:


Share the Box on Vagrant Cloud

Create a Vagrant Cloud Account

Install the Vagrant Cloud CLI

Install the Vagrant Cloud plugin:

vagrant plugin install vagrant-cloud

Authenticate with Vagrant Cloud

Log in to Vagrant Cloud from the CLI:

vagrant cloud auth login

Enter your username and password or an access token (generate one from your Vagrant Cloud account settings).

Create a Version for Your Box

Create a version for your box:

vagrant cloud version create yourusername/ubuntu2004 1.0.0

Create a Provider

Add a VirtualBox provider for the version:

vagrant cloud provider create yourusername/ubuntu2004 virtualbox 1.0.0

Upload the Box File

Upload the box file to Vagrant Cloud:

vagrant cloud provider upload yourusername/ubuntu2004 virtualbox 1.0.0 ~/vagrant-boxes/ubuntu.box

Publish the Box

Release the version to make it available:

vagrant cloud version release yourusername/ubuntu2004 1.0.0

Your box is now live on Vagrant Cloud. Test and share the box now.

Test the Box from Vagrant Cloud

Test the box by initializing it from Vagrant Cloud:

Create a new Vagrantfile in a new file:

   Vagrant.configure("2") do |config|
     config.vm.box = "yourusername/ubuntu2004"
     config.vm.box_version = "1.0.0"
   end

Run:

   vagrant up
   vagrant ssh

Clean up and delete:

   vagrant destroy -f

Conclusion

Creating and sharing a Vagrant box on Vagrant Cloud is straightforward once you understand the process. By following these steps, you can package a custom VM and make it available for others to use in their dev workflows. Whether you’re sharing a public box with the community or a private box with your team, Vagrant Cloud simplifies distribution.

For more advanced setups, consider using Packer to automate box creation. Happy Vagrant-ing!

Resources and Troubleshooting