What is Vagrant ?
Vagrant is an open source tool that helps in building and managing virtual environments. In a software development company, it is always difficult and requires constant monitoring in the maintenance of virtual environments. This is where Vagrant comes to your rescue as you can set up a virtual environment in minutes to mimic your production environment for the testing the latest release of code. Vagrant uses Virtualbox to create and maintain virtual environments. Even though there are a lot of virtual software like Docker, VMWare etc. Vagrant has gone a step ahead to provide an easy way to create and reproduce multiple virtual machines within minutes. It is developed using Ruby and supports Windows, Mac, Linux and more.
Why You Need Vagrant ?
Vagrant may play a vital role in a software development company with huge number of systems, where software installation and automation is a huge task. Also, often a virtual environment is provided on the delivery side to mimic production environments, but if a virtual environment is provided on the development side itself, it can provide a lot of control to the developers. It increase the productivity levels to a great extent. It is useful to all kinds of people in the company, as for developers, it allows them to have local environment to mimic the production environment in terms of the same OS, same libraries and processes and all other details, so you can test all your code in the development environment itself, before pushing it to production.
For system administrators, Vagrant can be boon as they provide them with a disposable environment, where they can test all their infrastructure management scripts. Before deploying your scripts on remote clouds like AWS or Rackspace, they can test shell scripts, Puppet modules and then test it on the cloud using the same workflow used earlier.
How to Install Vagrant on Ubuntu Linux?
Vagrant can be easily setup in the Ubuntu Linux environment. With Vagrant on board, you can greatly reduce the difficulties and complexities in setting up virtual environments. With the same basic configuration, multiple virtual environment can be deployed in seconds.
To setup Vagrant, all you need is a dedicated server to gain complete control over the server and also an SSH client like Putty.
Install the Virtualbox and dkms:
Virtualbox is used by Vagrant to manage the virtualization and hence make sure to install Virtualbox before Vagrant is installed.The dkms package helps in making sure that the Virtualbox host kernel versions are updated when the Linux kernel versions gets updated.
linuxbuzz@nixworld:~$ sudo apt-get install virtualbox virtualbox-dkms -y
Now install Vagrant in your ubuntu box using the following command
linuxbuzz@nixworld:~$ sudo apt-get install vagrant -y
Deploying the Vagrant system:
Vagrant can deploy multiple development environments in a jiffy. To download the vagrant image (Ubuntu 16.04 LTS), use the following command, that will download and install xenial64 box from the website of vagrant. This xenial64 box is a completely packaged image that helps in provisioning Ubuntu 16.04 VM instances
linuxbuzz@nixworld:~$ vagrant box add ubuntu/xenial64
Above command will download Ubuntu 16.04 LTS vagrant box under the “.vagrant.d/boxes/” in your current working directory. This vagrant box will be globally available for all the projects for currently logged in users, In my case ‘linuxbuzz’ is logged in user.
Configure the Vagrant Project:
Now make a directory for your vagrant project, Let’s assume i want to test ubuntu 16.04 vagrant box for my testenv
linuxbuzz@nixworld:~$ mkdir vagrant_testenv linuxbuzz@nixworld:~$ cd vagrant_testenv
Now create a vagrant file that will be the primary file that helps in controlling the project configuration.
linuxbuzz@nixworld:~/vagrant_testenv$ vagrant init
Above command will create a file with name “Vagrantfile” in your project folder.
Edit Vagrantfile and make the following Changes.
Vagrant.configure(2) do |config| config.vm.box = "ubuntu/xenial64" config.vm.provider "virtualbox" do |vb| vb.memory = "1024" end
Start the Vagrant Environment:
linuxbuzz@nixworld:~/vagrant_testenv$ vagrant up
And you’ve successfully downloaded and installed Vagrant with Virtual Box in an Ubuntu Environment. With Vagrant at your disposal, you can easily create and manage multiple virtual environments with just a single command.
When we run ‘vagrant up’ then it will automatically create one virtual machines bases on parameters specified in Vagrant file.
Status of a Virtual Machine
The status command provides the status of the virtual machine
linuxbuzz@nixworld:~/vagrant_testenv$ vagrant status
SSH Vagrant Provisioned Virtual Machine
To get the ssh console run ‘vagrant ssh’ command
linuxbuzz@nixworld:~/vagrant_testenv$ vagrant ssh
Shutdown of a Virtual Machine
The halt command shuts down or halts a virtual machine
linuxbuzz@nixworld:~/vagrant_testenv$ vagrant halt ==> default: Attempting graceful shutdown of VM... linuxbuzz@nixworld:~/vagrant_testenv$
Destroy of a Virtual Machine
If you’ve setup a virtual environment just for testing some scripts, you can destroy the virtual machine after all your testing is completed using the destroy command
linuxbuzz@nixworld:~/vagrant_testenv$ vagrant destroy default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Destroying VM and associated drives... linuxbuzz@nixworld:~/vagrant_testenv$
That’s all from this Vagrant guide, Please share your feedback and comments