Introduction: How to Setup and Use a Vagrant Box

Picture of How to Setup and Use a Vagrant Box

This might be useful if you need a Linux environment for development and you don't want install a Linux distribution with dual-boot. Basically, this is the same as running a VirtualBox instance but on some older machines (like mine) virtual machines are slow and unresponsive. Using Vagrant has the advantage of running mostly images that were intended for cloud environments so they only contain the basic things from that operating system. The vagrant boxes run without the usual VirtualBox GUI so you will need to use SSH to login into them as if they were remote machines.

Step 1: Download and Install the Tools

First, we need to download and install the tools that we will use:

  • Download VirtualBox from here and install it

  • Download Vagrant from here and install it

  • Download PuTTY from here

  • Download PuTTYgen from here

(You don't need to install PuTTY and PuTTYgen)

Step 2: Find the OS That You Want to Run

Look for the Operating System that you want to run in the catalog found on the official website: link

Step 3: Prepare the CMD Window Where You Will Run the Necessary Commands

Picture of Prepare the CMD Window Where You Will Run the Necessary Commands

Go and create a new folder on your hard drive. That folder will be the shared folder between the virtual machine and your host machine.

Now hold the 'Shift' key and right-click on that folder and select "Open command windows here" and you will open a CMD windows in that location.

Step 4: Prepare Your Vagrantfile

Picture of Prepare Your Vagrantfile

Prepare your vagrantfile (a file that contains some settings for your virtual machine) by running "vagrant init <your_OS>" where you replace <your_OS> with an operating system that is found in the catalog

Common examples:

  • For running Ubuntu 16.04 run "vagrant init ubuntu/xenial64"
  • For running Ubuntu 14.04 run "vagrant init ubuntu/trusty64"
  • For running Fedora 23 run "vagrant init fedora/23-cloud-base"
  • For running Centos 7 run "vagrant init centos/7"

Step 5: Start Up Your Virtual Machine

Picture of Start Up Your Virtual Machine

Start your virtual machine by running "vagrant up" (Do this on the same command prompt)

An OS image will be downloaded and installed. It should look similar to the screenshot.

Step 6: Prepare the Private Key That You Will Use to Login Into the Machine

Picture of Prepare the Private Key That You Will Use to Login Into the Machine

After the virtual machine is started you can't interact with it since you don't have the VirtualBox GUI so you will have to treat it as a remote machine and login by using SSH. The cloud images usually don't allow users to login by using a password and instead they have to use a private key. To import the generated private key into the SSH client that you will use (PuTTY) you will have to use PuTTYgen.

Start PuTTYgen and click the "Load" button and go to the folder where you just ran "vagrant up" and go to .vagrant\machines\default\virtualbox
Near the "File Name" menu you have a dropdown menu for selecting extensions. Select "All files" from there and then select the file named "private_key" and click "Open" Click the "Save private key" button and answer yes if you're asked if you're sure that you want to save it without a passphrase. Save it with a name that will be easy to recognize. I picked "private_key_putty"

Step 7: Login Into the Virtual Machine

Picture of Login Into the Virtual Machine

Open PuTTY and write "127.0.0.1" as the Host Name, 2222 as the port and then go to Connection->SSH->Auth and click the "Browse" button under the "Private key file for authentication" field. Here, select the private_key_putty file that you generated at the previous step.

(The details for your box might be different. To see details about that run "vagrant ssh")

Extra: Now go back to Session and you will be able to save the settings for this session so you won't have to input them again (Input a name in the "Saved Sessions" field and click "Save").

Click "Open" and you will be prompted to enter the user you want to login as. You can get the default user from the OS catalog mentioned in a previous step (or by running "Vagrant ssh").

For example the default user for Ubuntu distributions is ubuntu and the default one for Fedora is vagrant

Step 8: Using the Virtual Machine

Now you will be able to use the vagrant box for development given that you only need the command line to interact with it (if you also need the GUI i recommend installing a Linux distribution on your computer or using a normal Virtual Machine if your computer can handle it)

The vagrant commands that you'll likely use from now on are:

  • "vagrant up" - starts the box. After that's done you can connect to it using PuTTY
  • "vagrant halt" - stops the box.
  • "vagrant init" - generates the vagrantfile corresponding to the distribution that you picked
  • "vagrant destroy" - deletes the box

For more information you can always visit the official website: https://www.vagrantup.com/docs/cli/

Comments

Swansong (author)2016-10-13

Thanks for sharing :)

About This Instructable

814views

9favorites

License:

More by Cristi075:How to Setup and Use a Vagrant Box
Add instructable to: