Easily build virtual machines with Vagrant

If you want to try out Linux you care about, I think you will use a virtual machine such as VirtualBox. If it is an experimental or temporary environment, that is enough, but if you use a virtual machine as a software execution environment or development environment, the work required to build the environment may be troublesome.

If you want to return to the initial state when the environment gets dirty to some extent, or if you want to prepare another environment, you can use Vagrant to automatically proceed with the environment construction procedure, so you can save time and effort. I can

install

Download and install the Vagrant and Virtual Box packages from the URL below

Choose Box

Find the OS image (BOX) you want from the Vagrant Cloud site

https://app.vagrantup.com/boxes/search

There are boxes made by various people, and there are good and bad things, but it will be safe to choose the following ones

init

Download the published image to your PC and initialize the virtual machine. Execute the vagrant init command with the name of the Box found in Vatrant Cloud.

Open a terminal (PowerShell for Windows) and run the command

Image download and initialization


#Create a working directory before execution and work in it
> mkdir vagrant
> cd vagrant

#Ubuntu as an example/Download xenial
> vagrant init ubuntu/xenial64

A Vagrantfile will be created where you ran it

Execution result


> vagrant init ubuntu/xenial64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Editing Vagrantfile

When you want to set the virtual machine you want to prepare as needed, edit the generated Vagrantfile. If you make a mistake and want to return to the initial state, you can delete the Vagrantfile and execute vagrant init <Box name> again to start over.

See https://www.vagrantup.com/docs/vagrantfile for a detailed description of Vagrantfile. Most of the items are tied to the VirtualBox setting operation, so you can feel the necessary parts.

Run with the --provision option to apply the modified Vagrantfile

#If the virtual machine is not started
> vagrant up --provision

#When restarting a running virtual machine and applying it
> vagrant reload --provision

Start-up

Run vagrant up in the directory where your Vagrantfile is located

> vagrant up

#If you have multiple virtual machines installed`--provider`Explicitly run as an option
> vagrant up --provider virtualbox

ssh

Run vagrant ssh to manipulate the shell of the started virtual machine

> vagrant ssh

If you have set port forwarding, you can also operate with ssh connection by connecting to port 2222 (default state). You can log in below

Stop

To stop the started virtual machine, run vagrant halt in the directory containing the Vagrantfile.

> vagrant halt

Other commands

I have summarized the commands that I often use

command motion Note
vagrant box add <Box name, URL, or path> Download Box
vagrant box remove Delete the downloaded Box
vagrant box list Display a list of downloaded Boxes
vagrant init <Box name, URL> Virtual machine initialization Vagrantfile is created in the executed directory
vagrant ssh Open the ssh shell of the started virtual machine Run in the directory containing the Vagrantfile
vagrant up Start the virtual machine Run in the directory containing the Vagrantfile
vagrant halt Stop the virtual machine Run in the directory containing the Vagrantfile
vagrant reload Reboot the virtual machine Run in the directory containing the Vagrantfile
vagrant destroy Delete virtual machine Run in the directory containing the Vagrantfile
vagrant package Packaging virtual machines(Output in Box format)To do

tips

File save destination

Normally, you can delete it with the above vagrant command, but if you have uninstalled Vagrant, you can open the following location and delete the file.

Downloaded Box

~/.vagrant.d/boxes

~ is the user's home directory ( C: \ users \ username on Windows)

Virtual machine related files

~/VirtualBox VMs

Increase the version of Guest Additions

If the version of Additions installed in the virtual machine is old and does not work, you can increase the version with the following command.

> vagrant plugin install vagrant-vbguest

Create a shared folder that can be read and written in both directions

Add the following description to the Vagrantfile. If you update Guest Additions by the above procedure, you will have less trouble.

config.vm.synced_folder "./output", "/vagrant/output", type:"virtualbox"

Recommended Posts

Easily build virtual machines with Vagrant
Build python virtual environment with virtualenv
Easily build a development environment with Laragon
Build a python virtual environment with pyenv
[Memo] Build a virtual environment with Pyenv + anaconda
Build a virtual environment with pyenv and venv
Easily build HPC on AWS with genuine AWS Cfn Cluster
Easily daemonized with Supervisor
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python virtual environment with virtualenv and virtualenvwrapper
Easily beep with python
Build python3.x with pyenv
Virtual environment with Python 3.6
Easily take C program coverage with configure's build options!
Build a Django environment for Win10 (with virtual space)
How to build a python2.7 series development environment with Vagrant
Build a LAMP environment with Vagrant (Linux + Apache + MySQL + PHP)
Easily build network infrastructure and EC2 with AWS CDK Python
Easily serverless with Python with chalice
Build an Anaconda virtual environment
Easily write if-elif with lambda
Build python3 environment with ubuntu 16.04
Build python environment with direnv
Let's build git-cat with Python
Switch virtual environment with jupyter
Easily cProfile with a decorator
Easily build a natural language processing model with BERT + LightGBM + optuna