Installing vagrant on MacOS

in Tutorials with 1 comment

Sometimes, we need to do some software installation tests. It's not good to run them directly on the server. So we need to simulate the next environment first.

The simulation environment here needs to be the same as the online environment, such as the system version, bits and other software conditions. If the hardware conditions cannot be met, it is not necessary to see the requirements.

It is recommended to use vagrant to install virtual machine to simulate the online system environment. The tutorial is based on MacOS.

Agent installation acceleration

Open the terminal

 export http_ proxy=http://127.0.0.1:1087;export https_ proxy=http://127.0.0.1:1087;

This is related to the configuration of SS and needs to be checked by yourself

Installing vagrant

Homebrew needs to be installed in advance

 brew cask install virtualbox vagrant vagrant-manager

During installation, the following may occur:

==>Installer: the install failed. Contact the software manufacturer for assistance. )
Error: Command failed to execute!

You need to go to System Preferences > Security and privacy > allow, and then re install

Use of vagrant

Command list:

command Effect
vagrant up Start local environment
vagrant halt Shut down local environment
vagrant suspend Pause local environment
vagrant resume Restore local environment
vagrant reload Modify the vagrantfile to make it effective (equivalent to halt first and then up)
vagrant ssh Log in to the virtual machine of the local environment through SSH
vagrant destroy Completely remove local environment

Check the information about the system you need to simulate

 cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

getconf LONG_ BIT
64

Check for virtual machines

 vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.

Add a new virtual machine centos/7 , select 3 during installation

 vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_ desktop

Enter your choice: 3
==> box: Adding box 'centos/7' (v1803.01) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1803.01/providers/virtualbox.box
==> box: Successfully added box 'centos/7' (v1803.01) for 'virtualbox'!

Check whether the installation is successful

 vagrant box list
centos/7 (virtualbox, 1803.01)

Initial configuration

Create directory configuration

 cd ~
mkdir Vagrant && cd Vagrant
mkdir centos7 && cd centos7

Initialize CentOS / 7

 vagrant init centos/7
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.

ll
total 8
-rw-r--r--  1 air  staff   2.9K  4 12 10:55 Vagrantfile

Start CentOS / 7

 vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos7_ default_ 1523501887980_ 60805
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
     default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within  guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default:  additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly, 
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/air/Vagrant/centos7/ => /vagrant

notice Machine booted and ready! That means the startup is successful

View running status

 vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

At this point, we can open the VirtualBox and see that CentOS / 7 is already running

 1523502372489-62813238-a01a-4eeb-8f41-9dfcf77e30a9-image.png

Virtual machine usage

SSH landing system

 vagrant ssh
-bash: warning: setlocale: LC_ ALL: cannot change locale (zh_ CN.UTF-8)
-bash: warning: setlocale: LC_ ALL: cannot change locale (zh_ CN.UTF-8)
/bin/sh: warning: setlocale: LC_ ALL: cannot change locale (zh_ CN.UTF-8)
[vagrant@localhost ~]$

Enter the system, but there is warning. The solution is as follows:

 sudo echo "LC_ ALL=zh_ CN.UTF-8" >> /etc/environment
sudo echo "zh_ CN.UTF-8 UTF-8" >> /etc/locale.gen
sudo echo "LANG=zh_ CN.UTF-8" > /etc/locale.conf
sudo localedef -v -c -i zh_ CN -f UTF-8 zh_ CN.UTF-8

ctrl + d Exit SSH and log in again vagrant ssh

 vagrant ssh
Last login: Thu Apr 12 03:53:37 2018 from 10.0.2.2
[vagrant@localhost ~]$

cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

After successfully entering the system, you can do whatever you want

Virtual machine shutdown

before ctrl + d Exit SSH and use vagrant halt Turn off the virtual machine

 vagrant halt
==> default: Attempting graceful shutdown of VM...

Check whether the shutdown is successful

 vagrant status
Current machine states:

default                   poweroff (virtualbox)

The VM is powered off. To restart the VM, simply run `vagrant up`

Update vagrant

 brew cask reinstall vagrant

# this might take a long time
vagrant plugin update

That's the end of the tutorial.

Responses
  1. wwaf

    Homeboy benefits: https://6wll.com

    Reply