Vagrant is a virtual machine creation tool that can quickly install a local virtual machine in the same way as Docker and cloud servers can quickly install a system instead of just adding an image file to VirtualBox and then installing the system step by step

Install the software

In addition to vagrant itself, you need to install VirtualBox (you can also use other virtual machine software, such as vmware).

Install VirtualBox

Go to the VirtualBox home page and click the big download button to enter the download page.

VirtualBox is a cross-platform virtualization tool that supports multiple operating systems (oss). Download it based on your requirements.

Note that in addition to the main program, the corresponding extension package is also downloaded. Some advanced features, such as USB 3.0, require expansion pack support.

After installing the main program, double-click the extension package file to install the extension package.

Don’t close the download page, we’ll need it later.

Install the Vagrant

Download the latest version from Vagant and choose the version for your operating system.

Note that Vagrant does not have a graphical interface, so there are no desktop shortcuts once installed. The specific usage method will be explained in detail next.

Vagrant’s installer automatically adds the installation PATH to the PATH environment variable, so you can check for success by executing Vagrant version on the command line:

Vagrant Version Installed Version: 2.2.7 Latest Version: 2.2.8

Configure the VM storage location

Vm creation occupies a large amount of disk space. In Windows, the default vm creation location is on drive C. Therefore, you are advised to configure the vm elsewhere. There are two things that need to be configured, one is the virtual machine itself and the other is the system image

Configuration VirtualBox

After starting VirtualBox, choose Menu Management > Global Settings, or press Ctrl + G to change the default virtual machine location in the Global Settings dialog box and specify a disk with a large capacity.

Configuration Vagrant

Creating a virtual machine with Vagrant requires an initial import of image files, known as boxes, which are stored by default in the.vagrant.d directory in the user directory, which for Windows is C:\Users\ username \.vagrant.d

If you may need a lot of images later, or if you are short of space on drive C, you can set this directory by setting the environment variable VAGRANT_HOME.

On Windows, create a new system environment variable named VAGRANT_HOME with the value E:\ virtualbox.vagrant.d

Note that the last.vagrant.d directory name is not required, but it is recommended to keep it consistent so that you can see at a glance what the directory is for.

Some common commands

View the Box list

A box is similar to a system image

$ vagrant box list
Copy the code

Add the box

Is to add a system image

$Vagrant Box add imageCopy the code

Creating a VM

Create a VM in the current folder

$Vagrant Init imageCopy the code

Starting a VM

Start the vm in the current folder

$ vagrant up
Copy the code

Connect to the SSH

$ vagrant ssh
Copy the code

Viewing SSH Configurations

$ vagrant ssh-config
Copy the code

View the VM status

$ vagrant status
Copy the code

Stop a VM.

$ vagrant halt
Copy the code

Suspending a VM

$ vagrant suspend
Copy the code

Restore a suspended VM

$ vagrant resume
Copy the code

Note: Vagrant up can be executed to bring the virtual machine back up regardless of whether it is in a shut down, paused, or even error state.

Restart the VM.

$ vagrant reload
Copy the code

Deleting a VM

$ vagrant destroy
Copy the code

Resources zhuanlan.zhihu.com/p/259833884 https://www.bilibili.com/video/BV1F5411e7EL