DEV Community

Cover image for Exploring Vagrant: Starting with Virtual Box - Part 2
Syam SV
Syam SV

Posted on

Exploring Vagrant: Starting with Virtual Box - Part 2

Starting a Vagrant project from scratch involves a series of steps to set up a new development environment using Vagrant. Below is a step-by-step guide to help you start a Vagrant project:

Step 1: Install Required Software:

Before you begin, make sure you have the following software installed on your system:

  1. Virtualization software (VirtualBox)
  2. Vagrant: Download and install it from the Vagrant website.
  3. Vagrant Boxes: Select your specific box from the Vagrant Cloud

Step 2: Create a Project Directory:

Create a new directory where you want to set up your Vagrant project. You can name it whatever you prefer.

Step 3: Initialize the Vagrant Project:

Navigate to the project directory in your terminal and run the following command to initialize a new Vagrant project.

vagrant init
Enter fullscreen mode Exit fullscreen mode

To Start with a prefigured Vagrantfile following command for a specific box

vagrant init box_name
Enter fullscreen mode Exit fullscreen mode

Replace box_name with the name of the base box you want to use (e.g., hashicorp/bionic64 for an Ubuntu-based box).

This will create a Vagrantfile in your project directory.

Step 4: Configure the Vagrantfile:

Open the generated Vagrantfile in a text editor. Customize it according to your project's needs. Here's a simple example of a Vagrantfile that sets up a VirtualBox-based environment:

To write your own VirtualBox Vagrantfile configurations , please refer to the instructions provided here

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = 2048
    vb.cpus = 2
    vb.gui = true
  end 
end

Enter fullscreen mode Exit fullscreen mode

In this example, you specify the base box (ubuntu/jammy64), set some VirtualBox-specific settings.

Step 5: Start the Vagrant Environment:

In your terminal, navigate to the project directory and run the following command to start the Vagrant environment:

vagrant up
Enter fullscreen mode Exit fullscreen mode

This command creates and provisions the virtual machine according to the settings in your Vagrantfile. If this is the first time you're using a particular base box, Vagrant will download it.

On Mac/Linux System, the successfully downloaded boxes are located at:

~/.vagrant.d/boxes
Enter fullscreen mode Exit fullscreen mode

and unsuccessful boxes are located at:

~/.vagrant.d/tmp
Enter fullscreen mode Exit fullscreen mode

On Windows systems it is located under the Users folder:

C:\Users\%userprofile%\.vagrant.d\boxes
Enter fullscreen mode Exit fullscreen mode

Use vboxmanage list vms to list all the virtual machines

Step 6 : Access the Virtual Machine:

You can SSH into the virtual machine using the following command:

vagrant ssh
Enter fullscreen mode Exit fullscreen mode

Step 8: Stop and Destroy the Environment:

When you're done with the virtual machine, you can halt it using:

vagrant halt
Enter fullscreen mode Exit fullscreen mode

To completely remove the virtual machine and its resources, use:

vagrant destroy
Enter fullscreen mode Exit fullscreen mode

Top comments (0)