Vagrant VM provision & vagrant commands cheathsheet

Vagrantfile

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

Vagrant provision script

Create a file vagrant_provision.sh 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 vagrant_provision.sh

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 https://aka.ms/InstallAzureCLIDeb | 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 \
    lsb-release
    
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(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 containerd.io -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 https://github.com/docker/compose/releases/download/1.29.2/docker-compose-`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 https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -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
# https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=linux%2Ccsharp%2Cbash#install-the-azure-functions-core-tools
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-$(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 https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 
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 https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/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
ISTIO_VERSION=1.6.7
curl -sL "https://github.com/istio/istio/releases/download/$ISTIO_VERSION/istio-$ISTIO_VERSION-linux.tar.gz" | tar xz
cd istio-$ISTIO_VERSION
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 https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
helm version
echo "helm installed..."

# install heroku cli
curl https://cli-assets.heroku.com/install-ubuntu.sh | 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
wget www.scala-lang.org/files/archive/scala-2.13.6.deb
dpkg -i scala-2.13.6.deb
scala --version
echo "scala installed.."

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

## install spark
cd /opt
wget https://downloads.apache.org/spark/spark-3.1.1/spark-3.1.1-bin-hadoop2.7.tgz
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

update

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