[LINUX] [Introduction with official documentation] Vagrant tutorial (with translation + thorough explanation)

Introduction

Target audience

――I've heard of virtual environments, but I've never actually used them. --I can understand the basic commands of the terminal (cd, ls, mkdir, etc.). ――I want to use the basics of VirtualBox / Vagrant. ――I want to study Linux using a virtual environment.

Content introduction

So that people like the above can get a quick overview of Vagrant and how to use it.

-Introduction “Introduction to Vagrant” -Quick Start “Getting Started”

I have translated and summarized the two official Vagrant documentation articles.

Also, in putting together the above two documents, I added __supplementary explanations fairly carefully so that (even beginners) can understand with only this Qiita article as much as possible. __

In particular, __ "Concepts / Terms you should know when reading the official documentation" and "Installing VirtualBox and Vagrant" are explained as separate chapters as a supplement. __ (If you don't need it, skip itClick here!)

[Preparation # 1] Concepts / terms you should know

Here, in the next chapter, before actually building a virtual environment using VirtualBox and Vagrant, we will explain the terms that will help you to understand by dividing them into __7 __ topics as supplements.

1. What is virtualization with VirtualBox?

What is VirtualBox? According to IT Glossary / e-Words, "One of the open source virtualization software. You can build a virtual computer (Virtual Machine) as one of the applications that runs on the operating system (OS) and run another OS on it. "

The feature is that you can run a virtual machine with a different OS on a host (base) OS such as Mac, Windows, or Linux. At Vagrant, this kind of virtualization software (including VirtualBox) is called a provider.

In the figure created by the author above, the virtualization software is listed on the host OS. This is called host OS type virtualization. (There are other hypervisor types, but I will omit them this time.)

There is no problem for now with the understanding that a virtual machine running on another OS is placed on top of __OS. __

2. What is a provider?

__ Software that provides a virtual environment __. Vagrant builds and operates virtual machines on this virtual environment. This time we will use the basic VirtualBox, but you can also add VMWare Fusion (Workstation), AWS (EC2), Rackspace, etc. with plugins.

3. What is Provisioning?

__ Install and configure software such as "Web server (Apache or Nginx), programming language (PHP, Python, Ruby ,,, etc), DBMS (Posgre or MySQL ,,, etc)" on the virtual machine __ ,is.

This time we will use __shell script __ according to the official documentation, but you can also use other provisioning tools such as Ansible, Chef, and Puppet.

4. What is a Box file?

An image file that is the basis for starting a virtual machine. You can think of it as a virtual machine template __.

5. What is Vagrant cloud?

Vagrant clooud is the officially prepared Box file public site. Until I read @ alfa's Qiita article "How to get started with Vagrant", I also [vagrantbox.es](http://www.vagrantbox. I dropped it from an unofficial Box file site called es /). It was a good opportunity to read the official Vagrant documentation. Thank you very much!

6. What is Vagrantfile?

__ A file that describes the construction of a virtual machine (specs of the virtual machine to be built using Vagrant, specification of provisioning tool, etc.) __.

7. What is the Vagrant command?

Terminal It is a command that makes it easy to operate Vagrant above.

Summary so far

Virtualization is possible with VirtualBox alone, but by using Vagrant __ "Vagrantfile" you can easily set / share the virtual environment setting information, and by executing it with the "Vagrant command", you can build the environment. It's __ that can be very easy.

[Preparation # 2] Installing VirtualBox and Vagrant

Install VirtualBox

First, go to Oracle's VirtualBox Official Page and go to

Click Downloads on the left to download the one that suits your environment!

After the installer opens, if you answer all affirmatively, the installation will be completed successfully.

Install Vagrant

Go to Vagrant's official page and click the Download button!

Select the one that suits your environment and click the Download button! The dmg file will be downloaded, so click on it!

Double-click Vagrant.pkg to launch the installer. You can make a selection by clicking "Continue".

This completes the installation of VirtualBox and Vagrant! We will finally proceed with reading the official document from the next!

[Official] Introduction to Vagrant

What is Vagrant

Vagrant is a tool for building and managing virtual machine environments (with a single business procedure). Focusing on easy-to-use workflows and automation, Vagrant reduces development environment setup time and boosts production parity.

Why Vagrant

Vagrant provides a portable (portable) work environment that is easy to configure (anyone on your team) and reproducible.

The environment is built on industry-standard technology and is managed by a single, consistent workflow that helps maximize the productivity and flexibility of your team.

Vagrant exerts its power by riding on the shoulders of various giants (standing on a combination of existing technologies).

Virtual machines are provisioned on top of VMware, AWS, or other providers, as well as virtual environments like VirtualBox. Industry-standard provisioning tools such as shell scripts, Chef, and Puppet can be used to automatically install and configure software on virtual machines.

For developers

Vagrant separates dependencies and sets of settings in a single, consistent disposable environment. In that case, you will not be unable to use the tools you are familiar with (eg editors, browsers, debuggers, etc.).

Once someone (including you) has created one Vagrantfile, all you have to do is type vagrant up and everything you need for your job will be installed and configured.

Other members of the team also create the development environment from the same settings, so regardless of whether you are working on Linux, Mac, or Windows OS, the team members reflect the same dependencies in the same environment, exactly the same. You will run the code set to.

With this, you can say goodbye to "phenomena (bug) that occur only in your own environment"!

For Operators / Dev ops

Vagrant provides a disposable environment and a consistent workflow for developing and testing infrastructure management scripts.

You can quickly test shell scripts, Chef cookbooks, Puppet modules, and anything else you use for your local virtual environment like VirtualBox or VMware.

And with the same configuration, you can test these scripts in a remote cloud environment (such as AWS or RackSpace) based on the same flow.

Throw away your custom scripts to recycle (AWS) EC2 instances, stop juggling SSH prompts to different machines, and use Vagrant instead to get your life back on track!

For designers

Vagrant will automatically set all the necessary proposals for your web application, so you can concentrate on your design work!

Once the developer has set up Vagrant, you don't have to worry about how to get the app to work again.

You no longer need to have other developers help you modify your environment for design testing. Designers just have to check the code, vagrant up and start designing!

For everyone

Vagrant is the easiest and fastest software designed to create a virtual environment! In the next chapter, we'll finally get into Vagrant's quick start guide!

[Official] Getting started

1. Quick Start

Premise

--You have Vagrant installed --You have VirtualBox installed

Ubuntu 18.04 LTS 64-bit. Start a virtual machine on VirtualBox

The Box file name is created by the author. If you drop it from Vagrant clooud, you don't need to mention the url.

    //Create Vagrantfile by specifying Box file
    $ vagrant init hashicorp/bionic64
    //VM launch
    $ vagrant up
    //Check the status of the VM
    $ vagrant status
    //ssh connection
    vagarnt ssh
    //Delete VM
    $ vagrant destroy

Just type the vagrant up command and it will prepare everything you need for your project (installation of dependent software, network, setup of synced files, etc.) so you can work comfortably on your own machine!

2. Install Vagrant

Install Vagrant for your OS environment

Make sure it is installed

Launch a terminal and type in one of the three commands below.

    $ vagrant
    $ vagrant -h
    $ vagrant -v

Caution Some OS distributions also include the vagrant package in their upstream package repository. In that case, stop installing using the package manager (of that distribution). (There may be problems due to package dependencies.) Please use the download page.

3. Initialize a Project Directory

Creating a Vagrantfile

(1) Specify the root directory of your PJ. Many of Vagrant's configuration options are relative to this root directory. (2) Describe the types of machines and resources required for your PJ. Also describe what software to install and how you want to access it in the same way.

--Create a directory

    //vagrant_getting_Create started directory
    $ mkdir vagrant_getting_started
    //vagrant_getting_Move to started directory
    $ cd vagrant_getting_started

--Initialize directory

    $ vagrant init hashicorp/bionic64

vagarant init (although it is a built-in function) takes a box filename and URL as arguments. You should now have a Vagrantfile in the same directory. This file is intended to be subject to version control, allowing new team members to use this vagrantfile to build their environment right away.

4. Install and Specify a Box

You can quickly clone a virtual machine using a base image (image file) in Vagrant. These base images are known in Vagrant as Box files. Also, specifying a Box file is one of the tasks that you always do after creating a Vagrantfile, and it is also the first thing you should do.

Install Box file (optional)

(Although this work is basically unnecessary with the command to be learned later) If you want to install the box file without creating the Vagrantfile, it may be useful, so I will explain the command to install the Box file.

    $ vagrant box add hashicorp/bionic64
    $vagrant box add username/box name

Box is stored globally for current users. From there, you can use the box file as an initial image in each PJ. (That means you're using the same Box file in two projects, and adding a file in one doesn't affect the other.) The box file part can be replaced with a URL or a local file path.

Use Box files

Open the Vagrantfile and specify the Box file name in config.vm.box. Even if the box is not added in advance, Vagrat will automatically download and add the box at startup. (That's why you basically don't need the Box addition work introduced above.)

You can also explicitly specify the version with config.vm.box_version or config.vm.box_url. (Both are not required.)

    Vagrant.configure("2") do |config|
      config.vm.box = "hashicorp/bionic64"
      config.vm.box_version = "1.0.282" #->optionnal:config.vm.box_version = "1.0.282"
      #config.vm.box_url = "https://vagrantcloud.com/hashicorp/bionic64"
    end

Find another Box

Find it in Vagrant cloud!

5. Boot an Environment --Let's launch a VM (virtual machine)

If you type the following command, a VM running on Ubuntu should start up in about 1 minute.

    $ vagrant up

[Supplement] Check the VM status with commands and GUI, respectively.

First, check the status with a command.

    $ vagrant status

Next, check that the VM is up in the GUI (Graphical User Interface). Rewrite the Vagrantfile so that VirtualBox will be launched at startup.

    //Uncomment two to enable GUI mode
    config.vm.provider "virtualbox" do |vb|
    #   # Display the VirtualBox GUI when booting the machine
    vb.gui = true
    #
    #   # Customize the amount of memory on the VM:
    #   vb.memory = "1024"
    end

This should automatically launch VirtualBox. However, it is recommended to run it with SSH connection (as you do in the official document) because it is easier to run with the usual terminal!

SSH connection

Let's make an SSH connection to the launched virtual machine.

    $ vagrant ssh

If you want to disconnect from the ssh connection, type ʻexit or logout`, or press ctrl + d.

After making an SSH connection, first make sure you have the Vagrantfile

    [vagrant@localhost ~]$ pwd
    /home/vagrant
    [vagrant@localhost ~]$ cd ../../
    [vagrant@localhost /]$ ls
    bin   dev  home  lib64       media  opt   root  selinux  swapfile  tmp  vagrant
    boot  etc  lib   lost+found  mnt    proc  sbin  srv      sys       usr  var
    [vagrant@localhost /]$ ls vagrant/
    Vagrantfile
    [vagrant@localhost /]$

※__warning! !! __ __ Don't do rm -rf / because it's a virtual machine! !! __ Because Vagrant shares the directory in / vagrant with the directory on the host that contains your Vagrantfile, which will also be deleted! The details of shared folders will be explained in the next section.

Destroy the virtual machine

Let's break the ssh connection and delete all the resources created when the machine was created.

    $ vagrant destroy
        default: Are you sure you want to destroy the 'default' VM? [y/N] y ->Enter y!
    ==> default: Forcing shutdown of VM...
    ==> default: Destroying VM and associated drives...
    
    //Check vagrant status
    $ vagrant status
    Current machine states:
    
    default                   not created (virtualbox)
    
    The environment has not yet been created. Run `vagrant up` to
    create the environment. If a machine is not created, only the
    default provider will be shown. So if a provider is not listed,
    then the machine is not created for that environment.
    
    //By the way, the Box file is not gone
    $ vagrant box list
    hashicorp/bionic64  (virtualbox, 1.0.282)

Delete the Box file as well

    $ vagrant box remove hashicorp/bionic64
    Removing box 'hashicorp/bionic64' (v1.0.282) with provider 'virtualbox'...

So far, you've learned all about building, operating, and destroying your environment with Vagrant. From the next section, you'll learn how to sync files between your local PC and your (guest) virtual machine.

6. Synchronize Local and Guest Files

I don't think many people want to edit files with a plain vim editor to which they are connected to SSH.

Vagrant will automatically sync your files between your local-Ssh destination guest PC. This allows you to run locally edited files in a virtual development environment.

By default, Vagrant shares your project directory (the directory where you created the Vagrantfile with vagrant init in 3. Initialize a Project Directory) to the / vagrant directory on your guest machine.

Let's check how the file sharing with the guest machine on the local PC is actually performed.

Let's see the behavior of shared files

First, ssh and change to the / vagrant directory.

    $ vagrant ssh
    $ cd ../../vagrant
    $ pwd
    ->/vagrant

Create a new hoge file in the / vagrant directory

$ touch hoge

I was able to confirm that the local project file also has a hoge directory!

$ exit
logout
$ ls
Vagrantfile hoge

With synced folders, you can continue to use your own editor on your host machine and have the files sync into the guest machine. With synced files

Next, let's create a file called fuga.html on the local machine and see if that file is synchronized with the VM.

First, create the file locally.

$ touch fuga.html
$ ls 
Vagrantfile hoge fuga

Next, edit the contents of fuga with your favorite editor.

fuga.html


    <p>Can you edit it properly?</p>

I was able to confirm that there is fuga.html, which is also the ssh destination!

    $ vagrant ssh
    $ cd ../../
    $ ls vagrant/
    Vagrantfile hoge fuga.html
    //fuga.Check the contents of html(You can also check with the view command.)
    $ cat vagrant/fuga.html
    <p>Can you edit it properly?</p>

In the next theory, let's learn how to use provisioning to install packages, users, and more.

7. Provision a Virtual Machine

In this section, let's use provisioning to turn the VM into a web server.

Create an HTML directory locally

Create an html directory in ~ / Desktop / vagrant_test.

$ mkdir html

Next, create an index.html file in the html directory.

~/Desktop/vagrant_test/html/index.html


    <!DOCTYPE html>
    <html lang="ja">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Vagrant</title>
    </head>
    
    <body>
      <h1>I tried Vagrant for the first time!</h1>
    </body>
    
    </html>

Let's write a provisioning script!

In this tutorial, we will use a shell script to set up Apaceh (as a web server). First, save the following shell script as bootstrap.sh.

bootstrap.sh


    #!/usr/bin/env bash
    
    apt-get update
    apt-get install -y apache2
    if ![ -L /var/www ]; then
      rm -rf /var/www
      ln -fs /vagrant /var/www
    else
    fi

This script will download and launch Apache for you.

It also creates a symlink between the "directory of the files to be synced" and the "where the Apaceh server looks for content to provide as a response".

[Supplement] Shell script

--__ #! Script after __

The script after #! On the first line is called Shebang and specifies an interpreter (a program like a translator that interprets and executes the source code). In other words, this time, I'm telling the computer, "Please interpret this as a bash script." There is no problem with bin / bash. The explanation of the link destination is easy to understand!

apt-get is a package management system for Debian. (Ubuntu I'm using this time is also a type of Linux called Debian.) It's like Homebrew on Mac. Starting with Ubuntu 14.04, the "apt" command is recommended instead of "apt-get". As you can see in the explanation of the link, the apt command is basically easier for users.

--About scripts after __if __

I don't know the shell script! It seems to be, but the point is that you can think that the following if statement is written. Reference: [Shell script] How to use if for conditional branching!

if [condition 1]; then Processing in case of condition 1 elif [condition 2]; then Processing in case of condition 2 else Processing when neither of conditions 1 and 2 fi

Condition 1 is ![-L / var / www], but -L means "if file exists and is a symbolic link, it is true", and! Is logical negation ( ! true is synonymous with false).

In other words, if the file does not exist in the / var / www directory, the process will be executed.

From the above, this shell script is

The first time, "Since the symbolic link / var / www does not exist (after deleting the normal / var / www directory if it exists), create / var / www as the symbolic link of / vagrant."

And the second time, "Since there is a symbolic link / var / www, run the processing after else (nothing this time)"

I found out that it would do that.

Then start bootstrap.sh.

Set Vagrant to boot bootstrap.sh

Let's set up Vagrant so that this script will be launched when we set up the VM!

Add the following config.vm.provision line to your Vagrantfile.

 Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.provision :shell, path: "bootstrap.sh"
end

Vagrant will now use bootstrap.sh as a shell provisioner.

Provision a web server

If you haven't started the virtual machine yet, just type vagrant up and Vagrant will automatically provision it using bootstrap.sh.

However, even if you have already started the virtual machine, it will work without problems if you reload it as shown below. (Provisioning is done only at the first vagrant up, so you can't do it again with vagrant up.)

$ vagrant reload --provision

Let's get the HTML file using the wget command in the ssh destination!

$ vagrant ssh
$ wget -qO- 127.0.0.1
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Vagrant</title>
</head>

<body>

Vagrant I started Ngo!
</html>

[Supplement] What is the wget command?

According to the Atmark IT / Linux basic command, wget is a command that "downloads a file by specifying a URL". -q (--quiet) is an option to "do not output progress message" -O file (--output-document = file) saves the downloaded file to the destination specified as file. (Similar to specifying the save destination by redirect.) Also, if you specify - as file, the file will be output as standard.

This works because I'm doing "Install Apache" and "Set the / vagrant directory as the Apache Document root" in the shell script I've already set up.

[Supplement] Apache document root

By default, the Apache web server is set up in the / var / www directory with files and data that it manages and returns as a response to the web browser. (The directory under this management is read as the Document root.) This time, / var / www is a symbolic link of / vagrant, so if you mess with the contents of / vagrant, the changes will be reflected in the files under the Document root of Aapche.

The next tutorial will cover networks. Let's access the website provided by the guest machine by throwing a port to the guest machine.

If the provisioning script becomes too complex

For provisioning scripts, it's more efficient to package a custom Vagrant box with pre-installed packages instead of building it every time. See here for details.

8. Configure the Network

With port forwarding, you can specify a port on the guest machine and share (files) through the port on the host machine.

By setting the port forwarding, you will be able to access the Apache web server on the guest machine. Add the line config.vm.network to the Vagrantfile,

 Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/bionic64"
  config.vm.provision :shell, path: "bootstrap.sh"
  config.vm.network :forwarded_port, guest: 80, host: 4567
end

Let's reload.

$ vagrant reload

After the virtual machine reboots, try accessing http://127.0.0.1:4567.

That's why I was able to access it safely!

By the way, Vagrant has other network forms, such as assigning a static IP address to a guest machine or bridging a guest machine over an existing network. If you are interested in other network types, please read the networking page.

9. Share an Environment

In addition to providing a development environment, Vagrant makes it easy to share and collaborate on these environments. This basic function is called Vagrant Share.

Vagrant Share is a plugin. This plugin allows you to share your Vagrant environment over the net with anyone in the world. Vagrant Share provides URLs that route directly to your Vagrant environment from any internet-connected device around the world.

Premise

You have ngrok installed → I want to show the site created in the local environment via the net! A tool to use at times.

Install the plugin

Install Vagrant Share

$ vagrant plugin install vagrant-share

Share the environment

    $ vagrant share

Open a web browser and visit the URL you output! The page that Apache has already created should load! (If you modify the file and reload the URL, it will be reflected!) This page can be accessed from any device in the world!

Don't forget to end the session at the end.

$ ^C
==> default: Halting Vagrant share!

You should no longer be able to see the page after reloading the URL!

If you want to know more about Vagrant Share, click here. ](Https://www.vagrantup.com/docs/share) In the next chapter, let's learn about the teardown process!

10. Teardown an Environment

Here, we will actually create the virtual environment that we have built and modified in the previous chapters.

(1) Suspend [pause] vagrant suspend (2) Complete stop [shutdown] vagrant halt (3) Delete vagrant

Let's do it. Which option (1) to (3) to use depends on how long you want to stop the virtual environment you use for work and how quickly you want to restart the virtual environment. (Of course, it takes time to restart in the order of pause → stop → delete.)

If you haven't launched Vagrant yet, please do so.

$ vagrant up
$ vagrant status
->default                   running (virtualbox)

(1) Let's suspend the virtual machine

First, let's suspend the virtual machine using vagrant suspend.

$ vagrant suspend
==> default: Saving VM state and suspending execution...
$ vagrant status
->default                   saved (virtualbox)

Suspend a virtual machine with the vagrant suspend command allows you to quickly suspend and restart a VM.

By setting vagrant up, the VM will start again in the state before stopping.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/bionic64' version '1.0.282' is up to date...
==> default: Resuming suspended VM...
##... output truncated ...

(2) Let's stop the virtual machine completely

First, let's suspend the virtual machine with vagrant halt.

$ vagrant halt
$ vagrant status
->default                   poweroff (virtualbox)

This will safely shut down the guest machine's OS and power down the VM itself. The contents of the disc are also well preserved. Let's reboot for confirmation.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/bionic64' version '1.0.282' 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...
##... output truncated ...

However, restarting a completely stopped virtual machine takes some time because it wakes up from cold boot, and it consumes disk space even when it is stopped.

(3) Let's delete the virtual machine

Deleting a virtual machine with vagrant destroy removes all traces of the guest machine from your local machine. Regain disk space and RAM (consumed by the guest machine) by shutting down and powering down the guest machine.

$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

Enter "y" (yes) at the prompt after the vagrant destroy command as described above.

You can also start the virtual machine again by typing vagrant up. However, it takes time to start up, and the previous state has been discarded.

11. Rebuild an Environment

No matter how long you leave the project (with Vagrant), you can immediately spin up your environment (start your machine).

The method is simple.

$ vagrant up

that's all! The Vagrant environment is all set up via Vagrantfile. So development members can always just type the vagrant up command to have Vagrant recreate your working environment.

This is the end of the whole life cycle of building an environment with Vagrant!

In the next final chapter, we'll talk about providers other than VirtualBox!

12 .Explore other Providers

Install a new provider

Please refer to each official document for the settings for each provider!

Start with a new provider

If you can install a new provider, just type the following command and there is no problem. (No need to mess with Vagrantfile!)

$ vagrant up --provider=vmware_desktop

Once you specify another provider and do vagrant up as above, other Vagrant commands will automatically use the specified provider!

So, for example, when you're ready to connect to an SSH or delete a virtual machine (as you did in previous chapters), just type the command normally and you're good to go.

If you would like to learn more about providers, please refer to this page (link to the official English document).

Recommended Posts

[Introduction with official documentation] Vagrant tutorial (with translation + thorough explanation)
[Translation] scikit-learn 0.18 Tutorial Introduction of machine learning by scikit-learn