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.
Vagrant on Mac w Virtualbox The Goal: Install Brew, VirtualBox, Vagrant and Vagrant-Manager, on a Macintosh 10.x Context: We will use Vagrant with VirtualBox. This guide is to help those unfamiliar with how to install and use Vagrant and Virtualbox setup. Aug 24, 2019 Vagrant is a virtual machine manager that simplifies setting up environments. It uses VirtualBox to manage your local development environment and ensures that the configuration is self-contained. If you’re using Mac OS X or Windows, there is a tool called the Vagrant-Manager that can help you manage all your virtual machines from your menu bar.
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
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:
Run Mac In Virtualbox
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.
Install Virtualbox In Mac
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.
Mac Virtualbox Vagrant Search
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.