Cancel

Vagrant command in Linux

A vagrant is a tool for building and managing Virtual Machines locally. VirtualBox or any other tool is required to run Vagrant Box.

Linux (Debian, Ubuntu, Mint)

1
sudo apt install virtualbox vagrant

MacOSx (don’t forget to install Homebrew if you don’t have one)

1
brew install --cask virtualbox vagrant

Windows (don’t forget to install choco if you don’t have one)

1
choco install virtualbox vagrant

Alternatives links for Windows Users:

Run the following commands to check whether applications are running properly or not.

1
2
vagrant --version
VBoxManage --version

You can search vagrant boxes from here and run the following command once you are done with your searching.

1
2
3
4
5
6
7
8
vagrant init BOX_NAME

# for example
vagrant init ubuntu/bionic64 # OR
vagrant init archlinux/archlinux # OR

# You can even install specific version.
vagrant init centos/8 --box-version 1905.1

Run the following command to start and provision the vagrant environment.

1
vagrant up

and turn off your machine via vagrant halt.

Vagrantfile basically is your configuration file, where you can adjust your machine’s resources like RAM, Processor, disk space, and so on.

Here is the concise version of Vagrantfile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.hostname = "local"
  config.vm.box_check_update = false
  config.vm.synced_folder ".", "/vagrant", disabled: true

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.cpus = 1
    vb.name = "yafiz"
    vb.memory = 1024
  end

  if Vagrant.has_plugin?("vagrant-disksize")
    config.disksize.size = '10G'
  end
end

When you change anything inside your configuration file, then you will have to use the following command, which will load your new changes on your machine, and restart the machine for you too.

1
vagrant reload

You can also remove the virtual machine from your system by using the following commands. So -f option means force to remove, which will turn off your machine first if running, and then will remove it.

You can also specify the machine’s ID but it is optional.

1
2
3
4
vagrant destroy <MACHINE_ID> # only specific must be removed

vagrant destroy # ask before deleting the virtual machine
vagrant destroy -f # don't ask, just remove the virtual machine

Vagrant provides lots of plugins too, which can be found here. Plugins are not built by hashicorp itself or supported in any way, but installing these plugins are really helpfull.

Run following command to install plugins and reload the machine after plugin is installed.

1
2
3
4
5
vagrant plugin install <PLUGIN_NAME> <PLUGIN_NAME_1> ...

# for example.
vagrant plugin install vagrant-disksize
vagrant plugin install vagrant-vbguest

Run the following command to check, how many plugins are already installed on your local system.

1
vagrant plugin list

If you don’t need a plugin anymore, remove it via.

1
vagrant plugin uninstall <PLUGIN_NAME>

Current machine status can be seen from the following command whether your machine is off or on, following command will work only if you are inside the folder where the vagrant file exists.

1
vagrant status

If you are outside the folder where the vagrant file exists then run the following to list down all machine’s statuses at once, where you will find Machine’s ID as well.

1
vagrant global-status

Now let’s login to your machine via SSH. If you are inside the folder where a vagrant file exists.

1
vagrant ssh

If you are outside the folder where a vagrant file exists then use Machine’s ID to login.

1
vagrant ssh <MACHINE_ID>

Run the following command to output the SSH configuration file of the running Vagrant machine such as Username, IP Address, private key and etc.

1
vagrant ssh-config

If you wish to add, upgrade, or remove your vagrant boxes from your local system, then run the following commands.

1
2
3
4
vagrant box add <name> <url> # Download a box or image onto your computer (https://app.vagrantup.com/boxes/search)
vagrant box list # Listdown all installed boxes
vagrant box outdated # Updates outdated boxes
vagrant boxes remove <name> # Deletes a box from the machine