Jump toUpdate content

Installing and Configuring Vagrant

Reviewed on 11 April 2023Published on 31 October 2018
  • compute
  • virtualization
  • Vagrant
  • virtualbox
  • VMware

Vagrant - Overview

Vagrant is a tool for building and managing virtual machine environments in a single workflow.

The tool provides a portable, reproducible and easy to configure work environments built on top of industry-standard technology. Machines are provisioned on top of VirtualBox, VMware, Scaleway, or any other provider.

Security & Identity (IAM):

You may need certain IAM permissions to carry out some actions described on this page. This means:

  • you are the Owner of the Scaleway Organization in which the actions will be carried out, or
  • you are an IAM user of the Organization, with a policy granting you the necessary permission sets

Downloading and installing Vagrant

This tutorial supposes that you have already installed one of supported virtual providers, like VirtualBox or Docker.

  1. Download the latest version of Vagrant for your operating system.
  2. Launch the installation tool and follow the steps to install it on your local computer.
  3. Verify that the installation has completed successfully by launching the command vagrant -v:
    vagrant -v
    Vagrant 2.2.19

Deploying a Vagrant Box

Prepacked templates of virtual machines are called Boxes in Vagrant. You can find a full list of all public boxes on the box search page.

Important Vagrant Boxes are build for certain hypervizors and may not be universally compatible. Check the box search page to verify, if the Box is compatible with your hypervizor. Alternatively you may use Bento Boxes, these boxes are open source and built for a number of providers including VMware, Virtualbox, and Parallels. There are a variety of operating systems and versions available.

  1. Create a new directory for your Vagrant project and enter it:

    mkdir -p vagrant-projects/ubuntu-vm
    cd vagrant-projects/ubuntu-vm
  2. Initialize a new Vagrant environment inside the projects directory with the command vargrant init <boxpath>, where <boxpath> is the name of the Vargarant Box:

    vagrant init bento/ubuntu-18.04
  3. Start the new virtual machine:

    vagrant up

    Runnig the vagrant up command creates, configures and starts a virtual machine as described in your Vagrantfile.

    During the first run the tool downloads automatically the required Vagrant box from the box repository and performs provisioning of the machine.

  4. Log into your box using SSH:

    vagrant ssh default

    You can now run any tasks on your virtual machine and develop your application.

    Once you have finished your work on the virtual machine, you have several options to stop or to destroy it if you do not need it any longer.

  5. To stop the virtual machine, type:

    vagrant halt
  6. To hibernate the machine, type:

    vagrant suspend
  7. If you want to delete your VM, type:

    vagrant destroy

To recreate a VM from the configuration in your Vagrantfile simply type:

vagrant up

Working with the Vagrantfile

You can modify the Vagrantfile to customize the provision of virtual machines towards your needs.

To update your default Ubuntu to the latest version and to install the Apache web server automatically, uncomment the following lines as following in your Vagrantfile and save it:

# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2

Now you can recreate your VM with and follow the update of the system as well as the installation of Apache:

vagrant up

Provisioning Scaleway Instances with Vagrant

You can use a plugin that allows you to provide Scaleway Instances directly from your Vagrantfile.

  1. Create a new folder for your project:

    mkdir vagrant-scaleway
  2. Install the plugin with Vagrant:

    vagrant plugin install vagrant-scaleway
  3. Create a Vagrantfile that is looking like the following example:

    Vagrant.configure('2') do |config|
    config.vm.provider :scaleway do |scaleway, override|
    config.vm.synced_folder '.', '/vagrant', disabled: true
    scaleway.organization = 'YOUR_ORGANIZATION_UUID'
    scaleway.token = '$SECRET_KEY'
    scaleway.name = 'YOUR_SERVER_NAME'
    scaleway.commercial_type = 'GP1-S'
    scaleway.region = 'PAR1'
    scaleway.image = 'IMAGE_ID'
    override.ssh.private_key_path = '~/.ssh/id_rsa'

    You may check this RubyDoc for all configuration options.

  4. Once you have finished the work on your Vagrantfile, you can deploy the Instance by typing:

    vagrant up

    You will see an output like the following:

    Bringing machine 'default' up with 'scaleway' provider...
    ==> default: Creating a server with the following settings...
    ==> default: -- Commercial Type: GP1-S
    ==> default: -- Image: db52018e-b3c0-4676-94a0-c40ba3f4a282
    ==> default: -- Name: scw-vagrant-server
    ==> default: Starting the server...
    ==> default: Waiting for server to become "ready"...
    ==> default: Waiting for SSH to become available...
    ==> default: Machine is booted and ready for use!
  5. You can now log into your server:

    vagrant ssh

    And you will see an output like the following, meaning that you are connected to your Scaleway Instance now:

    Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-67-generic x86_64)
    * Documentation: https://help.ubuntu.com
    * Management: https://landscape.canonical.com
    * Support: https://ubuntu.com/advantage
    System information as of Tue Apr 11 13:58:46 UTC 2023
    System load: 0.0 Processes: 90
    Usage of /: 20.7% of 8.85GB Users logged in: 0
    Memory usage: 10% IPv4 address for ens2:
    Swap usage: 0% IPv6 address for ens2: 2001:bc8:

For more information how to manage your virtual machines with Vagrant, you may check out the official documentation.