Practice of building development environment by Vagrant

Keywords: vagrant


Development Environments Made Easy
- Official Website Title

vagrant is a command line hypervisor. It is used to simplify the construction of development environment.

vagrant is developed based on Chef using ruby language. Work with other virtual machine programs, such as vmare, virualbox, hyper-v, etc. And manage all functions of the virtual machine through CLI and VagrantFile (configuration file). vagrant uses its own image format for environment deployment, that is, box. It is similar to the image in iso or docker of virtual machine.

Choose the combination of vagrant+virualbox because they are open source and cross platform. Vagrant is simple and powerful to build a development environment.


Download and install from the official website. virualbox can be installed optionally
VirtualBox Extension Pack. Using the virtual machine requires the BIOS to turn on virtualization.

VirtualBox official website

Vagrant official website

BOX search

Several scenes

Here are several usage scenarios of vagrant. All steps have been verified under WIN11 system.

1. One line command to build a basic development environment

Start a virtual machine containing centos system under CMD command line. Where "centos/7" is the box name, and the default password of root user is vagrant. That's easy.

mkdir demo && cd demo && vagrant init centos/7 && vagrant up

2. Stand alone development environment

The goal is to build a small amount of customized stand-alone development environment. You need to use CLI commands and configuration files to do this.

Manually download BOX

The network speed is slow. You can download the box file yourself and add it to the local box library.

Download address:

Command to add box to local library

vagrant box add centos/7

**Initialization * * enter the command line, create a directory, and initialize with centos/7 box.

md demo
cd demo
vagrant init centos/7

After the basic configuration file is initialized, a file named Vagrantfile will be generated in the current directory. In Ruby language format.

Vagrant.configure("2") do |config| = "centos/7" #box name
  config.vm.hostname="base" #hostname in the virtual machine operating system "private_network", type: "dhcp" #Use private networks to automatically assign IP addresses "private_network", ip: ""    #Static IP can also be configured
  config.vm.provider "virtualbox" do |vb|
    vb.gui = false #Do not open GUI on startup
    vb.memory = "4096" #Memory limit 4096MB

Common commands

vagrant up #Start the virtual machine
vagrant status #View virtual machine status
vagrant ssh #Linked virtual machine
vagrant halt #Shut down the virtual machine
vagrant destroy #Delete virtual machine
vagrant reload #Restart the virtual machine to make the modified configuration effective.

3. Cluster development environment

The goal is to build a cluster environment running in the LAN, including multiple physical machines and virtual machines.

The idea is to customize the public box after it is installed. Package it into a custom box and distribute it to different physical machines for deployment.

Using vagrant's public network mode, each virtual machine is assigned a fixed IP in the LAN, and any physical machine and virtual machine in the cluster environment can communicate directly.

Initialize a centos environment

md test
cd test
vagrant init centos/7

Custom environment

Allow user name password link SSH

Linking virtual machines using vagrant ssh

vagrant ssh

su #Switch the root user with the password vagrant

vi /etc/ssh/sshd_config #Edit ssh profile

Modify configuration items, allow password login, and save.

PasswordAuthentication yes

service sshd restart #Restart ssh service

Operating system settings

#Modify time zone
timedatectl set-timezone Asia/Shanghai
#Turn off firewall
systemctl stop firewalld
systemctl disable firewalld
#Turn off Linux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
#Close swap 
swapoff -a #Temporarily Closed
sed -ri 's/.*swap.*/#&/' /etc/fstab #Permanent shutdown
free -g #Verify that swap must be 0

Set alicloud Centos image source

wget -O /etc/yum.repos.d/CentOS-Base.repo
yum clean all
yum makecache
yum update -y

Install Docker

#rely on
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo
yum list docker-ce --showduplicates | sort -r
yum install docker-ce-18.03.1.ce-1.el7.centos  -y
systemctl start docker
systemctl enable docker
#Set alicloud docker image source

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
  "registry-mirrors": [""]

systemctl daemon-reload
systemctl restart docker

Custom BOX

Package the current environment. The default name is Join the local library and name it base.

Box default path of local warehouse C:\Users\Administrator.vagrant.d\box

vagrant package
vagrant box add **base **

configuration file

Vagrant.configure("2") do |config|

  #Virtual machine 1 
  config.vm.define "node201" do |node1| = "base" #Use a custom box
    node1.vm.hostname = "node201" "public_network", ip: ""

  #Virtual machine 2
  config.vm.define "node202" do |node2| = "base"
    node2.vm.hostname = "node202" "public_network", ip: ""

  # config.vm.define "node203" do |node3|
  # = "base"
  #   node3.vm.hostname = "node203"
  # "public_network", ip: ""
  # end

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "4096"
    vb.cpus = 4


Deploy cluster environment

Copy the packaged custom box and configuration file to each physical machine to be deployed, and install the vagrant environment.

Add the box to the local library of the physical machine through vagrant box add.

Adjust the vagrant file and deploy it through vagrant init.


Extended reading vagrant CLI, VagrantFile, plug-in, network configuration, etc. can be found on the official website.

Documentation | Vagrant by HashiCorp (

Posted by mikep on Sat, 30 Oct 2021 01:09:26 -0700