Installing and Configuring Vagrant
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.
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
- You have an account and are logged into the Scaleway console
- You have configured your SSH Key
Downloading and installing Vagrant
This tutorial supposes that you have already installed one of supported virtual providers, like VirtualBox or Docker.
- Download the latest version of Vagrant for your operating system.
- Launch the installation tool and follow the steps to install it on your local computer.
- Verify that the installation has completed successfully by launching the command
vagrant -v:vagrant -vVagrant 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.
Create a new directory for your Vagrant project and enter it:mkdir -p vagrant-projects/ubuntu-vmcd vagrant-projects/ubuntu-vm
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
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.
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.
To stop the virtual machine, type:vagrant halt
To hibernate the machine, type:vagrant suspend
If you want to delete your VM, type:vagrant destroy
To recreate a VM from the configuration in your Vagrantfile simply type:
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: <<-SHELLapt-get updateapt-get install -y apache2SHELLend
Now you can recreate your VM with and follow the update of the system as well as the installation of Apache:
Provisioning Scaleway Instances with Vagrant
You can use a plugin that allows you to provide Scaleway Instances directly from your Vagrantfile.
Create a new folder for your project:mkdir vagrant-scaleway
Install the plugin with Vagrant:vagrant plugin install vagrant-scaleway
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: truescaleway.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'endend
You may check this RubyDoc for all configuration options.
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!
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/advantageSystem information as of Tue Apr 11 13:58:46 UTC 2023System load: 0.0 Processes: 90Usage of /: 20.7% of 8.85GB Users logged in: 0Memory usage: 10% IPv4 address for ens2: 10.10.10.1Swap 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.