This is a quick introduction to Vagrant as I use it on a daily basis for some of my local development.
So probably you’ve heard a bit of Vagrant, or probably have no clue what’s this all about. Either way, in a nutshell, Vagrant is a powerful tool for creating local virtual machines for identical and portable development environments.
Have you been in the situation that something works in your local machine and it brakes when pushed to production ( or vice versa )?
Well, that’s the problem which Vagrant tend to solve.
You can use Vagrant to build a virtual server on your local machine with custom specifications. This server can be distributed across your team or other developers to recreate the same environment as your current one – software requirements, packages, operating system, and more.
Vagrant vs XAMPP?
This question pops up all the time. Why not use XAMPP instead of trying to configure your own virtual server?
Short answer, Vagrant is far more powerful and you can learn a lot about setting your own server, getting used to the terminal and many things that you’ll one day need in order to deploy a complex application.
On top of that:
- XAMPP is a pre-built package of PHP, Apache and MySQL. You can’t add other servers.
- You are stuck with MySQL. That’s fine for WordPress, but a serious disadvantage when you work with other databases.
- XAMPP installs on your operating system, meaning it pollutes your main system’s space. On top of that, XAMPP is OS-specific, which brings a lot of Windows-related PHP issues.
Don’t get the wrong idea, I still like and use XAMPP on a daily basis for simple Apache servers with PHP and MySQL. And it’s faster for local development than Vagrant.
But like I said, Vagrant is far more powerful and it’s worth investing in learning it.
So enough dry theory, let’s try to install and run Vagrant.
Quick disclaimer: I’m using Windows 10 for this guide, but the steps for Linux/Mac are almost the same. You just need the appropriate VirtualBox and Vagrant installation for different operating systems.
What do we need first?
Since Vagrant is using virtual environments to work, we first need to install Virtual Box on our machine. This would give us the ability to create virtual servers through Vagrant.
After you install everything, open your favorite terminal and type
vagrant -v
This should print the current Vagrant version, which confirms that our installation is complete and ready to rock!
First Project Setup
Now we have everything we need. What should we do next?
The first step of creating any Vagrant project is to get or create the Vagrantfile. This is essentially the main configuration file for our virtual server and it contains the information for the Vagrant box ( we’ll get to this ). The command for creating a blank, boilerplate configuration file is:
vagrant init
Let’s try it out! Create a blank directory anywhere on your machine and name it however you want. For this tutorial I will name it Vagrant-Project
. That is the root of our project.
Next, open your command line, navigate to your newly created directory (in my case cd D:\Vagrant-Project
) and type:
vagrant init
As you can see, a new file has been created in our directory.
Those Vagrantfiles contain information for a base image so we can quickly clone a complete virtual machine. These base images are known as Vagrant boxes, and specifying the box to use is the first step for our project. One box can be used by anyone on any platform, that Vagrant supports, to provide identical working environment.
Hopefully there are many pre-configured Vagrantfiles available, so we don’t need to manually configure our Vagrant file. All we need to do is download a ready Vagrant file and type vagrant up in the command line. That’s it.
For LAMP environment I mainly use the Scotch Vagrant Box. It’s free, stable and it has all the features I need for a local LAMP development (including WordPress development). It comes with:
- Ubuntu, PHP, Ruby, Vim, Git, Node, Gulp, MySQL, MongoDB and many other useful features. See the full list here.
Building our server
Let’s use the Scotch Box to build our first virtual server.
First, go ahead and delete the blank Vagrantfile we’ve just created in the previous step – we don’t need it since we will download a new, pre-configured one.
Open the project folder in the terminal
cd D:\Vagrant-Project
and type:
git clone https://github.com/scotch-io/scotch-box.git .
This would download the vagrant file to our current directory. Alternatively, you can just go to https://github.com/scotch-io/scotch-box and download the package directly.
When we have everything downloaded, you should see a vagrant file in the current directory. The command for building and running the server is:
vagrant up
When we run this for the first time, the box will need to download all its resources and it may take some time. After that initial load, any subsequent vagrant up commands should be executed much faster.
Access your server
When the previous process ends, the newly created server should be up and running! You can now access your project by visiting the http://192.168.33.10/ in your browser. That’s the default ScotchBox home page and it displays the main installed features.
This page is located in the public folder ,which is the default folder for the public files ( like htdocs in XAMPP ). Any additional access details are provided on their GitHub page, such as SSH access, MySQL access, and others.
At this point we have fully functional virtual server running on our local host.
See, it wasn’t so hard. To turn off this server simply type vagrant halt, and when you want to boot it up again – vagrant up. Other useful commands include:
// Start the virtual server
vagrant up
// SSH into the virtual server
vagrant ssh
// Halt the virtual server
vagrant halt
// Destroy the virtual server
vagrant destroy
// Reloads the virtual server
vagrant reload
// Shows current status for the virtual server
vagrant status
Changing the URL
You can always change the default ugly IP address into something more readable like dev.local . To do this, we need to edit our hosts file and add a simple line. In Windows 10 this file is located in C:\Windows\System32\Drivers\etc and it requires administration rights in order to edit, so it’s a bit different from straight copy-paste-save.
To edit this file, first open NotePad as an administrator ( right-click on NotePad -> Run as administrator ), then click File -> Open ( or Ctrl + O ) and find the hosts file ( C:\Windows\System32\Drivers\etc\hosts ). Then add the following line at the bottom of your hosts file:
192.168.33.10 dev.local www.dev.local
And then save the file. You can change the dev.local part however you want. In our case, we can now access the Vagrant server through typing dev.local in the address bar.
I’ve only scratched the surface of what Vagrant can do. But for the most part, that’s all you need to get you up and running in no time. Now go ahead and feel free to experiment and mess around with your virtual environment.
In the worst-case scenario, all you need to do is to destroy and rebuild your virtual environment.