How to Install Vagrant on Linux to Create Virtual Dev Environment

Vagrant Logo

Using Vagrant, developers and sysadmins can create any virtual environment instantly. Vagrant is extremely simple to use and configure.

Vagrant acts as central configuration repository for managing and deploying multiple reproducible virtual environments from a canned OS image with same configuration.

Vagrant is an open source tool used for creating a portable virtual environment.

 

1. Install Vagrant

First, download the vagrant binaries for your OS from VagrantUp.

Currently it is available for MacOS, Windows, Debian and CentOS.

For this tutorial, we downloaded the 64bit rpm for centos.

# wget https://releases.hashicorp.com/vagrant/1.8.1/vagrant_1.8.1_x86_64.rpm

Install the downloaded file on your system using your systems appropriate package installer. Since we are installing this on CentOS, we have downloaded the rpm file and using rpm command to install the vagrant.

Please note that vagrant is available on windows as well which means you can download and install vagrant and virtualbox on your windows machine and then run the vagrant commands to quickly build any available vagrant OS’es for your testing.

# rpm -ivh vagrant_1.8.1_x86_64.rpmPreparing...     ################# [100%]   1:vagrant     ################# [100%]

Verify that the vagrant package is installed successfully.

# vagrant -vVagrant 1.8.1

2. Add a Vagrant Box

The next step is to run “vagrant box add” command. In this example, I am using one of the predefined catalogs available on HashiCorp’s Atlas box catalog. This catalog has LAMP stack already configured on it.

# vagrant box add smallhadroncollider/centos-6.4-lamp==> box: Loading metadata for box 'smallhadroncollider/centos-6.4-lamp'    box: URL: https://atlas.hashicorp.com/smallhadroncollider/centos-6.4-lamp==> box: Adding box 'smallhadroncollider/centos-6.4-lamp' (v1.1) for provider: virtualbox    box: Downloading: https://atlas.hashicorp.com/smallhadroncollider/boxes/centos-6.4-lamp/versions/1.1/providers/virtualbox.box==> box: Successfully added box 'smallhadroncollider/centos-6.4-lamp' (v1.1) for 'virtualbox'!

Verify that the vagrant box is successfully added using the following command. This will display all the vagrant boxes that are installed on our system. Currently we have only one box installed.

# vagrant box listsmallhadroncollider/centos-6.4-lamp (virtualbox, 1.1)

3. Initialize Vagrant Box

Now we are ready to initialize the new Vagrant environment by creating a vagrant file. vagrant status command displays the currently initialized vagrant environments. In this case since we do not have any machines initialized yet, vagrant status gives the below output.

Now we can create a new directory and initialize the vagrant environment that we just downloaded, this will place a plain vagrantfile in the current working directory. You can change many of settings in this file, we can change things such as network configuration, shared folders, and puppet and chef details.

# vagrant statusA Vagrant environment or target machine is required to run thiscommand. Run `vagrant init` to create a new Vagrant environment. Or,get an ID of a target machine from `vagrant global-status` to runthis command on. A final option is to change to a directory with aVagrantfile and to try again.

Just be aware a box restart will likely be required before changes take effect.

To learn more about configuration inside the vagrant file, you can refer the vagrant documentation.

# mkdir centos-6.4-lamp# cd centos-6.4-lamp# vagrant init smallhadroncollider/centos-6.4-lampA `Vagrantfile` has been placed in this directory. You are nowready to `vagrant up` your first virtual environment! Please readthe comments in the Vagrantfile as well as documentation on`vagrantup.com` for more information on using Vagrant.

After you initialize vagrant box, you’ll see that it has created the following file under the current directory.

# ls -ltr-rw-r--r--. 1 root root 3017 Jan 27 18:29 Vagrantfile

4. Provider Dependency

Now if you to try to bring up the vagrant machine, you will see the below error. This is because the provider has not been installed yet on the machine. The provider can be usually Oracle Virtualbox, VMWARE, Hyper-V. Once you choose the provider, depending on OS platform install the respective rpm on the machine.

# vagrant upNo usable default provider could be found for your system.Vagrant relies on interactions with 3rd party systems, known as"providers", to provide Vagrant with resources to run developmentenvironments. Examples are VirtualBox, VMware, Hyper-V.The easiest solution to this message is to install VirtualBox, whichis available for free on all major platforms.If you believe you already have a provider available, make sure itis properly installed and configured. You can see more details aboutwhy a particular provider isn't working by forcing usage with`vagrant up --provider=PROVIDER`, which should give you a more specificerror message for that particular provider.

Once a provider (Oracle virtualbox, VMWare, etc.) is installed, try to bring up the virtual environment using “vagrant up” as shown below, you can also setup your public network and any other software packages needs to be included or any other configurations in the vagrant file and bring up the virtual machine.

Note: In my case, I have downloaded and installed Oracle Virtualbox. You need to have one of the virtualization provider installed on your machine for this to work. Earlier we explained in detail about How to install Oracle VirtualBox.

# vagrant upBringing machine 'default' up with 'virtualbox' provider...==> default: Importing base box 'smallhadroncollider/centos-6.4-lamp'...==> default: Matching MAC address for NAT networking...==> default: Checking if box 'smallhadroncollider/centos-6.4-lamp' is up to date...==> default: Setting the name of the VM: centos-64-lamp_default_1419728026285_91788==> default: Clearing any previously set network interfaces...==> default: Preparing network interfaces based on configuration...    default: Adapter 1: nat==> default: Forwarding ports...    default: 22 => 2222 (adapter 1)==> default: Booting VM...==> default: Waiting for machine to boot. This may take a few minutes...    default: SSH address: 127.0.0.1:2222    default: SSH username: vagrant    default: SSH auth method: private key......

5. Start and Stop Vagrant

The vagrant status command displays the current status of the virtual machine.

# vagrant statusCurrent machine states:default                   running (virtualbox)The VM is running. To stop this VM, you can run `vagrant halt` toshut it down forcefully, or you can run `vagrant suspend` to simplysuspend the virtual machine. In either case, to restart it again,simply run `vagrant up`.

To halt or shutdown a virtual environment for making changes, use the “vagrant halt” command as shown below,

# vagrant halt==> default: Attempting graceful shutdown of VM...    default: Guest communication could not be established! This is usually because    default: SSH is not running, the authentication information was changed,    default: or some other networking issue. Vagrant will force halt, if    default: capable.==> default: Forcing shutdown of VM...

Now if you check the status again, you’ll notice that it says “poweroff” as shown below.

# vagrant statusCurrent machine states:default                   poweroff (virtualbox)The VM is powered off. To restart the VM, simply run `vagrant up`

To start the Vagrant again, use the following:

# vagrant up

6. Login to Vagrant Virtual Machine

To SSH in to the vagrant virtual machine, you use the vagrant SSH command as shown below.

# vagrant [email protected]$

In order to display the SSH config on the vagrant environment, you can use the below command. You can set any of the values in the Vagrantfile if you need to modify it.

# vagrant ssh-configHost default  HostName 127.0.0.1  User vagrant  Port 2222  UserKnownHostsFile /dev/null  StrictHostKeyChecking no  PasswordAuthentication no  IdentityFile /root/.vagrant.d/insecure_private_key  IdentitiesOnly yes  LogLevel FATAL  ForwardAgent yes

7. Reload Vagrant

Whenever you make any changes to the Vagrantfile, you can reload the configuration using “vagrant reload” command. This will reboot your virtual environment.

# vagrant reload==> default: Attempting graceful shutdown of VM...    default: Guest communication could not be established! This is usually because    default: SSH is not running, the authentication information was changed,    default: or some other networking issue. Vagrant will force halt, if    default: capable.==> default: Forcing shutdown of VM...==> default: Checking if box 'smallhadroncollider/centos-6.4-lamp' is up to date...==> default: Clearing any previously set forwarded ports...==> default: Clearing any previously set network interfaces...==> default: Preparing network interfaces based on configuration...    default: Adapter 1: nat    default: Adapter 2: hostonly    default: Adapter 3: bridged==> default: Forwarding ports...    default: 22 => 2222 (adapter 1)==> default: Booting VM.........

To destroy the vagrant machine after all your testing is completed, you can run the following command.

# vagrant destroy

This will remove all the disks allocated to the VM, but the Vagrantfile will be still there.

Even after you delete the vagrant instance, if you ever want to rebuild the virtual environment with the same configuration, you can still use the Vagrantfile, that was created initially when you did the initialization.

>

You may also like...