Vagrant
Vagrant is an open-source tool that allows you to create, configure, and manage boxes of virtual machines through an easy to use command interface. Essentially, it is a layer of software installed between a virtualization tool (such as VirtualBox, Docker, Hyper-V) and a VM. Vagrant is an open-source utility created by guys in Hashicorp. It is a wrapper utility that works on top of Virtual machine solutions like Virtualbox, HyperV, VMware, and also Docker. It abstracts away all the complex activities involved in managing a VM through the VM solutions and can automate most of the tasks.
Learn about virtual machines and how you can use Vagrant and VirtualBox to set up a local Ubuntu installation.
I've gotten to the point in my web development career where I can no longer avoid the words VirtualBox, Vagrant, and Docker. Thanks to my friend Timm Stelzer, I finally got over my fear of the unknown and delved into the world of virtual machines via Vagrant and VirtualBox.
Now that I've finally used them, I can see how fun and useful they are, and since I've just set everything up myself in 2017 with the most up-to-date installations I could find, I can share what I've learned with you.
Prerequisites
The only prerequisite to this article is command line knowledge, and it is mandatory. This article that I wrote, How to Use the Command Line, will tell you everything you need to know, from moving around directories, creating files, and ssh
ing into servers. If you're not familiar with any of that, please read the aforementioned article.
Goals
In this tutorial, we will learn:
- What VirtualBox is
- What Vagrant is
- How to use Vagrant and VirtualBox in tandem to set up a local Ubuntu installation
- How to connect to the Ubuntu server we create
Introduction
VirtualBox is basically inception for your computer. You can use VirtualBox to run entire sandboxed operating systems within your own computer.
Vagrant is software that is used to manage a development environment. Through the command line, you can grab any available OS, install it, configure it, run it, work inside of it, shut it down, and more.
Using VirtualBox and Vagrant, you can simulate the production environment of your app or website. For example, if you're using Digital Ocean or AWS to run a Virtual Private Server (VPS) in the cloud running Ubuntu, PHP, and MySQL, you can install your local version to have all the same versions of that software, while keeping your own computer's software untouched. This can reduce and eliminate bugs and errors that result from trying to develop code for a production server on an environment that does not match.
In this tutorial, we're going to install Vagrant and VirtualBox, and install Ubuntu Server. We're going to connect to the Ubuntu server though ssh
to confirm that everything is running properly.
After this tutorial, learn to install a LAMP server on the environment we create.
Step 1 - Install Virtual Box and Vagrant
Install VirtualBox
Go to the VirtualBox download page, choose your operating system (Windows or macOS) and download and install the software.
I clicked OS X and the download started.
Install Vagrant
Go to the Vagrant download page, choose your operating system (Windows or macOS) and download and install the software.
Again, I chose Mac OS X.
Step 2 - Install Ubuntu Server with Vagrant
Create a directory for your virtual host to live. I created a VirtualMachines directory (which will potentially contain all of my virtual machine installations), and made a new directory within called ubuntu for this specific installation to live. The whole path is as follows:
Your path will be slightly different for Windows, using C:/
, but otherwise will be the same idea.
Install Ubuntu Box
Vagrantfile
You can Discover Vagrant Boxes via the Vagrant Cloud. ubuntu/trusty64
is the most popular box (machine), with nearly 30 million installs and updated within the last day. This is the one we'll be using.
In Terminal (Mac) or Git Bash (or whatever shell you're using on Windows), type the following to install Ubuntu Server 14.04.
After a few moments, this will be the output to let you know it's successful.
Now initialize the new vagrant
in your VirtualMachines/ubuntu directory with the init
command.
Finally, get Ubuntu up and running with the vagrant up
command.
When you see 'Machine booted and ready!', everything is good to go!
Fixing Guest Additions Error
This is great, and all set up. However, you may have gotten this error:
This might not be a problem, but let's make sure it doesn't become a problem. First, use the halt
method to shut down your currently running Vagrant.
Let's quickly install a plugin called vbguest
to fix this error.
It will run through some more commands, and most likely the above error will still persist. Let's reload the Vagrant machine with the reload
command.
Now the errors will be gone.
What about VirtualBox?
Looks like we didn't use VirtualBox for anything at all. But go ahead and open up the VirtualBox application. This is what you will see running:
As you can see, VirtualBox is letting us know that Ubuntu is running properly. We don't really need to know much more about it at this point.
Step 3 - SSH into Ubuntu
Now we have this Ubuntu operating system set up on our computer, but how do we access it? Just like you would access any remote Linux server through the command line, you will do the same with Vagrant. Run vagrant ssh
to securely enter the Ubuntu virtual machine.
When you login, you will see something along these lines.
You're in! You now have a complete Ubuntu Server installation running through VirtualBox on your computer, and you're connected to it. Type exit
at any time to exit out of the server, and vagrant halt
to shut it down.
Conclusion
Here is a review of the commands we used with Vagrant today.
Command | Purpose |
---|---|
vagrant box add ORG/BUILD | Add a new virtual machine |
vagrant init ORG/BUILD | Initialize virtual machine |
vagrant up | Start up virtual machine |
vagrant reload | Restart virtual machine |
vagrant halt | Shut down virtual machine |
vagrant ssh | SSH into the virtual machine |
Note: All these commands must be done locally from the directory in which you want the virtual machine to be installed, except vagrant box add
.
As a review, VirtualBox is the software that runs the operating system, and vagrant will give you the methods to manage them. You can also think of vagrant as a package manager that has a repository of different operating systems to use.
Vagrant and VirtualBox can be used to create a local environment that matches the production environment of your server.
Move on to Part 2: Setting up LAMP (Linux, Apache, MySQL, and PHP) from the Ubuntu Server environment we created, and how to access it from your local computer.
Vagrant
The Vagrant getting started tutorials will walk you through your firstVagrant project, highlighting major features of Vagrant.
If you are curious about the benefits Vagrant read the 'Why Vagrant?' introduction page.
Vagrant Boxes
The getting started tutorials use Vagrant with VirtualBox,since it is free and available on every major platform. Vagrantcan work with many other providers.
»Prerequisites
- Install the latest version of Vagrant.
- Install VirtualBox
»Up and Running
After you run the following two commands, you will have a fully runningvirtual machine in VirtualBox runningUbuntu 18.04 LTS 64-bit.
Initialize Vagrant.
Start the virtual machine.
Vagrant Status
SSH into this machine with vagrant ssh
, and explore your environment. Leave the SSH session with logout
.
Vagrant Synonym
When you are done exploring terminate the virtual machine, and confirm when the CLI prompts you by typing yes
.
Now imagine every project you've ever worked on being this easy toset up! With Vagrant, vagrant up
is all you need to work on any project,to install every dependency that project needs, and to set up anynetworking or synced folders, so you can continue working from thecomfort of your own machine.
The rest of these tutorials will walk you through setting up a morecomplete project.
You have just created your first virtual environment with Vagrant. Read on tolearn more about project setup.