Exploring Vagrant: Starting with Virtual Box - Part 2
Tags: devops, ubuntu, virtualmachine, linux
Published: 2023-08-18 11:59:23

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](https://developer.hashicorp.com/vagrant/downloads?product_intent=vagrant).
3. Vagrant Boxes: Select your specific box from the [Vagrant Cloud](https://app.vagrantup.com/boxes/search)

**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. 

```bash
vagrant init
```

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

```bash
vagrant init box_name
```

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](https://developer.hashicorp.com/vagrant/docs/providers/virtualbox/configuration) 

```ruby
# -*- 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

```

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:

```bash 
vagrant up
```

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
```

and unsuccessful boxes are located at:

```
~/.vagrant.d/tmp
```

On Windows systems it is located under the Users folder:

```
C:\Users\%userprofile%\.vagrant.d\boxes
```

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:

```bash 
vagrant ssh
```

**Step 8: Stop and Destroy the Environment:**

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

```bash 
vagrant halt
```

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

```bash
vagrant destroy
```