The Most Basic Things you need to know about Vagrant
DevOps, Vagrant

The Most Basic Things you need to know about Vagrant

Hi guys, if you’re starting maintain a project from youre customer that using Vagrant for setup enviroment, but you don’t know that. Or simply, you want learn this technonogy quickly, this article is for you. Let’s get started!

What the heck is Vagrant?

Define on Wikipedia:

Vagrant is an open-source software product for building and maintaining portable virtual software development environments, e.g. for VirtualBox, KVM, Hyper-V, Docker containers, VMware, and AWS. It tries to simplify the software configuration management of virtualizations in order to increase development productivity.

But if you’re feeling the define is too complex. Just simply understand that vagrant is a tool support you manage your Virtual Machines. End! :))

Prerequires before using Vagrant

Prerequires before you can start use vagrant, your computer was installed VirtualBox. You also use VMware, Hyper-V, Docker containers,… instead.

If you’re a beginner, I recommend you use VirtualBox instead of others. So, ensure you was installed Virtualbox and then, let’s install Vagrant and start explore it in below.

Vagrantfile

This is the most importance file in Vagrant. It contain everything you need to config for your VM. You need config network? or need config synced folder? or run several commands everytime vagrant up your vagrant? All you need to do is write everything you want into this file.

Let me show you, an basic Vagrantfile look like this:

Vagrant.configure("2") do |config|
  config.vm.box = "laravel/homestead"
  # write what config you want here
end

Now, let’s to see somethings you can write into this file.

Browse into VM with Vagrant Networking

When you working with VM, certainly you’ll must browse to your website is running in VM. To do this, you must config network for your VM, and here is 2 main types config you should know.

Vagrant Networking
Vagrant Networking

Forward Network

Assume that the guest machine is running a web server listening on port 80, you can make a forwarded port mapping to port 8080 (or anything) on your host machine.

Vagrant.configure("2") do |config|
  config.vm.box = "laravel/homestead"
  config.vm.network "forwarded_port", guest: 80, host: 8080
end

Then you can then open your browser to localhost:8080 and browse the website, while all actual network data is being sent to the guest.

Private Network

You can also specify a static IP address for the machine. This lets you access the Vagrant managed machine using a static, known IP. The Vagrantfile for a static IP looks like this:

Vagrant.configure("2") do |config|
  config.vm.box = "laravel/homestead"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "private_network", ip: "192.168.50.4"
end

When use this, you must make sure that the static IP doesn’t conflict with any other machines on the same network.

Set hostname for your Vagrant VM

This is bonus section. If you want to set a hostname in Vagrantfile, just use this config:

Vagrant.configure("2") do |config|
  config.vm.box = "laravel/homestead"
  config.vm.network "forwarded_port", guest: 80, host: 8080

  config.vm.hostname = "haminh.net" #set hostname here
end

Now, after you up your Vagrant, you can browse to http://haminh.net instead of IP andress you’re config in Vagrantfile.

In fact, Vagrant will set update /etc/hosts (with MacOS, Linux or it will be C:/Windows/system32/driver/etc/hosts if you’re using Windows) on the guest with the configed hostname.

Synced Folders in Vagrant

To synced folders from your host machine with the guest machine, just use config.vm.synced_folder in the Vagrantfile. For example:

Vagrant.configure("2") do |config|
  config.vm.box = "laravel/homestead"
  config.vm.network "forwarded_port", guest: 80, host: 8080

  config.vm.synced_folder ".", "/var/www" #synced folder
end

In above example, we’re config to synced folder contain Vagrantfile with /var/www folder in VM. You can also config sync to anywhere you want.

Automatically install software by use Vagrant

Run bash shell
Run bash shell

To do this, you’ll need to use one thing called “Vagrant Provisioning”. Provisioners allow you to automatically install software, alter configurations, and more on the VM as part of “vagrant up” process. Let me show you simplest way you can do it:

Vagrant.configure("2") do |config|
  config.vm.box = "laravel/homestead"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.synced_folder ".", "/var/www"
  
  config.vm.provision "shell", inline: "sudo service apache2 restart"
end

In our case, your VM will only run once ” sudo service apache2 restart ” command on the first time “vagrant up”. If you want to run it with vagrant reload or run on next times vagrant up, you can use –provision flag to do it. Likewise, you can also use –no-provision flag to skip provisioning.

How to config to run Once, Always or Never

By default, provisioners are only run once, during the first vagrant up since the last vagrant destroy, unless the --provision flag is set, as noted above.

Optionally, you can configure provisioners to run on every up or reload. They will only be not run if the --no-provision flag is explicitly specified. To do this set the run option to “always”, as shown below:

Vagrant.configure("2") do |config|
  config.vm.box = "laravel/homestead"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.synced_folder ".", "/var/www"
  
  config.vm.provision "shell", inline: "sudo service apache2 restart",
    run: "always" # Set run everytime "vagrant up" or "vagrant reload"
end

You can also set run: to "never" if you have an optional provisioner that you want to mention to the user in a “post up message” or that requires some other configuration before it is possible, then call this with vagrant provision --provision-with bootstrap.

Several Common Vagrant Commands

In process of working with Vagrant, you’ll need to know several common commands to work with vagrant in most cases.

Several Common Vagrant Commands
Several Common Vagrant Commands

Note: To run these commands, ensure that you’re cd to directory that The Vagrantfile is placed in.

$ vagrant init [name or url]

This command will create an initial Vagrantfile if one does not already exist. You can go to https://app.vagrantup.com/boxes/search to find Vagrant Boxes you want use.

$ vagrant up

This command will turn on your virtual machine and setup everything you was configed in Vagrantfile.

$ vagrant ssh

This command help you ssh to your VM.

$ vagrant halt

Reverse of “up” is “halt”. When you want to turn off your virtual machine. Above command will help you do that.

$ vagrant reload

Reload your VM

$ vagrant version

Tells you your version you have installed as well as latest vervion of Vagrant that currently available

$ vagrant destroy

This command stops is the running machine your Vagrant is managing and destroy all resource. You can use -f or --force flag to don’t ask confirm before destroying.

Conclusion

Those is the most basic things you need to know to start use Vagrant. With these knowledges, you can setup a simple development enviroment you want. I hope these useful for you. If you’re starting maintain a project and this post is not enough for you. Please search it in Google what you don’t know or refer this document. Thank for reading.