A vagrant is a common tool for virtual machines management combined with a simple command line interface. The tool is expected to make the entire process easy without polluting the main installation with tons of configuration files. In the backend, there is one Virtual Box utilized by the Vagrant tool to run virtual machines. A Vagrant box is just another term for virtual machines handled specifically by this utility. Vagrant supports Hyper-V, Docker, Virtual Box, and this tool can manage other machines like Amazon EC2, VMWare, etc., as well.
In this blog, we would cover Vagrant in detail, along with its installation, features, benefits, virtual boxes, setting up the environment, etc. Learning Vagrant is not rocket science, but you should start from basics. Either you are a fresher or a rocket scientist, learning basics will save your years of frustration, misery, and pain. Also, it helps in building real-world projects quickly that dazzles potential employers.
For programmers, especially in the Python domain, “pip” and “virtual” are two popular commands that work perfectly to get all the stuff done. Let us understand these two commands in detail below.
For programmers outside the Python world, “pip” is a package manager in the Python world allows you to install modules and libraries super easily. Basically, it allows you ethically steal the code and get infinite power.
Pip install django
With this simple command, now you are ready to use Django and create Django apps.
Pip install flask
With this simple command, now you are ready to use Flask and create Flask apps. Yeah, it is that much easy! You must have the clear idea of “pip” command and how it is useful. The best part is that 99% you won’t face any problem in installing libraries or packages. There may be some issues if you are using Windows, so try avoiding Windows operating system in that case. Buy MAC operating system or get a Virtual Machine installed as fast as possible. Luckily, Vagrant can save your $1500 here, if you know how to use it perfectly.
“Virtualenv” command allows you to create a virtual environment. For example, Django project does not need to know about the Flask project. SO, you should create a separate virtual environment for both platforms first.
Let us go a little deep now and learn how Vagrant can help in overcoming real-life challenges. Before moving ahead, take a quiz, and evaluate your existing skills now.
Now you must be convinced enough How Vagrant has made the life of developers easy and more robust. Let us go little technical now and learn to install Vagrant and Virtual box on your favorite operating system. If you are not sure what to learn as a Vagrant user, check the syllabus first, before moving ahead.
Sometimes, Vagrant packages are already available in the system upstream packages repo. You should not install Vagrant in this manner. Here, updated files are missing, and it generally includes an older version of the tool. The best idea is to go to the download page by accessing the link given earlier and install the latest package that is highly in demand and suitable for today.
In the next section, let us see how to avail virtual boxes:
In Vagrant, standard templates of virtual machines are named as boxes. You may check the public list of vagrant boxes here - https://app.vagrantup.com/boxes/search
Vagrant boxes are generally provider-specific, and not all boxes are available for all providers. You should sort out the search by provider name so that you may find the desired box quickly. Once you find the right box, create a directory to store the project. You can use the following command for this purpose.
$ mkdir –p vagrant –projects/tutorial $ cd vagrant-project/tutorial
Now initialize the new Vagrant environment within the project’s directory by typing the following command:
$ vagrant init Ubuntu/xenia164
Here “init” command will create a “Vagrantfile” in the current directory, and the name of the box is given in the end that you want to initialize. Let us start the Vagrant Environment in the next step.
To start the Vagrant virtual environment, type:
$ vagrant up
This command creates, configures, and starts the virtual machine based on the Vagrant file. When the command is run, it will automatically download the needed Vagrant box from the box repository and performs the provisioning.
To SSH any running Vagrant machine or to access the shell, you may use the following command:
$ vagrant ssh
To suspend the development, or to stop the virtual machines, just suspend it with the following command given below:
$ vagrant halt
To suspend a virtual machine, you can write:
$ Vagrant suspend
If you want to clean up the development environment, you should destroy the machine. Here is the command to stop and destroy the Vagrant machine:
$ vagrant destroy
When you need a clear Vagrant environment based on a standard template, just type:
$ vagrant up
Accessing Ubuntu Vagrant box is not rocket science, but you should use the following commands here:
Mkdir folder_of_awesomeness Cd folder_of_awesomeness Vagrant init bento/Ubuntu-16.04 Vagrant up Vagrant ssh
These commands will simply enable you accessing shiny new Ubuntu Vagrant box. In a few cases, you may get the “Timeout” error. But don’t panic, you just have to upgrade the Virtual Box in that case. If you are a Windows User, then you should use a different command to SSH:
Protocol: SSH Hostname: 127.0.0.1 Port 2222
Boom!! You just have entered a completely new and different virtual world. Are you still confused, what is so special about it? If yes, let us take the discussion ahead.
With a virtual screen, you are in a completely different world. As a Windows user, if you think you are on Windows, you are not anymore. So, just saved $1500, isn’t it splendid. Command line is more powerful when compared to User Interfaces, and it allows you to hack the ultimate setups with three simple lines of code. With Vagrant, you may get entire setups with few lines of code.
One more thing cool about the Vagrant is that it helps in stealing another virtual box using Vagrant and finally follow the advanced image recognition without installing all modules and libraries on your system. In this way, Vagrant allows sharing the virtual environment and box with anyone else in the world without installing extra things.
If you face any problem in getting tools to work on Windows or MAC, then ethically steal boxes and get all the things you want working. Even if you want something more complicated like a Postgres database, then it is super easy with a simple command: sudo apt-get install postgresql
If you want to brush up the basic commands first before getting deep into the concept, let us break it down here.
To create a directory, use
To change a directory, use
To create a Vagrant file in your directory, type:
vagrant init bento/Ubuntu-16.04
To list all the directories, Type
To see the content of the file, you should use the following command:
To run and install Ubuntu image automatically from the web, you can use the following command:
config.vm.box = ”bento/Ubuntu-16.04”
To run the Vagrant file and start the virtual Vagrant environment, use the following command:
To SSH the Vagrant box, use the following command:
SSH sound boring sometimes, instead you can use Hack or takeover words here. These words are suitable here because it happens when you SSH into something. With a simple command, you take control of another system.
With Vagrant, you live in a completely fake environment, a virtual box that you may share with anyone. Let us go to it.
You put all the stuff in the /vagrant folder, but it can be seen on the local computer where your Vagrant file is. Not, it is the time to edit your code with the help of an editor on your favorite operating system. Just run the code from the virtual box and SSH to the command line.
Touch /vagrant/new_python.py Exit Is
Here, touch is the command to create a file quickly, and new-python is the freshly generated file. Obviously, this command will not work on Windows, but it is good for command line interface only. If you are in a vagrant box, it will work. WOW, you are done again.
With exit command, you can come back to the local computer again. The “ls” command is used to list all files on your local computer. You can use the “dir” command if you are on a Windows command line instead of Linux command line. If you find the file, you can open it on GUI as well and edit the content as required. Save the file, SSH to the box to run the code.
Tip: SSH the device to multiple systems together, open the file, and edit the content into your favorite editor and save it. It will save a lot of time, and you may focus on other things now.
Once you are sure of basic commands in Vagrant and how to install Vagrant and Virtual boxes. This is the time getting started with Vagrant and set up your first project. For this purpose, you need a Vagrant file. The purpose of the Vagrant file is two-fold, as discussed below.
Here are a few commands that will allow you to set up a Vagrant project:
$ mkdir vagrant_getting_started $cd vagrant_getting_started $ vagrant init hashicorp/precise64
It will create a Vagrant file in your directory. Observe the file closely that is full of comments and examples. Don’t be scared if there is something wrong; we will fix it soon. You can also use these commands for an existing project and use a pre-existing directory in that case. The vagrant file is committed to version control for the project. In this way, almost everyone can take benefits from the Vagrant without any upfront work.
Now, when you know how to set up a project in Vagrant, this is just the right time to set up a virtual box in Vagrant. Vagrant does not use a virtual machine from scratch that could be a tedious and time-consuming process. Vagrant uses a base image to clone a virtual machine which is named as “boxes” in Vagrant. Boxes are used to set up the virtual environment, once the Vagrant file is generated.
You already know how to install a virtual box as we have discussed in basics already. So, we will not repeat those steps again here. However, this section is worth reading to know more about virtual boxes. To add a new virtual box to the Vagrant environment, you should use the following command.
$vagrannt box add hashicorp/precise64
This command adds a new virtual box with the name specified in the command. Now it can be used by multiple Vagrant environments in future when required. It is easy to add boxes from the local computer or custom URL, etc. Boxes are stored globally so that they can be accessed by different Vagrant when necessary.
Each user can clone the existing box and use it for their own project. He is not allowed to modify the actual base image. So, the original file will never get affected, and users can modify their cloned copy according to requirements.
Boxes are generally namespace and broken down into parts with the username and the box name using a slash. You can see the above example carefully for the same purpose. Boxes can be specified with URLs or file pathname depends on your knowledge and coding style.
It is a misconception that boxes namespaced with Ubuntu or Hashicorp are managed by the same Company, but it is not true. Boxes are published by third-Company generally, and Ubuntu support team does not have any control over it.
Now, when you have added a box to the Vagrant, it is the time to configure a project and use it as the database. First of all, open the Vagrant file and change its content. You may use the following command for the same purpose.
Vagrant.configure(“2”) do |config| Config.vm.box =”hashicorp/precise64” end
It matches the name you used to add the box above. In this way, Vagrant verifies how to use a Vagrant box. If the box is not added earlier, it will be automatically added and installed when it is run. You may also specify the explicit version of the box with the following command:
vagrant.configure("2") do |config| config.vm.box ="hashicorp/precise64" config.vm.box_version = "1.1.0" end
You may also specify the URL instead of box name, as shown below.
vagrant.configure("2") do |config| config.vm.box ="hashicorp/precise64" config.vm.box_url ="https://vagrantcloud.com/hashicorp/precise64" end
In the next, let us bring up the virtual environment and learn a little bit more about it.
To bring up the Vagrant virtual environment, you can use the following command.
This command creates, configures, and starts the virtual machine based on the Vagrant file. When the command is run, it will automatically download the needed Vagrant box from the box repository and performs the provisioning. To SSH any running Vagrant machine or to access the shell, you may use the following command:
$ vagrant ssh
This command will allow you to interrupt another system and do whatever you want. However, it may be tempting, be careful here. Wait a moment and think about what has been done here. With two simple commands, we set up the virtual environment and SSH to other systems as per the requirements. Isn’t it sounds cool? Obviously, Yes! To terminate the SSH session, you should use the following command:
[email protected]:~$ logout Connection to 127.0.0.1 closed.
Now destroy or remove commands can be used to destroy the virtual machine completely and come back to your local system.
Is this possible editing files on your own system and sync them with the virtual machine? Yes, Vagrant can make it possible with the provisioning feature. First SSH in and install a web server, every Vagrant user should work in the same manner. Vagrant has built-in support for automated provisioning. In this way, it automatically downloads software when starting the virtual environment and makes your machine ready to use.
Let us see how to set up Apache for your first Vagrant project. It can be done using a shell script with the following commands:
#!/usr/bin/env bash apt-get update apt-get install -y apache2 if ! [-L /var/www ]; then rm -rf /var/www ln -fs /vagrant /var/www fi
Now configure the Vagrant to run this shell script and set up your own machine. The configuration is possible by editing the Vagrant file. Here is the set of commands to follow:
vaggrant.configure ("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision :shell, path: "bootstrap.sh" end
Once everything is done, run the Vagrant up command to start the virtual environment and Vagrant will provision it automatically. You should also monitor the output from the shell script appearing to the terminal. If the guest machine is already running, then restart it and skip the important initial steps. Once Vagrant completes running, the web server will be up and starts running. You can see the website from your own browser and verify the provision work by loading a file from SSH from the machine.
$ vagrant ssh ... [email protected]:~$ wget -qo- 127.0.0.1
You are free to play around commands by creating more file and view the content from your own terminal. In the next section, let us learn networking with Vagrant.
At this point, we have a web server and modified files that are already synced with the guest machine. However, accessing webpages through the terminal is not that much satisfying. Here, Vagrant is useful where you can use its networking features to give us additional options for accessing the machine from the host machine.
One excellent option here is port forwarding. It allows you to specify ports on a guest machine and support them through a host machine. You may also access ports on your personal machine and forward the all network traffic to a specific port on the guest machine. Doing so with Vagrant is so simple, and it looks like this:
Vagrant.configure("2") do |config| config.vm.box = "hashicorp/precise64" config.vm.provision :shell, path: "bootstrap.sh" config.vm.network :forwarded_port, guest: 80, host:4567 end
Once the machine starts running again, load the IP address in your browser, and you will observe that web page on the virtual machine is automatically set up by the Vagrant. Further, it allows other networking options too like assigning a static IP address, bridging guest machine with the existing network, and more. In the next section, let us learn how to set up shares with Vagrant.
Now when a web server is running and accessible from different machines, we are working on a fully functional development environment. Vagrant makes it easy to share and collaborate information among different environments. The primary feature of Vagrant is a share that allows sharing Vagrant with anyone around the world. You just need the internet to establish the connection and share the environment. It will generate a URL that can be accessed from anywhere in the world who is connected to the internet. Here is the simple command for the same purpose:
$ vagrant share ... ===> default: Creating Vagrant Share Session... ===> default: HTTP URL: http://b1fb1f3f.ngrok.io ...
Your URL could be different, but it should be accessible and working in a web browser. When you share the folder and refresh the URL, you can see that it is updated. The URL with the route you to the Vagrant environment directly and press the CTRL+C button to end the session and make sure that the environment is not more shared.
It is just wonderful when you control a computer in a completely different part of the world and come right back to your own world with just a simple command. Now you must be sure of the benefits and flexibility of Vagrant. It is certainly a better option than VMware, and it makes your life ten times smoother and productive as a software developer. Vagrant skills certainly put you out of the crowd when you add them to your resume.
JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.
Course for testing
Receive Latest Materials and Offers on DevOps Course