Create server in VirtualBox using a Vagrant file

Create server in VirtualBox using a Vagrant file

Before you can start using Vagrant with VirtualBox, make sure you have the following installed on your machine

Step 1: Install

Once both VirtualBox and Vagrant are installed, you can proceed with the following steps.

Step 2 : Create a New Directory for Your Vagrant Project

  1. Open a terminal or command prompt.
  2. Create a new directory where you want to store your Vagrant project and navigate into it. For example :

mkdir vagrant_project

cd vagrant_project

Step 3: Initialize the Vagrant Project

vagrant init

This will generate a Vagrantfile in your directory. The Vagrantfile is a configuration file that defines the VM and its environment settings.

Step 4: Configure the Vagrantfile for VirtualBox

Now you need to configure the Vagrantfile to use VirtualBox and set up your server.

Open the Vagrantfile in a text editor of your choice.

Modify the file to configure the settings for your virtual machine. Below is an example of a simple configuration


Vagrant.configure("2") do |config|

# Use an Ubuntu base box

config.vm.box = "ubuntu/bionic64"

# Set up the VM provider to use VirtualBox

config.vm.provider "virtualbox" do |vb|

vb.memory = "1024" # Set memory size (in MB)

vb.cpus = 2 # Set the number of CPUs

end

# Optional: Forward ports from the VM to the host machine (for accessing services like a web server)

config.vm.network "forwarded_port", guest: 80, host: 8080 # Forward port 80 from VM to port 8080 on host

end


  • config.vm.box: Defines the base box to use. In this case, it’s an Ubuntu box (ubuntu/bionic64).
  • config.vm.provider "virtualbox": Configures VirtualBox settings, including the amount of memory and number of CPUs.
  • config.vm.network: Forwards ports between the guest VM and your host machine. In this example, port 80 on the VM is forwarded to port 8080 on your host machine

Step 5 : Start and Provision the Virtual Machine

Once the Vagrantfile is set up, you can create the virtual machine by running the following command

vagrant up

This command will

  • Download the base box (if it’s not already downloaded).
  • Create and start a VirtualBox VM based on the configuration in the Vagrantfile.
  • Provision the VM with the default settings.

Step 6 : Access the Virtual Machine

After the VM has been created, you can access it via SSH using the following command

vagrant ssh

This will log you into the virtual machine, where you can perform any tasks you need (like setting up a server, installing software, etc.).

Step 8 : Stopping, Restarting, and Destroying the VM

  • To stop the virtual machine : vagrant halt
  • To restart the virtual machine : vagrant reload
  • To destroy the virtual machine (this deletes the VM completely) : vagrant destroy
  • To check VM status : vagrant status .


Aman Reddy

Research Scientist - IT | SAMEER - MeitY, Government of India | Software Developer 2+ Years of Experience

1 个月

Good article Apeksha Bansod

要查看或添加评论,请登录

Apeksha Bansod的更多文章

  • Configuring and preparing the VM for use

    Configuring and preparing the VM for use

    After creating a virtual machine (VM), the next steps involve configuring and preparing the VM for use. Here are the…

    2 条评论

社区洞察

其他会员也浏览了