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.

 

This Article Provides:

     

    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.

    [root@vagrant-01 ~]# yum install -y kernel-devel-$(uname -r) kernel-headers gcc make perl wget
    ...
    Installed:
    gcc.x86_64 0:4.8.5-36.el7_6.2
    kernel-devel.x86_64 0:3.10.0-957.el7
    kernel-headers.x86_64 0:3.10.0-957.21.2.el7
    perl.x86_64 4:5.16.3-294.el7_6
    wget.x86_64 0:1.14-18.el7_6.1

    Dependency Installed:
    cpp.x86_64 0:4.8.5-36.el7_6.2
    glibc-devel.x86_64 0:2.17-260.el7_6.5
    glibc-headers.x86_64 0:2.17-260.el7_6.5
    libmpc.x86_64 0:1.0.1-3.el7
    mpfr.x86_64 0:3.1.1-4.el7
    perl-Carp.noarch 0:1.26-244.el7
    perl-Encode.x86_64 0:2.51-7.el7
    perl-Exporter.noarch 0:5.68-3.el7
    perl-File-Path.noarch 0:2.09-2.el7
    perl-File-Temp.noarch 0:0.23.01-3.el7
    perl-Filter.x86_64 0:1.49-3.el7
    perl-Getopt-Long.noarch 0:2.40-3.el7
    perl-HTTP-Tiny.noarch 0:0.033-3.el7
    perl-PathTools.x86_64 0:3.40-5.el7
    perl-Pod-Escapes.noarch 1:1.04-294.el7_6
    perl-Pod-Perldoc.noarch 0:3.20-4.el7
    perl-Pod-Simple.noarch 1:3.28-4.el7
    perl-Pod-Usage.noarch 0:1.63-3.el7
    perl-Scalar-List-Utils.x86_64 0:1.27-248.el7
    perl-Socket.x86_64 0:2.010-4.el7
    perl-Storable.x86_64 0:2.45-3.el7
    perl-Text-ParseWords.noarch 0:3.29-4.el7
    perl-Time-HiRes.x86_64 4:1.9725-3.el7
    perl-Time-Local.noarch 0:1.2300-2.el7
    perl-constant.noarch 0:1.27-2.el7
    perl-libs.x86_64 4:5.16.3-294.el7_6
    perl-macros.x86_64 4:5.16.3-294.el7_6
    perl-parent.noarch 1:0.225-244.el7
    perl-podlators.noarch 0:2.5.1-3.el7
    perl-threads.x86_64 0:1.87-4.el7
    perl-threads-shared.x86_64 0:1.43-6.el7

    Dependency Updated:
    glibc.x86_64 0:2.17-260.el7_6.5 glibc-common.x86_64 0:2.17-260.el7_6.5
    libgcc.x86_64 0:4.8.5-36.el7_6.2 libgomp.x86_64 0:4.8.5-36.el7_6.2

    Complete!

    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.

    [root@vagrant-01 ~]# 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.

    [root@vagrant-01 ~]# cd /tmp
    [root@vagrant-01 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.

    [root@vagrant-01 tmp]# rpm --import oracle_vbox.asc

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

    [root@vagrant-01 tmp]# 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.

    [root@vagrant-01 tmp]# 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.

    [root@vagrant-01 tmp]# yum install -y VirtualBox-6.0
    ...
    Installed:
    VirtualBox-6.0.x86_64 0:6.0.8_130520_el7-1

    Dependency Installed:
    SDL.x86_64 0:1.2.15-14.el7
    dejavu-fonts-common.noarch 0:2.33-6.el7
    dejavu-sans-fonts.noarch 0:2.33-6.el7
    fontconfig.x86_64 0:2.13.0-4.3.el7
    fontpackages-filesystem.noarch 0:1.44-8.el7
    libICE.x86_64 0:1.0.9-9.el7
    libSM.x86_64 0:1.2.2-2.el7
    libX11.x86_64 0:1.6.5-2.el7
    libX11-common.noarch 0:1.6.5-2.el7
    libXau.x86_64 0:1.0.8-2.1.el7
    libXcursor.x86_64 0:1.1.15-1.el7
    libXdamage.x86_64 0:1.1.4-4.1.el7
    libXext.x86_64 0:1.3.3-3.el7
    libXfixes.x86_64 0:5.0.3-1.el7
    libXmu.x86_64 0:1.1.2-2.el7
    libXrender.x86_64 0:0.9.10-1.el7
    libXt.x86_64 0:1.1.5-3.el7
    libXxf86vm.x86_64 0:1.1.4-1.el7
    libglvnd.x86_64 1:1.0.1-0.8.git5baa1e5.el7
    libglvnd-glx.x86_64 1:1.0.1-0.8.git5baa1e5.el7
    libvpx.x86_64 0:1.3.0-5.el7_0
    libxcb.x86_64 0:1.13-1.el7
    libxshmfence.x86_64 0:1.2-1.el7
    mesa-libGL.x86_64 0:18.0.5-4.el7_6
    mesa-libglapi.x86_64 0:18.0.5-4.el7_6
    opus.x86_64 0:1.0.2-6.el7

    Complete!

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

    Check status of vboxdrv.service.

    [root@vagrant-01 tmp]# 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.

    [root@vagrant-01 tmp]# 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.

    [root@vagrant-01 tmp]# 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.

    [root@vagrant-01 centos7]# yum install -y rsync
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: mirrors.ges.net.pk
    * extras: mirrors.ges.net.pk
    * updates: mirrors.ges.net.pk
    Resolving Dependencies
    --> Running transaction check
    ---> Package rsync.x86_64 0:3.1.2-6.el7_6.1 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    ================================================================================
    Package Arch Version Repository Size
    ================================================================================
    Installing:
    rsync x86_64 3.1.2-6.el7_6.1 updates 404 k

    Transaction Summary
    ================================================================================
    Install 1 Package

    Total download size: 404 k
    Installed size: 815 k
    Downloading packages:
    rsync-3.1.2-6.el7_6.1.x86_64.rpm | 404 kB 00:00
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
    Warning: RPMDB altered outside of yum.
    Installing : rsync-3.1.2-6.el7_6.1.x86_64 1/1
    Verifying : rsync-3.1.2-6.el7_6.1.x86_64 1/1

    Installed:
    rsync.x86_64 0:3.1.2-6.el7_6.1

    Complete!

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

    Download Vagrant RPM using wget command.

    [root@vagrant-01 tmp]# 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.

    [root@vagrant-01 tmp]# 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.

    [root@vagrant-01 tmp]# mkdir ~/vagrant-home
    [root@vagrant-01 tmp]# cd ~/vagrant-home/

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

    [root@vagrant-01 vagrant-home]# mkdir alpine
    [root@vagrant-01 vagrant-home]# cd alpine
    [root@vagrant-01 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.

    [root@vagrant-01 alpine]# 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.

    0 Comments