Community Blog How to Install Vagrant on ECS Ubuntu 18.04

How to Install Vagrant on ECS Ubuntu 18.04

In this article, we'll discuss the steps to install Vagrant on Ubuntu 18.04 on an Alibaba Cloud Elastic Compute Service.

By Alain Francois, Alibaba Cloud Community Blog author

Vagrant is a virtualization tool that allows you to administer and deploy virtual machines from a command line interface. With the vagrant commands, you have the possibility to create new virtual light environments. Vagrant runs on Linux, Mac OS X or Windows and runs a whole bunch of operating systems. Vagrant is under the spell of the giants of the virtualization world.

The popularity of vagrant comes from the world of developers because it's an easy way for them to virtualize a new development environment without many constraints. In this tutorial, you learn how to install vagrant on Ubuntu 18.04 and create a vagrant machine.

How does vagrant works?

To run vagrant, you need first a virtualization tool to be installed such as virtualbox or vmware workstation. Vagrant creates the virtual machines based on what we call vagrant boxes which is are type of image of an operating systems that you will need to install. You can consider it as a type of template from which the vagrant machine will be based on.

To create a vagrant machine, the vagrant command will download the box (image of the operating system) that you want your vagrant machine to be based on. It can be a modified vagrant machine with some other pre-installed tools. You can check the list of all the boxes available in the official link of vagrant


In our case, we will install first virtualbox on our Ubuntu machine. To be sure to have a full compatibility when writing this article, I recommend you to install the version VirtualBox 5.2

$ sudo apt install virtualbox
[sudo] password for alain: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  adwaita-icon-theme at-spi2-core binutils binutils-common binutils-x86-64-linux-gnu build-essential cpp cpp-7
  dconf-gsettings-backend dconf-service dkms dpkg-dev fakeroot fontconfig fontconfig-config fonts-dejavu-core g++
  g++-7 gcc gcc-7 gcc-7-base glib-networking glib-networking-common glib-networking-services
  gsettings-desktop-schemas gtk-update-icon-cache hicolor-icon-theme humanity-icon-theme libalgorithm-diff-perl
  libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan4 libasound2 libasound2-data libasyncns0
Need to get 84.6 MB/112 MB of archives.
After this operation, 606 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Now make sure that you have installed the version that I recommended

$ vboxmanage --version 

If you want to manually download it, you can go to the official link of virtualbox then download it

$ wget https://download.virtualbox.org/virtualbox/5.2.26/virtualbox-5.2_5.2.26-128414~Ubuntu~bionic_amd64.deb
--2019-03-20 16:36:35--  https://download.virtualbox.org/virtualbox/5.2.26/virtualbox-5.2_5.2.26-128414~Ubuntu~bionic_amd64.deb
Resolving download.virtualbox.org (download.virtualbox.org)...
Connecting to download.virtualbox.org (download.virtualbox.org)||:443... connected.
HTTP request sent, awaiting response... 200 OK

Now you can install it with dpkg

$ sudo dpkg -i virtualbox-5.2_5.2.26-128414~Ubuntu~bionic_amd64.deb

If you face some errors with the dependencies, use the apt install -f command

Install Vagrant

Now that your virtualbox is installed, you can install vagrant. By default, it's present in the officials repositories

$ sudo apt install vagrant
[sudo] password for alain: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  attr augeas-lenses bsdtar cpu-checker db-util db5.3-util exfat-fuse exfat-utils extlinux fonts-droid-fallback
  fonts-lato fonts-noto-mono genisoimage ghostscript gsfonts gstreamer1.0-plugins-base gstreamer1.0-plugins-good
  gstreamer1.0-x hfsplus ibverbs-providers icoutils ipxe-qemu ipxe-qemu-256k-compat-efi-roms javascript-common
  keyutils ldmtool libaa1 libafflib0v5 libaio1 libarchive-tools libarchive13 libaugeas0 libauthen-sasl-perl
  Need to get 65.0 MB of archives.
After this operation, 271 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

When your vagrant is installed, you can now create your virtual machine

Create a Vagrant Machine

To create the vagrant machine, you need a vagrant box. You can check the list of the available boxes from the official link of vagrant. Let's create a vagrant machine based on debian 9, on your terminal, but first let's create a folder to organize our vagrant machines

$ mkdir vagrant-machine && cd vagrant-machine

Now you can run the command to initialize your vagrant machine

~/vagrant-machine$ vagrant init debian/stretch64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

That command creates in the current directory the file Vagrantfile containing all the information necessary to initialize, run and configure the vagrant machine by using the template of Debian 9 64 bits. You can check if you have the Vagrantfile in the current directory. It's essential to run the vagrant machine.

~/vagrant-machine$ ls

Understanding the Lines of the Vagrantfile

The Vagrantfile contains some lines giving some information for the creation of your virtual machine. You can list the content of the file with the cat command

  • config.vm.box = "ubuntu/bionic64": box setting, it's as the template of the box to use.
  • config.vm.box_check_update = false: to enable or disable the fact that vagrant should check if the box is up to date
  • config.vm.network "forwarded_port", guest: 80, host: 8080: configure the port forwarding between the guest and the host. This means that a web application on the port 80 of your vagrant machine is accessible through the port 8080 of your physical machine.
  • config.vm.network "private_network", ip: "": to create a private network for the vagrant machine with a host-only access.
  • config.vm.network "public_network": creates public network working as a bridged so that you vagrant-machine will work as an independent node of the network
  • config.vm.synced_folder "../data", "/vagrant_data": works like shared folders, so there is the possibility to synchronize a folder in your vagrant machine and a folder of your physical machine like sharing a folder between the guest and the host
  • config.vm.provider "virtualbox" do |vb|...end: this is the block of information for the provider that your vagrant machine is using. Most of the time it's useful for graphical debug if an error occurs when you start your vagrant machine
  • config.vm.provision "shell", inline: <<-SHELL...end: this block defines the provision method (automation tool) that you want to use to automate some process on your vagrant machine during the boot. The provision method used here is the shell. You can use another provision method like chef, ansible, etc.

Running the Vagrant Machine

Now you can run your vagrant machine

~/vagrant-machine$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'debian/stretch64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'debian/stretch64'
    default: URL: https://vagrantcloud.com/debian/stretch64
==> default: Adding box 'debian/stretch64' (v9.8.0) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/debian/boxes/stretch64/versions/9.8.0/providers/virtualbox.box
==> default: Successfully added box 'debian/stretch64' (v9.8.0) for 'virtualbox'!
==> default: Importing base box 'debian/stretch64'...

The first time that you run that command, it downloads and stores the vagrant box on your computer in the .vagrant.d directory of your home folder so that it will be used for the initialization of the vagrant machines that you will need to create from that box. You can see that it's downloading the box first. If you have downloaded a version of virtualbox which is not compatible with vagrant, you will receive a similar message as below

No usable default provider could be found for your system.

Vagrant relies on interactions with 3rd party systems, known as
"providers", to provide Vagrant with resources to run development
environments. Examples are VirtualBox, VMware, Hyper-V.

The easiest solution to this message is to install VirtualBox, which
is available for free on all major platforms.

If you believe you already have a provider available, make sure it
is properly installed and configured. You can see more details about
why a particular provider isn't working by forcing usage with
`vagrant up --provider=PROVIDER`, which should give you a more specific
error message for that particular provider.

Now you can access your vagrant machine by using ssh but not by the normal usual way as you know. It's the vagrant ssh command used like below

$ vagrant ssh
Linux stretch 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3 (2019-02-02) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You can try an update

Now you are in the virtual machine. You can start to work normally by updating and installing the packages

vagrant@stretch:~$ sudo apt update
Hit:1 http://security.debian.org/debian-security stretch/updates InRelease
Ign:2 http://deb.debian.org/debian stretch InRelease
Hit:3 http://deb.debian.org/debian stretch Release 
Reading package lists... Done                      
Building dependency tree       
Reading state information... Done
10 packages can be upgraded. Run 'apt list --upgradable' to see them.
vagrant@stretch:~$ exit

Some Important Vagrant Commands

With vagrant you will need to manage your virtual machine and also the boxes that you will download. There are a lots to operations that you can do on vagrant but you should at least know the commands which can be very useful for regular operations.

You should understand that to run those vagrant commands, for most of them, you have to be in the folder containing the vagrant machine, in other words the Vagrantfile.

Below you can see a list of useful vagrant command to manage your virtual machine

  • vagrant init: creates a vagrant configuration file (Vagrantfile) which will be read to initialize the vagrant machine to setup. The file contains the box on which the machine should be based on, the cpu, networking, etc
  • vagrant up: downloads or imports the box to run the vagrant machine. It will read the content of the Vagrantfile and executes line in order to set up the machine according to the environment that is specified in the file
  • vagrant ssh: by default it helps you to access your vagrant machine but unlike the normal ssh command, you have to be in the current folder where your machine (Vagrantfile) is stored. When you want to leave the vagrant machine, just use the exit command to return to the host
  • vagrant suspend: to suspend the vagrant machine
  • vagrant resume: to resume a vagrant machine which has been suspended
  • vagrant halt: to stop a vagrant machine like the shutdown. But you can use the linux shutdown command inside the vagrant machine to stop it also as it works like a normal machine.
  • vagrant reload: helps to stop and start your vagrant machine like a restart but it will not read again the new instructions that you provided in the Vagrantfile
  • vagrant destroy: this command will stop the vagrant machine and destroy all the configuration that you did
  • vagrant status: it gives you the state of the current vagrant machine, if it's running, stopped or suspended.

Now it will be also useful for your to know the vagrant command which will help you to operate on your boxes.

  • vagrant box add: helps to download the box specified after the command. The box will be stored in the default folder ~/.vagrant.d
  • vagrant box list: shows you a list of all the boxes that you have downloaded
  • vagrant box update: updates the box of the current vagrant machine. If you want to update a specific box without being in folder of the vagrant machine, use the --box followed by the name of the box
  • vagrant box remove: deletes the specified box following the command

Creating a Vagrant Machine from an Existing Box in Your Computer

As said before, the vagrant up command will download the box to run the vagrant machine immediately after the downloading process. But it's possible to first download all the boxes that you need and then run the vagrant machine that you want not immediately.

To do this, we use the command vagrant box add followed by the name of the box to download as below

$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'centos/7' (v1902.01) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1902.01/providers/virtualbox.box

During the process it will download and store the boxes in the default folder ~/.vagrant.d. You can use the same command to download the others boxes that you can need from the official link

Now let us try to run the vagrant machine based on centos 7. First let's create a directory and initialize our machine

$ mkdir centos7 && cd centos7
$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Now let's run the machine,

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1902.01' is up to date...
==> default: Setting the name of the VM: centos7_default_1553955605013_87107
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Machine booted and ready!

You see that it didn't try to download the virtual machine but it just imported the box directly because it was detected in the default folder where it's saved. You can now ssh your vagrant machine with the vagrant ssh command and first update the system or whatever you want

[vagrant@localhost ~]$ sudo yum update
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.wiru.co.za
 * extras: mirror.wiru.co.za
 * updates: www.ftp.saix.net
base                                                     | 3.6 kB     00:00     
extras                                                   | 3.4 kB     00:00     
updates                                                  | 3.4 kB     00:00     
(1/4): base/7/x86_64/group_gz                              | 166 kB   00:01     
(2/4): extras/7/x86_64/primary_db                          | 187 kB   00:01     
(3/4): base/7/x86_64/primary_db                            | 6.0 MB   00:07     
(4/4): updates/7/x86_64/primary_db                         | 3.4 MB   00:16     
Resolving Dependencies
--> Running transaction check
---> Package NetworkManager.x86_64 1:1.12.0-8.el7_6 will be updated
---> Package NetworkManager.x86_64 1:1.12.0-10.el7_6 will be an update
---> Package NetworkManager-libnm.x86_64 1:1.12.0-8.el7_6 will be updated

Now you know how to create a virtual lightweight environment for your tests before going on production. Vagrant can be manipulated also by non system administrators and is very popular among the developers. It gives the advantages to manage configuration of the virtual machine with provision tools such as chef, puppet if you have many environments to configure.

0 0 0
Share on

Alibaba Clouder

2,606 posts | 737 followers

You may also like