Create Virtual Machine with Vagrant
Published on

Create Virtual Machine with Vagrant

Authors

Vagrant is a tool for building and managing virtual machine environments in a single workflow. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time.

To install Vagrant on Ubuntu, run the following commands:

sudo apt-get update
sudo apt-get install -y virtualbox vagrant

We need to install VirtualBox as well because Vagrant uses VirtualBox to manage the virtual machines.

In the below example, we will create a virtual machine with Ubuntu 22.04. We will configure the virtual machine with 4GB of memory and 2 CPUs. As for the network, we will use the public network and bridge it to the eno1 interface with the IP address 192.168.0.51 for the virtual machine. Lets say you're behind router so this IP address would be private IP address.

Vagrant.configure("2") do |config|
    config.ssh.username = "vagrant"
    config.vm.define "vm1" do |vagrant|
        vagrant.vm.box = "ubuntu/jammy64"
        vagrant.vm.network "public_network", bridge: "eno1", ip: "192.168.0.51"
        vagrant.vm.provider "virtualbox" do |vb|
            vb.memory = "4096"
            vb.cpus = 2
        end
    end
end

To get the bridge interface name we can run ip addr show command. In the output, you will see the interface name. In my case, it is eno1 the machine's primary network interface.

Also, the ip addr show command does not show the public IP address. It shows the network configuration and IP addresses of all network interfaces on your system, which are typically private IP addresses assigned to your machine on your local network.

If you want to see your public IP address (the one that the outside world sees), you can use a service like ipecho.net. Here's a command that will print your public IP address:

curl ipinfo.io/ip

To run the virtual machine, navigate to the directory where the Vagrantfile is located and run the following command:

vagrant up

This will create the virtual machine with the specified configuration. To SSH into the virtual machine, run the following command:

vagrant ssh

If you want to change the SSH public key. You need to place the public key in the ~/.ssh/authorized_keys file. You can use the following command to copy the public key to the virtual machine. But if you do this we cannot ssh into the virtual machine using vagrant ssh command, for making it work you need to add the private key ./.vagrant/machines/{Machine Name}/virtualbox/private_key.

Now this virtual machine can be used for development purposes and can be accessed by a static local IP and would function or act like a seperate machine. You can install the required software and tools on this virtual machine and use it for development purposes.

But as we are manually configuring the virtual machine and not using vagrant for assigning SSH keys we need to be careful and not run vagrant up as it can overwrite the configuration and we may lose the data incase you make any changes to Vagrantfile.

To stop the virtual machine.

vagrant halt

To destroy the virtual machine.

vagrant destroy

This will destroy the virtual machine and remove all the files associated with it. You can create a new virtual machine by running vagrant up again.

Sometimes you need to just run the VM we can use vboxmanage command to directly interact with the virtual machine.

To list all the virtual machines.

vboxmanage list vms

To start the virtual machine.

vboxmanage startvm "VM Name"