Common Commands
- up
Starts and provisions the virtual machine defined in your Vagrantfile. If the VM doesn’t exist yet, it creates it.
- destroy
Stops and completely deletes the virtual machine (including all resources).
- halt
Gracefully shuts down the running virtual machine without deleting it.
- snapshot
Saves or manages the state of a VM at a specific point in time.
- ssh
Connects to the running VM via SSH.
- rsync
Synchronizes files from your host machine to the VM using rsync.
Ruby constructs
- loops
Loops repeat a block of code multiple times until a condition is met.
Example:
i = 0while i < 3 puts i i += 1end
- conditionals
Conditionals let your program make decisions based on conditions.
Example:
age = 18if age >= 18 puts "Adult"else puts "Minor"end
- variables
Variables store data that your program can use and modify.
Example:
name = "Alice"age = 25puts "#{name} is #{age} years old"
- code blocks
Code blocks are chunks of code that can be passed to methods and executed later.
Example:
3.times do puts "Hello!"end
Vagrantfile
- provider
Specifies which virtualization platform (provider) Vagrant should use (e.g., VirtualBox, VMware).
Example:
Vagrant.configure("2") do |config| config.vm.provider "virtualbox" do |vb| vb.memory = "2048" endend
- provisioner
Defines how the VM is set up (install software, run scripts, configure environment).
Example:
Vagrant.configure("2") do |config| config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y nginx SHELLend
- VM: The main configuration block for defining the virtual machine.
define: Defines multiple VMs in one Vagrantfile.
Vagrant.configure("2") do |config| config.vm.define "web" do |web| web.vm.box = "ubuntu/bionic64" end config.vm.define "db" do |db| db.vm.box = "ubuntu/bionic64" endend
hostname: Sets the hostname inside the VM.
config.vm.hostname = "my-server"
network: Configures how the VM connects to networks.
config.vm.network "private_network", ip: "192.168.56.10"
sync_folder: Shares files between your host machine and the VM.
config.vm.synced_folder "./data", "/var/www/data"
box: Specifies the base image (OS) used to create the VM.
config.vm.box = "ubuntu/bionic64"