Onur Yasarlar
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.


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.
- name: Setting OS version fact
osversion: "{{ ansible_distribution | lower }}{{ ansible_distribution_major_version }}"
- name: Check OS version
msg: "OS Version( {{ ansible_distribution }}{{ ansible_distribution_major_version }} ) is not certified for the role"
- osversion != "ubuntu18"
- osversion != "centos7"
- osversion != "centos8"
- name: Check CPU Virtualization Support
cmd: "egrep -c '(vmx|svm)' /proc/cpuinfo"
warn: false
changed_when: false
register: cpuinfo_output
tags: ['skip_ansible_lint']
- name: Check CPU Virtualization Support
msg: "CPU does not support virtualization"
- cpuinfo_output.stdout == "0"
- name: Check if your processor is 64-bit
cmd: "egrep -c ' lm ' /proc/cpuinfo"
warn: false
changed_when: false
register: bitinfo_output
tags: ['skip_ansible_lint']
- name: Check if your processor is 64-bit
msg: "CPU is not 64-bit"
- bitinfo_output.stdout == "0"
For VirtualBox installation, we only require to provide the version information which I keep under vars/main.yml file:
# vars file for virtualbox
virtualbox_version: "6.1"

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.
# yum_repository module does not allow to only enable a yum_repository
# so I deploy full repo file instead
- name: "Enable PowerTools repository"
src: CentOS-PowerTools.repo
dest: "/etc/yum.repos.d/CentOS-PowerTools.repo"
owner: "root"
group: "root"
mode: "0644"
when: ansible_facts['distribution_major_version'] == "8"
- name: "Install prerequisite packages"
- "gcc"
- "make"
- "perl"
- "kernel-devel"
- "kernel-headers"
- "elfutils-libelf-devel"
- "xorg-x11-xauth"
- "xorg-x11-apps"
state: present
- name: "Add VirtualBox repository"
name: "virtualbox"
description: "OEL / RHEL / CentOS-$releasever / $basearch - VirtualBox"
baseurl: "http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch"
gpgkey: "https://www.virtualbox.org/download/oracle_vbox.asc"
gpgcheck: "yes"
repo_gpgcheck: "yes"
- name: Import VirtualBox repo GPG key
state: present
key: https://www.virtualbox.org/download/oracle_vbox.asc
- name: "Install VirtualBox-{{ virtualbox_version }}"
name: "VirtualBox-{{ virtualbox_version }}"
state: present

Installation on Ubuntu 18.x

The remaining of this section is no magic. We will just install the required packages.
- name: Install prereq packages
- build-essential
- dkms
- unzip
state: present
update_cache: true
- name: Add VirtualBox repo keys
url: "https://www.virtualbox.org/download/{{ item }}.asc"
state: present
- oracle_vbox
- oracle_vbox_2016
- name: Add VirtualBox repo
repo: 'deb https://download.virtualbox.org/virtualbox/debian {{ ansible_distribution_release }} contrib'
state: present
update_cache: true
- name: "Install virtualbox in version {{ virtualbox_version }}"
name: virtualbox-{{ virtualbox_version }}
state: present

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.
- name: CPU support checks
include_tasks: prereq.yaml
- name: Install and Configure for CentOS
include_tasks: centos.yaml
when: ansible_distribution == "CentOS"
- name: Install and Configure for Ubuntu
include_tasks: ubuntu.yaml
when: ansible_distribution == "Ubuntu"
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.