詳細検索

Let's use Vagrant super easy and convenient!

Avatar
by 平形大樹
4 min read

Let's use Vagrant super easy and convenient!
Translated from 日本語 • View original

Hello. This is the developer's flat shape.

Introduction

This article explains how to use Vagrant. This article is a continuation of the series, so if you haven't seen it yet, we recommend reading the following article first. Build an agile and scalable development environment in the DevOps era with Vagrant, GitHub, Travis, Chef, and OpsWorks

What is Vagrant?

Vagrant is a virtual development environment construction software for FLOSS[1]. It can also be seen as a wrapper for virtualization software such as VirtualBox, as well as configuration management software such as Chef, Salt, and Puppet. With Vagrant, you can automatically perform everything from building to configuring a virtual environment based on a configuration file that describes configuration information. Initially, it targeted VirtualBox, but in versions 1.1 and later, it can also target other virtualization software such as VMware and server environments such as Amazon EC2.[3] Vagrant itself is written in Ruby, but it can also be used to develop other programming languages such as PHP, Python, Java, C#, and JavaScript. Quote source: wiki

Why Vagrant?

First, I will talk about the current issues. Is there anything like this?

  • The developer's PC OS is disjointed, so it is troublesome to create a procedure for building an environment.
  • I don't even want to think about Windows.
  • Everyone's environment is different during hands-on, so everyone behaves differently and I hate it.

With Vagrant, you can do this.

  • Specifying the virtual machine's operating system, network settings, and system resources to be used by code.
  • You can build an environment using configuration management tools such as Chef.

The following benefits are available:

  • Distribute a common development environment to multiple developers.
  • Virtual machine settings can be managed in a single file, making it easy to recognize.
  • It can provide a common development environment for Windows, Mac, and Linux.

Do you think it's troublesome to use vagrant? It's insanely easy! If you haven't used it yet, install it for now!

Installation

VirtualBox

Vagrant will use virtualization software such as VirtualBox to launch a virtual machine. This time we will use VirtualBox.

Download and install it from here.

Vagrant

Download and install it from here.

Quick Start

The OS will be ubuntu 12.04 (32bit).

First, create a directory of work locations.

$ mkdir vagrant_test
$ cd vagrant_test

Next, create a configuration file for Vagrant.

$ vim Vagrantfile

Paste the following into the 'Vagrantfile'.

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise32"
end

Start the virtual machine.

$ vagrant up

And that's it. What do you think? It's so easy, isn't it? Did you have a nosebleed? By the way, this time I chose ubuntu, but you can also specify other OS. Please choose your favorite one from the catalog below and specify it as the box name. Box Catalog

Basic Operations of Vagrant

  • Launch the 'vagrant up' virtual machine
  • SSH login to the 'vagrant ssh' virtual machine
  • 'Vagrant provision' provisioning of virtual machines (configuration updates when using configuration management tools such as chef)
  • 'Vagrant halt' virtual machine shutdown
  • 'vagrant suspend' virtual machine suspend
  • 'vagrant destroy' deletion of virtual machine

For the time being, you don't have to worry about remembering this.

Vagrantfile

Perform detailed virtual machine settings in a vagrantfile.

IP Address

Set the IP address to '192.168.50.100'.

config.vm.network "private_network", ip: "192.168.50.100"

Set the amount of memory and the number of cores on the CPU used by the virtual machine.

Set the memory to '2048MB' and the number of CPU cores to 2.

config.vm.provider :virtualbox do |vb|
  vb.memory = 2048
  vb.cpus = 2
end

Synchronizing the Directory

Sync the './data' directory on the host side to '/srv/website' on the guest side. This is the premise that the 'data' directory exists.

config.vm.synced_folder "./data", "/srv/website"

For example, if you are developing a web application, it is convenient to specify the document route here.

Log in with SSH

You can log in quickly with 'vagrant ssh'.

$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

* Documentation:  https://help.ubuntu.com/
New release '14.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
vagrant@precise32:~$ 

I don't need to set it up. It's crispy.

It's OK to mess with various things, break them, and throw them away. If something happens, you can start over from scratch. When remaking, 'vagrant destroy' and 'vagrant up'.

The Vagrantfile I Used This Time

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.network "private_network", ip: "192.168.50.100"
  config.vm.provider :virtualbox do |vb|
    vb.memory = 2048
    vb.cpus = 2
  end
  config.vm.synced_folder "./data", "/srv/website"
end

If you have changed the vagrantfile, restart it with 'vagrant reload'.

Conclusion

How was it? Was it difficult? I don't think I feel the power of Vagrant that much at this point. However, from the next time onwards, it will work like a wrinkle and body blow.

Next time

We'll talk about how to manage configurations in Chef.

Related Articles