DEV Community

Cover image for Quick Guide: Spinning Up Vagrant in No Time
DevOps Descent
DevOps Descent

Posted on

Quick Guide: Spinning Up Vagrant in No Time

How to Install Vagrant on Ubuntu 22/24

Vagrant enables the creation and configuration of lightweight, reproducible, and portable development environments.

Follow these steps to install the latest version of Vagrant on Ubuntu.

Step 1: Add the HashiCorp Repository

To add the HashiCorp repository, run the following commands:

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Vagrant

sudo apt install vagrant -y
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the Installation

To confirm that Vagrant was installed successfully, check the version:

vagrant --version
Enter fullscreen mode Exit fullscreen mode

Step 4: Install VirtualBox (optional)

Vagrant requires a provider, such as VirtualBox, to run virtual machines. Install VirtualBox with this command:

sudo apt install -y virtualbox
Enter fullscreen mode Exit fullscreen mode

Image description

Use: Create a Development Environment with Vagrant

To create a Vagrant project, start by creating a new project directory in your preferred location for Vagrant
configuration and related files.

mkdir vagrant-project 
cd vagrant-project
Enter fullscreen mode Exit fullscreen mode

To initialize the Vagrant configuration file with an Ubuntu box, run the command:

vagrant init ubuntu/jammy64
Enter fullscreen mode Exit fullscreen mode

You can add the --minimal flag to the initialization command of the Vagrantfile to generate a Vagrantfile without
any additional settings.

Manage Vagrant

1.vagrant up: Launches the virtual machine and provisions it according to the settings in the Vagrantfile. This
command will simply connect to the virtual machine if it is already running.

2.vagrant halt: Stops the virtual machine by delivering a shutdown signal to the guest operating system. This
command is similar to shutting down a real computer.

3.vagrant reload: Restarts the virtual machine and re-provisions it depending on any changes in the Vagrantfile.

4.vagrant ssh: Connects to the virtual machine via SSH. This command is useful for accessing the command line
interface of the virtual machine.

5.vagrant status: Shows the current status of the virtual machine, including whether it's running, stopped, or
suspended.

6.vagrant destroy: Deletes the virtual machine and all associated resources. This command is useful for cleaning up
your development environment.

BONUS CHEATSHEET:

Image description

Some common SSH troubleshooting:

1. The default username and password for Vagrant's SSH access are often both set to vagrant. If you're prompted
for a password, try entering vagrant.

2.Use SSH Keys: Most Vagrant boxes use SSH key-based authentication, not passwords. Vagrant automatically sets up a
private key (.vagrant/machines/default/virtualbox/private_key by default). You can try connecting with this key
manually:

ssh -i .vagrant/machines/default/virtualbox/private_key vagrant@127.0.0.1 -p 2222
Enter fullscreen mode Exit fullscreen mode

3. Re-provision the VM: If the configuration seems incorrect, run:

vagrant reload --provision
Enter fullscreen mode Exit fullscreen mode

4.Force a Specific Key Type: You can try forcing the use of RSA, as some SSH clients default to newer key types not
supported by certain Vagrant boxes. Use this command:

ssh -o HostKeyAlgorithms=+ssh-rsa -o PubkeyAcceptedKeyTypes=+ssh-rsa -i .vagrant/machines/default/virtualbox/private_key vagrant@127.0.0.1 -p 2222
Enter fullscreen mode Exit fullscreen mode

5.Regenerate SSH Key: Sometimes, the private key in .vagrant/machines/default/virtualbox/private_key can become
corrupted or incompatible. You can try destroying and recreating the VM to regenerate a fresh SSH key:

vagrant destroy -f
vagrant up
Enter fullscreen mode Exit fullscreen mode

6.Vagrant Box Compatibility: Some older Vagrant boxes may not support recent SSH configurations. If you’re using an
old box version, consider updating it:

vagrant box update
vagrant destroy -f
vagrant up
Enter fullscreen mode Exit fullscreen mode

Support if you found this helpfulπŸ˜‰

No Money πŸ™…πŸ»β€β™€οΈ just Subscribe to my YouTube channel.

Linktree Profile: https://linktr.ee/DevOps_Descent
GitHub: https://github.com/devopsdescent

Top comments (2)

Collapse
 
ddivyasharma profile image
DdivyaSharma

I just blindly followed this, and it worked.
Thanks alot

Collapse
 
devops_descent profile image
DevOps Descent

Glad it helped