Automatically Create and manage VMs with Vagrant on Mac M1 chip
Creating and managing development environment can be tedious if done manually. The most efficient way to create VMs is to create them automatically through code, so it becomes repeatable and easy. Vagrant is an open-source tool used for creating and managing virtual development environments. It provides a simple and portable way to create and configure virtual machines (VMs) with predefined configurations, making it easier to set up and manage development environments for software projects.
Prerequisites:
i. MacBook with M1 chip
ii. Homebrew: This is a package manager that can be used to install and manage software packages. Click here to install homebrew.
iii. Rosetta: Rosetta is a software compatibility layer developed by Apple that allows Mac computers with Apple Silicon processors (such as the M1 chip) to run apps designed for Intel-based Macs. Rosetta acts as a translator, enabling older software that was built for Intel-based Macs to run on the newer Apple Silicon-based Macs.
/usr/sbin/softwareupdate --install-rosetta --agree-to-license
iv. VMware: VMware’s virtualization software allows multiple operating systems to run on a single physical machine. Click here to create an account on vmware. Then, click here to download the .dmg installer for VMWare Fusion Tech Preview.
FOLLOW THE STEPS BELOW TO CREATE VMs AUTOMATICALLY USING VAGRANT:
Step 1: Install Vagrant with Homebrew
Vagrant allows developers to define virtual environments as code, using a declarative configuration file known as a “Vagrantfile”. This file specifies the details of the virtual machine, such as the operating system, system resources, networking, and software provisioning. With Vagrant, developers can easily share and reproduce development environments across different platforms and team members, making it ideal for collaborative software development.
To install vagrant run the following command:
brew install --cask vagrant
Step 2 : Create a link
A symbolic link, also known as a symlink or a soft link, is a unique type of file that acts as a pointer to another file or directory, referred to as the target. Once created, a symbolic link can be used as a substitute for the target file or directory, allowing for convenient access and manipulation of the target file using the symbolic link’s name.
run the command below to create a symbolic link for the path.
ln -s /Applications/VMWare\ Fusion\ Tech\ Preview.app /Applications/VMWare\ Fusion.app
Step 3: Install vmware provider and install plugin
Vagrant primarily supports VirtualBox, Hyper-V, and Docker as its default providers. However, Vagrant is a highly extensible tool with a plugin system, and there are community-developed plugins that provide support for VMware as a virtualization provider.
vagrant-vmware-desktop: This plugin provides support for using VMware Fusion or VMware Workstation as the virtualization provider for Vagrant on macOS and Windows.
To download and install Vagrant vmware Utility click here, then click on “allow” to download. This will download the compatible file called ‘vagrant-vmware-utility_1.0.21_x86_64.dmg’.
Navigate to your Downloads folder, and double click the .dmg file to install.
Next, run the command to install plugin
vagrant plugin install vagrant-vmware-desktop
Step 4: Create directory to store Vagrantfile
Next step, we have to create a directory to store the Vagrantfile. This is up to you where you want to create this directory.
However for this demo run the following command to complete this task
cd ~
mkdir virtualmachines
cd virtualmachines/
mkdir ubuntu
cd ubuntu
At this stage, we need to create the Vagrantfile.
So, run the below command
nano Vagrantfile
copy and paste the below content in the Vagrantfile and save
Vagrant.configure("2") do |config|
config.vm.box = "spox/ubuntu-arm"
config.vm.box_version = "1.0.0"
config.vm.network "private_network", ip: "192.168.56.11"
config.vm.provider "vmware_desktop" do |vmware|
vmware.gui = true
vmware.allowlist_verified = true
end
end
Step 5: Use Ubuntu server on the VM.
i. To start the vm automatically run the command below
vagrant up
ii. To ssh into the ubuntu server automatically run the command below
vagrant ssh
As seen in the image above, the prompt changes indicating we successfully logged into the ubuntu server.
iii. To check the status of the Virtual Machine, run the command below:
#log out of server
exit
#destroy VM
vagrant status
iii. To automatically delete the virtual machine, run the command below:
#log out of server
exit
#destroy VM
vagrant destroy --force
More Commands:
i. To shut down the VM, you can run the command below:
vagrant halt
ii. To simply suspend the virtual machine, you can run the following:
vagrant suspend
Summary
We learnt how to use Vagrant on an M1 MacBook to create and manage virtual machines. Install Vagrant, create a directory, configure the Vagrantfile to use Ubuntu, and use additional commands to control the virtual machine. Vagrant allows for easy sharing and reproduction of development environments, making it perfect for collaborative software development.