Latest

Sunday, 16 June 2019

How to Install Vagrant with VirtualBox on CentOS 7

Install Vagrant with VirtualBox on CentOS 7

Vagrant is an open-source software for easy provisioning of virtual software development environments. The virtual environments called Vagrant boxes are portable in nature and can be used over many platforms without any change. Vagrant is developed by HashiCorp and distributed under MIT License. Vagrant leverages a declarative configuration file which describes all your software requirements, packages, operating system configuration, users, and more.

In this article, we will install Vagrant with VirtualBox on CentOS 7 server. At first, we will install VirtualBox as the provider for Vagrant software, then we will install Vagrant on CentOS 7.

 

Table of Contents:

Install Vagrant with VirtualBox on CentOS 7

Vagrant Features:

  • Create complex virtual development environments in a single workflow.
  • Portal development environments, that can be moved to other machines conveniently.
  • Starts a desired development environment with just a single command.
  • A same vagrantfile can be used by all developers to provision their own development environments.
  • Users can create their own custom Vagrant Box or select from the globally available Vagrant boxes of popular software.
  • Easy and Fast.

 

Vagrant Architecture:

Vagrant uses Provisioners and Providers as building blocks to create and manage the development environments.

  • Provisioners are tools to customize the virtual environments e.g. Puppet, Chef, Ansible, etc.
  • Providers are the services that Vagrant uses to setup and create virtual environments e.g. VirtualBox, Docker, Hyper-V, etc.

Vagrant creates a layer on top of the virtualization software and allow the users to manager development environments with just a single vagrant command. Machine and software requirements are defined in vagrantfile, to execute necessary steps to create a development-ready box.

Box is a format and an extension (.box) for Vagrant environments, and it is portable across various platforms.

 

Vagrant System Requirements:

Vagrant has no special hardware and software requirements except that it requires a Provider to create development environments.

 

Environment Specification:

We have provisioned a CentOS 7 based system with following specifications:

  • Hostname - vagrant-01.example.com
  • IP Address - 192.168.116.176 /24
  • Operating System - CentOS 7.6
  • CPU - 3.4 Ghz (2 Core)
  • Memory - 2 GB
  • Storage - 20 GB

 

Installing VirtualBox on CentOS 7:

Connect with vagrant-01.example.com using ssh as root user.

Install prerequisite software packages using yum command.

# yum install -y kernel-devel-$(uname -r) kernel-headers gcc make perl wget

We have used $(uname -r) with above yum command. It demands a little clarification.

The vboxdrv.service requires same kernel-devel modules as of running Kernel. Otherwise, after installation vboxdrv.service will give you following errors.

# tail /var/log/messages
Jun 16 11:22:05 vagrant-01 vboxdrv.sh: Building VirtualBox kernel modules.
Jun 16 11:22:06 vagrant-01 vboxdrv.sh: This system is currently not set up to build kernel modules.
Jun 16 11:22:06 vagrant-01 vboxdrv.sh: Please install the Linux kernel "header" files matching the current kernel
Jun 16 11:22:06 vagrant-01 vboxdrv.sh: for adding new hardware support to the system.
Jun 16 11:22:06 vagrant-01 vboxdrv.sh: The distribution packages containing the headers are probably:
Jun 16 11:22:06 vagrant-01 vboxdrv.sh: kernel-devel kernel-devel-3.10.0-957.el7.x86_64
Jun 16 11:22:06 vagrant-01 systemd: vboxdrv.service: control process exited, code=exited status=1
Jun 16 11:22:06 vagrant-01 systemd: Failed to start VirtualBox Linux kernel module.
Jun 16 11:22:06 vagrant-01 systemd: Unit vboxdrv.service entered failed state.
Jun 16 11:22:06 vagrant-01 systemd: vboxdrv.service failed.

Therefore, we have used $(uname -r) command to suffix the Kernel version with the yum command for the desired result.

VirtualBox is a free and open-source Hypervisor, developed by Oracle Corporation.

VirtualBox's yum repository configurations are available at VirtualBox Official Download Page.

Therefore, we will first add VirtualBox’s yum repository in our CentOS 7 server and then we will install VirtualBox using yum command.

First of all, we have to import Oracle public key in our CentOS 7 server.

Download Oracle public key as follows.

# cd /tmp
# wget https://www.virtualbox.org/download/oracle_vbox.asc
--2019-06-16 10:29:47--  https://www.virtualbox.org/download/oracle_vbox.asc
Resolving www.virtualbox.org (www.virtualbox.org)... 137.254.60.32
Connecting to www.virtualbox.org (www.virtualbox.org)|137.254.60.32|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1734 (1.7K) [text/plain]
Saving to: âoracle_vbox.ascâ

100%[======================================>] 1,734       6.44KB/s   in 0.3s

2019-06-16 10:29:51 (6.44 KB/s) - âoracle_vbox.ascâ saved [1734/1734]

Import Oracle public key in our CentOS 7 server.

# rpm --import oracle_vbox.asc

Download and add VirtualBox yum repository in our CentOS 7 server.

# wget https://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo -O /etc/yum.repos.d/virtualbox.repo
--2019-06-16 10:32:01--  https://download.virtualbox.org/virtualbox/rpm/el/virtualbox.repo
Resolving download.virtualbox.org (download.virtualbox.org)... 23.46.86.216
Connecting to download.virtualbox.org (download.virtualbox.org)|23.46.86.216|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 259 [text/plain]
Saving to: â/etc/yum.repos.d/virtualbox.repoâ

100%[======================================>] 259         --.-K/s   in 0s

2019-06-16 10:32:04 (1.02 MB/s) - â/etc/yum.repos.d/virtualbox.repoâ saved [259/259]

Build yum cache for newly added repositories.

# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ges.net.pk
 * extras: mirrors.ges.net.pk
 * updates: mirrors.ges.net.pk
http://repos.del.extreme-ix.org/centos/7.6.1810/os/x86_64/repodata/repomd.xml: [Errno 14] curl#7 - "Failed connect to repos.del.extreme-ix.org:80; Connection refused"
Trying other mirror.
base                                                     | 3.6 kB     00:00
extras                                                   | 3.4 kB     00:00
updates                                                  | 3.4 kB     00:00
virtualbox/7/x86_64/signature                            |  181 B     00:00
Retrieving key from https://www.virtualbox.org/download/oracle_vbox.asc
Importing GPG key 0x98AB5139:
 Userid     : "Oracle Corporation (VirtualBox archive signing key) <info@virtualbox.org>"
 Fingerprint: 7b0f ab3a 13b9 0743 5925 d9c9 5442 2a4b 98ab 5139
 From       : https://www.virtualbox.org/download/oracle_vbox.asc
Is this ok [y/N]: y
virtualbox/7/x86_64/signature                            | 2.9 kB     00:04 !!!
virtualbox/7/x86_64/primary_db                             | 130 kB   00:00
Metadata Cache Created

Install VirtualBox on CentOS 7 using yum command.

# yum install -y VirtualBox-6.0

vboxdrv.service is automatically enabled and started by VirtualBox installation process.

Check status of vboxdrv.service.

# systemctl status vboxdrv.service
â vboxdrv.service - VirtualBox Linux kernel module
   Loaded: loaded (/usr/lib/virtualbox/vboxdrv.sh; enabled; vendor preset: disabled)
   Active: active (exited) since Sun 2019-06-16 11:28:47 PKT; 10min ago
  Process: 39778 ExecStart=/usr/lib/virtualbox/vboxdrv.sh start (code=exited, status=0/SUCCESS)

Jun 16 11:27:27 vagrant-01.example.com systemd[1]: Starting VirtualBox Linux ...
Jun 16 11:27:27 vagrant-01.example.com vboxdrv.sh[39778]: vboxdrv.sh: Startin...
Jun 16 11:27:27 vagrant-01.example.com vboxdrv.sh[39778]: vboxdrv.sh: Buildin...
Jun 16 11:28:47 vagrant-01.example.com systemd[1]: Started VirtualBox Linux k...
Hint: Some lines were ellipsized, use -l to show in full.

 

Install VirtualBox Extension Pack on CentOS 7:

VirtualBox is split into two components.

  • Base Package
  • Extension Packs

We have already installed VirtualBox Base Package above. Now, we are installing VirtualBox Extension Pack to extend the functionality of VirtualBox software.

VirtualBox Extension Pack is available at VirtualBox Official Download Page.

There is a single universal package of VirtualBox Extension Pack available that supports all platforms.

Therefore, we are downloading it using wget command.

# wget https://download.virtualbox.org/virtualbox/6.0.8/Oracle_VM_VirtualBox_Extension_Pack-6.0.8.vbox-extpack
--2019-06-16 11:53:23--  https://download.virtualbox.org/virtualbox/6.0.8/Oracle_VM_VirtualBox_Extension_Pack-6.0.8.vbox-extpack
Resolving download.virtualbox.org (download.virtualbox.org)... 23.213.142.80
Connecting to download.virtualbox.org (download.virtualbox.org)|23.213.142.80|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23309069 (22M) [text/plain]
Saving to: âOracle_VM_VirtualBox_Extension_Pack-6.0.8.vbox-extpackâ

100%[======================================>] 23,309,069   595KB/s   in 40s

2019-06-16 11:54:06 (570 KB/s) - âOracle_VM_VirtualBox_Extension_Pack-6.0.8.vbox-extpackâ saved [23309069/23309069]

Install VirtualBox Extension Pack using VBoxManage command.

# VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-6.0.8.vbox-extpack
VirtualBox Extension Pack Personal Use and Evaluation License (PUEL)

License version 10, 20 July 2017
...
...
...

Do you agree to these license terms and conditions (y/n)? y

License accepted. For batch installation add
--accept-license=56be48f923303c8cababb0bb4c478284b688ed23f16d775d729b89a2e8e5f9eb
to the VBoxManage command line.

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Successfully installed "Oracle VM VirtualBox Extension Pack".

VirtualBox 6.0 and VirtualBox Extension Pack have been successfully installed on CentOS 7 server.

 

Installing Vagrant on CentOS 7:

Installing prerequisite packages for Vagrant using yum command.

# yum install -y rsync

Latest version of Vagrant is available on Vagrant Official Download Page.

Download Vagrant RPM using wget command.

# wget https://releases.hashicorp.com/vagrant/2.2.4/vagrant_2.2.4_x86_64.rpm
--2019-06-16 12:05:28--  https://releases.hashicorp.com/vagrant/2.2.4/vagrant_2.2.4_x86_64.rpm
Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.9.183, 2a04:4e42:2::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.9.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 40824607 (39M) [application/x-redhat-package-manager]
Saving to: âvagrant_2.2.4_x86_64.rpmâ

100%[======================================>] 40,824,607   566KB/s   in 73s

2019-06-16 12:06:41 (549 KB/s) - âvagrant_2.2.4_x86_64.rpmâ saved [40824607/40824607]

Install Vagrant RPM using rpm command.

# rpm -ivh vagrant_2.2.4_x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:vagrant-1:2.2.4-1                ################################# [100%]

Create a home directory to store Vagrant projects.

# mkdir ~/vagrant-home
# cd ~/vagrant-home/

Create a new project directory and create a vagrantfile therein for Alpine Linux.

# mkdir alpine
# cd alpine
# vagrant init generic/alpine38
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.

We have initialized a new Vagrant project with generic/alpine38 vagrant box.

You can find a Vagrant box as per your choice of operating system and application software at Discover Vagrant Boxes.

Start the Vagrant Project using vagrant command.

# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'generic/alpine38' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'generic/alpine38'
    default: URL: https://vagrantcloud.com/generic/alpine38
==> default: Adding box 'generic/alpine38' (v1.9.14) for provider: virtualbox
    default: Downloading: https://vagrantcloud.com/generic/boxes/alpine38/versions/1.9.14/providers/virtualbox.box
    default: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
==> default: Successfully added box 'generic/alpine38' (v1.9.14) for 'virtualbox'!
==> default: Importing base box 'generic/alpine38'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'generic/alpine38' version '1.9.14' is up to date...
==> default: Setting the name of the VM: alpine_default_1560671657772_73973
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...

We have successfully installed Vagrant with VirtualBox on CentOS 7. Before using Vagrant, we recommend that you should read Vagrant: Up and Running by O'Reilly Media to have a thorough understanding of Vagrant concepts and command usage.

If you find this article useful? Don't you mind support us by Buy Me A Coffee


2 comments:

  1. after vagrant run commant,

    There was an error while executing `VBoxManage`, a CLI used by Vagrant
    for controlling VirtualBox. The command and stderr is shown below.

    Command: ["startvm", "f98d8452-e049-4f65-a37a-4a1cdfe8bdd8", "--type", "headless"]

    Stderr: VBoxManage: error: VT-x is not available (VERR_VMX_NO_VMX)
    VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

    ReplyDelete
  2. Your machine does not support VT-x or virtualization. If you are executing above steps on a virtual machine then you have to enable virtualization support from VM settings.

    ReplyDelete

© 2022 CentLinux. All Rights Reserved.