Beginner’s guide to Vagrant
Who in today’s world wants to waste their time in setting up development environment, configuring variables and a lot more of such repetitive tasks.
Everyone loves automation! Especially developers :-P
What is Vagrant?
According to the official doc: Vagrant is a tool for building and managing virtual machine environments in a single workflow.
A virtual machine is a computer file, typically called an image, which behaves like an actual computer. In other words, creating a computer within a computer.
Through vagrant command-line utility, you can easily
- grab any available OS.
- install it
- configure it
- run it
- work inside of it
- shut it down and much more.
A user just needs to get your Vagrantfile and he can configure the environment with one command. Yes, it’s that simple.
Getting Started
Installation:
Visit https://www.vagrantup.com/downloads.html. Install vagrant by choosing your OS.
// Check whether your installation was successful or not
vagrant -vExpected Output: (version can be different)
Vagrant 2.2.5
Create a new directory
mkdir learn-vagrant
cd learn-vagrant
Now, we need to initialize vagrant. For that, run
vagrant init
Check the directory contents, you will notice a Vagrantfile is now present. Let’s check this —
vi Vagrantfile
you will notice that the file has a lot of comments. For now, you can clear the whole file, we will write it from scratch.
Replace the content of Vagrantfile with following :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y nodejs
SHELL
end
Here, in line 2 → config.vm.box denotes which OS we are installing on our machine. We are using ubuntu/bionic64 which is installing ubuntu 18.04.
In lines 3–6 → we execute some shell commands on our ubuntu machine. We are installing nodejs.
Let’s get our system up and running. You guessed the command probably :-P
vagrant up
This might take some time since it downloads ubuntu in the first run.
Let’s ssh into our created box, to check if nodejs is installed or not?
vagrant ssh
// You will be in your created box now
run node -vExpected output : (version might be different)
v8.10.0
Tada !! That was easy.
Now, it’s up to you to play along with this. Setup a web server, host your site, etc.
To stop the machine —
vagrant halt
// To stop and delete all traces
vagrant destroy// Check out further vagrant commands using -
vagrant help
To learn more about vagrant — check out https://www.vagrantup.com/.
Do leave comments on how you use vagrant and make your life easier.
Don’t forget to share and follow :)
Check out my other articles here.