Onur Yasarlar
Search…
How to Install VirtualBox on CentOS 7/8 and Ubuntu 18

Getting Started

I really like testing new things in my home lab but I also often break things. I am really bored with installing my home lab over and over again so I decided to create a series of posts to show how I automated my home lab provisioning. I personally prefer KVM over VirtualBox but I wanted to also document how to install it with an Ansible role. There are tons of writings on how to install VirtualBox on various operating systems including the official documentation and I ensure you what I implemented here is no magic or different than what others already documented. I just wanted to keep it in an Ansible module under my own GitHub repository and refer to it if I need some time in the future. I will also mention here how I like to structure my Ansible roles and my habits while I am automating things.
Special thanks to Jeff Geerling. I learned a lot from his work while I am writing my own Ansible roles
You can find my Ansible role in Ansible Galaxy here.
I usually test things under CentOS but after Red Hat's recent announcement of converting CentOS into a beta version of RHEL, I decided to give it a try with Ubuntu and added Ubuntu support to this role recently. The role covers operating systems below but I can add more in the future:
  • CentOS 7
  • CentOS 8
  • Ubuntu 18.x
Ubuntu 20.x has not been certified yet officially by Oracle. The support for Ubuntu 20.x will be added later.

Prerequisites

I usually prefer to have a separate prerequisite play and include it in my main.yml. I start checking if the role is being run for the operating systems that I tested. Here I added another check and verified if the CPU of the server supports virtualization and it is indeed 64-bit.
tasks/prereq.yaml
1
---
2
- name: Setting OS version fact
3
set_fact:
4
osversion: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"
5
6
- name: Check OS version
7
fail:
8
msg: "OS Version( {{ ansible_distribution }}{{ ansible_distribution_major_version }} ) is not certified for the role"
9
when:
10
- osversion != "ubuntu18"
11
- osversion != "centos7"
12
- osversion != "centos8"
13
14
- name: Check CPU Virtualization Support
15
shell:
16
cmd: "egrep -c '(vmx|svm)' /proc/cpuinfo"
17
args:
18
warn: false
19
changed_when: false
20
register: cpuinfo_output
21
tags: ['skip_ansible_lint']
22
23
- name: Check CPU Virtualization Support
24
fail:
25
msg: "CPU does not support virtualization"
26
when:
27
- cpuinfo_output.stdout == "0"
28
29
- name: Check if your processor is 64-bit
30
shell:
31
cmd: "egrep -c ' lm ' /proc/cpuinfo"
32
args:
33
warn: false
34
changed_when: false
35
register: bitinfo_output
36
tags: ['skip_ansible_lint']
37
38
- name: Check if your processor is 64-bit
39
fail:
40
msg: "CPU is not 64-bit"
41
when:
42
- bitinfo_output.stdout == "0"
43
Copied!
For VirtualBox installation, we only require to provide the version information which I keep under vars/main.yml file:
vars/main.yml
1
---
2
# vars file for virtualbox
3
virtualbox_version: "6.1"
4
Copied!

Installation on CentOS 7/8

The remaining of this section is no magic. We will just install the required packages and enable PowerTools repository if we will install VirtualBox to CentOS 8 operating system.
tasks/centos.yaml
1
---
2
# yum_repository module does not allow to only enable a yum_repository
3
# so I deploy full repo file instead
4
- name: "Enable PowerTools repository"
5
copy:
6
src: CentOS-PowerTools.repo
7
dest: "/etc/yum.repos.d/CentOS-PowerTools.repo"
8
owner: "root"
9
group: "root"
10
mode: "0644"
11
when: ansible_facts['distribution_major_version'] == "8"
12
13
- name: "Install prerequisite packages"
14
package:
15
name:
16
- "gcc"
17
- "make"
18
- "perl"
19
- "kernel-devel"
20
- "kernel-headers"
21
- "elfutils-libelf-devel"
22
- "xorg-x11-xauth"
23
- "xorg-x11-apps"
24
state: present
25
26
- name: "Add VirtualBox repository"
27
yum_repository:
28
name: "virtualbox"
29
description: "OEL / RHEL / CentOS-$releasever / $basearch - VirtualBox"
30
baseurl: "http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch"
31
gpgkey: "https://www.virtualbox.org/download/oracle_vbox.asc"
32
gpgcheck: "yes"
33
repo_gpgcheck: "yes"
34
35
- name: Import VirtualBox repo GPG key
36
rpm_key:
37
state: present
38
key: https://www.virtualbox.org/download/oracle_vbox.asc
39
40
- name: "Install VirtualBox-{{ virtualbox_version }}"
41
yum:
42
name: "VirtualBox-{{ virtualbox_version }}"
43
state: present
44
Copied!

Installation on Ubuntu 18.x

The remaining of this section is no magic. We will just install the required packages.
tasks/ubuntu.yaml
1
---
2
- name: Install prereq packages
3
apt:
4
name:
5
- build-essential
6
- dkms
7
- unzip
8
state: present
9
update_cache: true
10
11
- name: Add VirtualBox repo keys
12
apt_key:
13
url: "https://www.virtualbox.org/download/{{ item }}.asc"
14
state: present
15
loop:
16
- oracle_vbox
17
- oracle_vbox_2016
18
19
- name: Add VirtualBox repo
20
apt_repository:
21
repo: 'deb https://download.virtualbox.org/virtualbox/debian {{ ansible_distribution_release }} contrib'
22
state: present
23
update_cache: true
24
25
- name: "Install virtualbox in version {{ virtualbox_version }}"
26
apt:
27
name: virtualbox-{{ virtualbox_version }}
28
state: present
29
Copied!

Putting things together

Let me also show how all above put together in the main.yml. I tend to keep main.yml as simple as possible and perform tasks under their dedicated plays.
tasks/main.yml
1
---
2
- name: CPU support checks
3
include_tasks: prereq.yaml
4
5
- name: Install and Configure for CentOS
6
include_tasks: centos.yaml
7
when: ansible_distribution == "CentOS"
8
9
- name: Install and Configure for Ubuntu
10
include_tasks: ubuntu.yaml
11
when: ansible_distribution == "Ubuntu"
Copied!
So we are ready to call this role if we want to install KVM on top of CentOS 7/8 or Ubuntu 18.x/20.x
It is important to run this module as root. Otherwise, it will not be able to install packages and will eventually fail.