Skip to main content

Vagrant

Vagrant commands

Typing vagrant from the command line will display a list of all available commands.

Be sure that you are in the same directory as the Vagrantfile when running these commands!

Creating a VM

CommandDescription
vagrant initInitialize Vagrant with a Vagrantfile and ./.vagrant directory, using no specified base image. Before you can do vagrant up, you'll need to specify a base image in the Vagrantfile.
vagrant init <boxpath>Initialize Vagrant with a specific box. To find a box, go to the public Vagrant box catalog. When you find one you like, just replace it's name with boxpath. For example, vagrant init almalinux/9.

Starting a VM

CommandDescription
vagrant upstarts vagrant environment (also provisions only on the FIRST vagrant up)
vagrant resumeresume a suspended machine (vagrant up works just fine for this as well)
vagrant provisionforces reprovisioning of the vagrant machine
vagrant reloadrestarts vagrant machine, loads new Vagrantfile configuration
vagrant reload --provisionrestart the virtual machine and force

Getting into a VM

CommandDescription
vagrant sshconnects to machine via SSH
vagrant ssh <boxname>If you give your box a name in your Vagrantfile, you can ssh into it with boxname. Works from any directory.

Stopping a VM

CommandDescription
vagrant haltstops the vagrant machine
vagrant suspendsuspends a virtual machine (remembers state)

Cleaning Up a VM

CommandDescription
vagrant destroystops and deletes all traces of the vagrant machine
vagrant destroy -fsame as above, without confirmation

Boxes

CommandDescription
vagrant box listsee a list of all installed boxes on your computer
vagrant box add <name> <url>download a box image to your computer
vagrant box outdatedcheck for updates vagrant box update
vagrant box remove <name>deletes a box from the machine
vagrant packagepackages a running virtualbox env in a reusable box

Tips

CommandDescription
vagrant -vget the vagrant version
vagrant statusoutputs status of the vagrant machine
vagrant global-statusoutputs status of all vagrant machines
vagrant global-status --prunesame as above, but prunes invalid entries
vagrant provision --debuguse the debug flag to increase the verbosity of the output
vagrant pushyes, vagrant can be configured to deploy code!
vagrant up --provision | tee provision.logRuns vagrant up, forces provisioning and logs all output to a file.

After destroying and cleaning up a vagrant project, you may need to manually cleanup:

rm -rf ~/.vagrant.d
warning

Even after vagrant destroy, vagrant often keeps some data cache. You may need to manually delete a box from ~/.vagrant.d/boxes/{BOX_NAME} before re-creating with the same name.

How to resize (expand) Vagrant/VirtualBox disk size?

Install vagrant-disksize plugin and set disk size:

Vagrantfile
Vagrant.configure(2) do |config|
config.vagrant.plugins = {
"vagrant-disksize" => {"version" => "0.1.3"},
}

config.disksize.size = "40GB"
...
end

Bring up the vagrant box:

vagrant up <boxname>

SSH to the box:

vagrant ssh <boxname>

List storage volumes:

[root@master-vagrant-cluster-001 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 40G 0 loop /export
loop1 7:1 0 40G 0 loop /cluster-001-share
loop2 7:2 0 40G 0 loop /backup
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 200M 0 part /boot/efi
├─sda3 8:3 0 1G 0 part /boot
└─sda4 8:4 0 18.3G 0 part /
[root@master-vagrant-cluster-001 ~]# parted /dev/sda print free
Warning: Not all of the space available to /dev/sda appears to be used, you can fix the GPT to
use all of the space (an extra 42926080 blocks) or continue with the current setting?
Fix/Ignore? Fix
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 2097kB 1049kB biosboot bios_grub
2 2097kB 212MB 210MB fat16 EFI System Partition boot, esp
3 212MB 1286MB 1074MB xfs boot
4 1286MB 21.0GB 19.7GB xfs root
21.0GB 42.9GB 22.0GB Free Space
[root@master-vagrant-cluster-001 ~]# parted /dev/sda resizepart 4 100%
Warning: Partition /dev/sda4 is being used. Are you sure you want to continue?
Yes/No? yes
Information: You may need to update /etc/fstab.
[root@master-vagrant-cluster-001 ~]# parted /dev/sda print free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number Start End Size File system Name Flags
17.4kB 1049kB 1031kB Free Space
1 1049kB 2097kB 1049kB biosboot bios_grub
2 2097kB 212MB 210MB fat16 EFI System Partition boot, esp
3 212MB 1286MB 1074MB xfs boot
4 1286MB 42.9GB 41.7GB xfs root
[root@master-vagrant-cluster-001 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 40G 0 loop /export
loop1 7:1 0 40G 0 loop /cluster-001-share
loop2 7:2 0 40G 0 loop /backup
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1M 0 part
├─sda2 8:2 0 200M 0 part /boot/efi
├─sda3 8:3 0 1G 0 part /boot
└─sda4 8:4 0 38.8G 0 part /

Finally,

xfs_growfs -d /

Vagrant plugin error after upgrading vagrant

Error message:

Vagrant failed to initialize at a very early stage:

The plugins failed to initialize correctly. This may be due to manual
modifications made within the Vagrant home directory. Vagrant can
attempt to automatically correct this issue by running:

vagrant plugin repair

If Vagrant was recently updated, this error may be due to incompatible
versions of dependencies. To fix this problem please remove and re-install
all plugins. Vagrant can attempt to do this automatically by running:

vagrant plugin expunge --reinstall

Or you may want to try updating the installed plugins to their latest
versions:

vagrant plugin update

Error message given during initialization: Unable to resolve dependency: user requested 'vagrant-cachier (= 1.2.1)'

To resolve this issue, rename (if you want to backup) or delete the existing .vagrant directory in the project root.

Resources