March 6, 2018 · EN snippets

Install Oracle VirtualBox on Ubuntu & Debian

Default ubuntu repos do not seem to contain the latest version of Oracle VirtualBox. However, it can easily be installed on Ubuntu (17.10 & 16.04 and 14.04) and Debian (9 & 8) with the following instructions. Copy and paste the following python lines into a python file named install_virtualbox.py and execute it with sudo privilleges.

The code will determine your OS version, fetch the appropriate packages and install it.

import subprocess
import os

version = subprocess.check_output(['lsb_release', '-a'])

ub1710 = 'deb http://download.virtualbox.org/virtualbox/debian zesty contrib'
ub1604 = 'deb http://download.virtualbox.org/virtualbox/debian xenial contrib'
ub1404 = 'deb http://download.virtualbox.org/virtualbox/debian trusty contrib'
deb9   = 'deb http://download.virtualbox.org/virtualbox/debian stretch contrib'
deb8   = 'deb http://download.virtualbox.org/virtualbox/debian jessie contrib'

# step 1 : edit /etc/apt/sources.list

sources_list = '/etc/apt/sources.list'
sources_file = open(sources_list, 'a')

if 'ubuntu' in version.lower():
    if '17.10' in version:
        sources_file.write(ub1710)
    elif '16.04' in version:
        sources_file.write(ub1604)
    elif '14.04' in version:
        sources_file.write(ub1404)
elif 'debian' in version.lower():
    if '9' in version:
        sources_file.write(deb9)
    elif '8' in version:
        sources_file.write(deb8)

sources_file.close()

# step 2 : add sign keys

os.system('wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add -')
os.system('wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -')

# step 3 : install virtualbox

os.system('apt-get update')
os.system('apt-get install virtualbox-5.2')

print ('\nVirtualbox has been installed!\n')

Run the script

sudo python install_virtualbox.py

That should install the latest version (as of now 5.2.8) of VirtualBox.

The complete script can be downloaded here.*

* md5sum : 4880da544333f72aa1652f5547a24d67