Vagrant VM provision & vagrant commands cheathsheet


Vagrant.configure("2") do |config|
  # = "debian/bullseye64" = "ubuntu/focal64" # v 20.04 LTS
  config.vm.synced_folder "./", "/vagrant" "forwarded_port", guest: 8080, host: 8080
  config.vm.provision :shell, path: ""
  config.vm.provider "virtualbox" do |vb| = "UbuntuBionic"     
    vb.memory = 2048
    # vb.gui = true

Vagrant provision script

Create a file in the same folder of Vagrantfile, with the following content. Just remove the tools not required, here’s all I’ve ever needed.

Make sure to check for latest versions of some of the installed tools. Here’s

echo "provisioning VM..."
sudo apt update
sudo apt-get update

sudo apt-get install curl -y

# set environment variable
echo ENVIRONMENT_NAME=DEV >> /etc/environment

# install az cli
sudo curl -sL | sudo bash
sudo apt -y install gnupg2 pass
az upgrade
az --version
echo "azure cli installed..."

# install docker and run
# install docker and run
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
curl -fsSL | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli -y

sudo systemctl start docker
sudo systemctl enable docker
docker --version
echo "docker installed and running..."

# create group 'docker' and add user 'vagrant' (the one I connect with) to it
sudo usermod -aG docker vagrant
echo "docker group created and 'vagrant' user added to it..."

# install docker compose
sudo curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
echo "docker compose installed..."

# install node and npm
sudo apt install -y nodejs npm

# install dotnet sdk
wget -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y apt-transport-https
sudo apt-get update
sudo apt-get install -y dotnet-sdk-3.1
dotnet --version
echo "dotnet sdk 3.1 installed"

# install azure functions
curl | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64]$(lsb_release -cs)-prod $(lsb_release -cs) main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-get update
sudo apt-get install azure-functions-core-tools-3

# install minikube & run it with docker driver by default
curl -LO 
sudo install minikube-linux-amd64 /usr/local/bin/minikube
minikube config set driver docker
minikube version
echo "minikube installed and configured to use docker driver..."

# install kubectl
curl -LO`curl -s`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version --client
echo "kubectl installed and in PATH..."

# install istioctl
# Specify the Istio version that will be leveraged throughout these instructions
curl -sL "$ISTIO_VERSION/istio-$ISTIO_VERSION-linux.tar.gz" | tar xz
sudo cp ./bin/istioctl /usr/local/bin/istioctl
sudo chmod +x /usr/local/bin/istioctl
istioctl version --remote=false
echo "istioctl installed..."

# install helm
curl | bash
helm version
echo "helm installed..."

# install heroku cli
curl | sh
echo "heroku cli installed..."

# install mosquitto client
sudo apt install -y mosquitto-clients
echo "mosquitto-clients installed..."

## install java jdk8
apt-get install openjdk-8-jdk -y
export JAVA_HOME=/usr/lib/jvm/openjdk-8-jdk
export PATH=$PATH:$JAVA_HOME/bin
java -version
echo "java installed.."

## install scala
dpkg -i scala-2.13.6.deb
scala --version
echo "scala installed.."

## install sbt
echo "deb /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
apt-key adv --keyserver hkp:// --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
apt-get update
apt-get install sbt
sbt --version
echo "sbt installed.."

## install spark
cd /opt
tar -xvzf spark-3.1.1-bin-hadoop2.7.tgz
mv spark-3.1.1-bin-hadoop2.7 spark

echo 'export PATH=$PATH:$SPARK_HOME/bin:$SPARK_HOME/sbin' >> ~/.bashrc
echo 'export SPARK_HOME=/opt/spark' >> ~/.bashrc

echo "spark installed.."

Most used commands


vagrant box update

new vagrant file

vagrant init

check status

vagrant status
vagrant global-status

to manage VMs (boxes)

vagrant box

to run VM

local project folder is sync with /vagrant in VM

vagrant up

to ssh in

vagrant ssh

close ssh connection with Ctrl+d

to shut down the VM (without loosing everything)

vagrant halt

to destroy the VM (losing everything)

vagrant destroy

ssh into VM

vagrant ssh

vagrant VM ssh server is in host’s 2222 port, so putty can be used targeting localhost:2222 Also, vagrant VM’s 22 port can be forwarded to any other host’s port.

Other commands

vagrant init -- Initialize 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 ubuntu/trusty64.

vagrant up -- starts vagrant environment (also provisions only on the FIRST vagrant up)
vagrant resume -- resume a suspended machine (vagrant up works just fine for this as well)
vagrant provision -- forces reprovisioning of the vagrant machine
vagrant reload -- restarts vagrant machine, loads new Vagrantfile configuration
vagrant reload --provision -- restart the virtual machine and force provisioning

vagrant ssh -- connects 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.

vagrant halt -- stops the vagrant machine
vagrant suspend -- suspends a virtual machine (remembers state)
vagrant destroy -- stops and deletes all traces of the vagrant machine
vagrant destroy -f -- same as above, without confirmation
vagrant destroy <id>

vagrant box list -- see a list of all installed boxes on your computer
vagrant box add <name> <url> -- download a box image to your computer
vagrant box outdated -- check for updates vagrant box update
vagrant boxes remove <name> -- deletes a box from the machine
vagrant package -- packages a running virtualbox env in a reusable box

vagrant snapshot save [options] [vm-name] <name> -- vm-name is often default. Allows us to save so that we can rollback at a later time

vagrant -v -- get the vagrant version
vagrant status -- outputs status of the vagrant machine
vagrant global-status -- outputs status of all vagrant machines
vagrant global-status --prune -- same as above, but prunes invalid entries
vagrant provision --debug -- use the debug flag to increase the verbosity of the output
vagrant push -- yes, vagrant can be configured to deploy code!
vagrant up --provision | tee provision.log -- Runs vagrant up, forces provisioning and logs all output to a file