Install Vagrant on Ubuntu OS 18.04 and create first vagrant virtual environment

Vagrant is an open source command line tool for building and managing virtual machine environments. By default Vagrant can provision machines on top of VirtualBox, Hyper-V and Docker but many other providers such as Libvirt (KVM), VMware and AWS can be installed via the Vagrant plugin system.

Vagrant is mostly used by developers to easily set up a development environment, that matches the production environment. In this blog post, we’ll discuss how to install Vagrant on Ubuntu 18.04 Operating System. We’ll be using the VirtualBox provider, which is the default provider for Vagrant.

Although below steps are written for Ubuntu 18.04 Bionic Beaver the same steps can be used for Ubuntu 16.04 Xenial Xerus. Same goes for other linux distributions.

Install Oracle VirtualBox

Since we are going to use Oracle virtualbox as provider, the first step is to install the Oracle VirtualBox package. This package is also available in the Ubuntu’s repositories:

$ sudo apt install virtualbox -y

install virtualbox on ubuntu

Install Vagrant

We can use below command to install vagrant:

$ sudo apt install vagrant -y

install vagrant on ubuntu

Do note that the Vagrant package which is available in the Ubuntu’s repositories may not always be the latest version. If you want to install the latest version of Vagrant then download the Debian package from the Vagrant Download page.

Verify Vagrant installation

We can use vagrant --version to check if the installation went fine and to find out the version installed. On successful run, you should see an output like below:

checking vagrant version installed

Create first Vagrant Box

Next obvious step is to create our first virtual environment using Vagrant. For this, first we’ll create a directory where we’ll store the Vagrantfile. The Vagrantfile is written in Ruby language and describes how to configure and provision the virtual machine. We can use below commands to create and switch into directory:

$ mkdir ubuntu16
$ cd ubuntu16/

Now, we need to create a Vagrantfile for which we’ll using the vim editor. Create a file named Vagrantfile and add below text:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
end

create and setup vagrant file for ubuntu xenial

Another popular way is to initialize a new Vagrantfile using the vagrant init command and specify the box we wish to use. Boxes are the package format for the Vagrant environments and are provider-specific. You can find a list of publicly available Vagrant Boxes on the Vagrant box catalog page. For using ubuntu/xenial64 box, the commands are as below:

$ vagrant init ubuntu/xenial64

When you run above command, you should see something like below message:

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.

You can open the Vagrantfile, read the comments and make adjustment according to your needs.

Now we need to run the vagrant up command which will create and configure the virtual machine according to the Vagrantfile:

==> vagrant: A new version of Vagrant is available: 2.2.4 (installed version: 2.2.3)!
==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/xenial64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/xenial64' version '20190207.0.0' is up to date...
==> default: A newer version of the box 'ubuntu/xenial64' for provider 'virtualbox' is
==> default: available! You currently have version '20190207.0.0'. The latest is version
==> default: '20190221.0.0'. Run `vagrant box update` to update.
==> default: Setting the name of the VM: vagrant-playbooks_default_1551652857594_98832
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection aborted. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 5.1.38
    default: VirtualBox Version: 6.0

It may take few minutes to create the environment. Vagrant also mounts the project directory at /vagrant in the virtual machine which allows you to work on your project’s files on your host machine.

To ssh into the virtual machine, we can simply run vagrant ssh. On successful login, you should see below like message:

Welcome to Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-142-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.

New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


vagrant@ubuntu-xenial:~$

We can also display details of ssh connection using vagrant ssh-config and to destroy the virtual machine, we can run vagrant destroy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s