Skip to main content

Getting started

Basic step​

  • Init vagrant vagrant init hashicorp/bionic64
  • Up vagrant vagrant up
  • Ssh to the machine vagrant ssh
  • Leave ssh logout
  • Stop the machine vagrant suspend (the virtual machine will still use disk space while suspended)
  • Shutdown the machine vagrant halt
  • When you want to delete the machine vagrant destroy it will delete the machine

Local and Guest files​

  • By default, Vagrant shares your project directory (the one containing the Vagrantfile) to the /vagrant directory in your guest machine.
tip

vagrant up, and vagrant ssh you will see the files at /vagrant

Provisioning Virtual Machine​

  • Write a provisioning script bootstrap.sh
Vagrant.configure("2") do |config|
config.vm.box = "hashicorp/bionic64"
config.vm.provision :shell, path: "bootstrap.sh"
end
  • Configure Vagrant to run this shell script when setting up the machine
tip

For complex provisioning scripts, it may be more efficient to package a custom Vagrant box with those packages pre-installed instead of building them each time. Learn more in the packaging custom boxes documentation.

  • To run provisioning script
vagrant reload --provision

Configure Port Forwarding​

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
  • Reload so that these changes can take effect vagrant reload

Share the environment​

  • If you want to share environment , install the plugin vagrant share

Error​

warning

The character device /dev/vboxdrv does not exist. Please install the virtualbox-dkms package and the appropriate headers, most likely linux-headers-generic

sudo apt-get install virtualbox-dkms
sudo dpkg-reconfigure virtualbox-dkms
sudo dpkg-reconfigure virtualbox

https://stackoverflow.com/questions/13291015/how-to-correct-virtualbox-installation

caution

Vagrant does not work with Virtualbox 6.1