Create your Virtual Instance with Vagrant by Hashicorp

mqzerr
5 min readFeb 8, 2021

Vagrant enables users to create and configure lightweight, reproducible, and portable development environments. Vagrant is an open-source software product for building and maintaining portable virtual software development environments; e.g., for Virtual Box, KVM, Hyper-V, Docker containers, VMware, and AWS. It tries to simplify the software configuration management of virtualization in order to increase development productivity.

Installation

Prerequisite

Before you learn vagrant you need to know some of the following below

  • Basic linux command
  • Virtualbox (virtual machine concept)

Installation

1. Install virtualbox

$ sudo apt update
$ sudo apt install virtualbox

2. Install Vagrant

$ sudo apt install vagrant
$ vagrant -v

Create new instance

$ mkdir vagrant-server
$ cd vagrant-server

2. Init your os instance

Syntax : $vagrant init [os name]

Ex: $ vagrant init ubuntu/bionic64

#List of Os images name
Link : https://app.vagrantup.com/ubuntu

3. Edit Vagrantfile

After you init directory to vagrant directory it will create a Vagrantfile which store the configuration.

$ nano Vagrantfile

You will see the config below

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

# All Vagrant configuration is done below. The “2” in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don’t change it unless you know what
# you’re doing.
Vagrant.configure(“2”) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.

# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = “ubuntu/bionic64”

# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing “localhost:8080” will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network “forwarded_port”, guest: 80, host: 8080

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network “forwarded_port”, guest: 80, host: 8080, host_ip: “127.0.0.1”

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network “private_network”, ip: “192.168.2.6”

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network “public_network”

# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder “../data”, “/vagrant_data”

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider “virtualbox” do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = “1024”
# end
#
# View the documentation for the provider you are using for more
# information on available options.

# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision “shell”, inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end

Un-comment this line and update your instance ip address

# config.vm.network “private_network”, ip: “192.168.2.6”

4. Up your instance running

$ vagrant up

It will shows like this

5. Ssh to your instance

It will create a default user vagrant for you to ssh to you instance so, you can use this command below to enter your instance

$ vagrant ssh

Finally, you can use this instance as192.168.2.6

6. Increase size of your instance

In case you want to do you just install this plug in vagrant plugin install vagrant-disksize and edit in Vagrantfile to

Vagrant.configure(‘2’) do |config|
config.vm.box = ‘ubuntu/bionic64’
config.disksize.size = ‘50GB’
end

Ref: https://github.com/sprotheroe/vagrant-disksize

Stop your instance

You can stop your instance by this command

$ vagrant halt

Reload your instance

If you update any configuration in Vagrantfile you just run the command below to reload configuration

$ vagrant reload

Destroy your instance

When you destroy your instance mean that you want build a new instance using the same configuration in Vagrantfile, anything in your instance will be destroy with new empty instance, everything will lose. If you want to destroy your instance you just execute the command below :

$ vagrant destroy

It shown you like this when you execute this command

➜ laravel-server vagrant destroy
default: Are you sure you want to destroy the ‘default’ VM? [y/N] y
==> default: Forcing shutdown of VM…
==> default: Destroying VM and associated drives…

If you want a more comprehensive view of Vagrant and all its available commands and features, please refer to the official Vagrant documentation.

Cheer !!! Thank you …. :)

I hope this post will be a part of your knowledge.

#sharingculture#startfromyou#cambodia#devops

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

mqzerr
mqzerr

Written by mqzerr

DevOps engineer at ABA Bank Cambodia — Be your own unique :)

No responses yet

Write a response