Using VirtualBox for virtual machines
There are several options for running virtual machines on your workstation. These include Parallels (for macOS), KVM/QEMU (for Linux), VMware (commercial for several host operating systems), and VirtualBox (an Oracle product). We'll use VirtualBox because it is open source and free to use. It's also portable in the sense that you can run VirtualBox and your virtual machines on Linux, Windows, macOS, and other host operating systems.
Introduction to virtualization
Virtualization uses special instructions and features of your workstation's CPU to run a generic pseudo-computer system (virtual machine) on your host. Within this virtual machine, you can install a wide range of operating systems, including various versions of Windows Server, Linux, BSD, and so on. The operating system running in a virtual machine is called the guest operating system; the operating system running on your workstation is called the host operating system.
As the guest operating system executes code, it will be required to perform disk and network access, execute privileged CPU instructions, and otherwise access shared resources with the host. The virtualization software effectively traps these guest operating system accesses and translates them into host operating system calls. Thus, code running in the virtual machine is mostly running at full native CPU speed until these shared access traps are executed – then there is some overhead for the translation to host accesses.
The guest virtual machines may be configured before you install an operating system within. You can set how much RAM to use, one or more virtual disks, one or more Ethernet controllers, a graphics card, an ISO file (installation media) to insert in the virtual CD-ROM drive, and so on.
You typically set RAM, disk space, and the number of virtual CPU cores to appropriate values for your guest operating system and the apps you intend to use within the guest. For example, if you are going to run Windows in a virtual machine, you might want to give it at least 2 virtual CPU cores and 8 gigabytes of RAM and 32 gigabytes of disk space. If you are going to run an application in the virtual machine that needs more than 8 gigabytes of memory, you would want to assign more RAM; if the app needs a lot of disk space, you would assign more disk space.
Creating a virtual machine
To boot the virtual machine, use the VirtualBox program (user interface). When the virtual machine boots, it acts just like a physical PC – as far as the installer on the installation media is concerned, it is a physical PC. The installer will work as if you were installing on a new PC or reinstalling on your PC.
A virtual machine may present its console or desktop within a window on your workstation's desktop, or it can be headless. A headless virtual machine is similar to a server machine – you access it via FTP, SSH, and so on. You would use a headless virtual machine when you have no use for the operating system console or graphical interface. The headless machine provides all the services of a server you would remotely access.
You start a headless virtual machine from the command line instead of the VirtualBox user interface program. This is done via the VBoxManage command, which is documented here: https://www.virtualbox.org/manual/ch08.html. It is more likely that you will be using a guest operating system with a graphical user interface, though.
A typical headless virtual machine might be used to run a LAMP application—Linux, Apache, MySQL, and PHP all contained neatly within the virtual machine and not within the filesystem of your workstation. You can model a scalable LAMP application by starting a headless virtual machine that runs MySQL and two headless virtual machines that run the HTTP server and the PHP code.
A typical graphics/desktop virtual machine might be used to run Windows in a window on your Mac computer, to run Linux in a window on your Mac computer, to run Linux in a window on your Windows machine, and so on. If you like to use Linux, but you need to run Windows programs, doing it in a virtual machine is a good way to go.
A non-headless install will have a few display options. The entire desktop can be displayed in a window on your host's desktop. This is the default display mode. The window can be resized like any other window on the desktop. However, within the interior of the window, the guest's desktop will not resize to fit until you install the VirtualBox guest additions in the guest.
The guest window can be made full screen. This makes the guest look like it's the operating system running native on the workstation. If you are running macOS, you can switch desktops using the macOS gestures and go back and forth between full-screen Windows and full-screen macOS desktops.
For some host operating systems, the guest can be put into seamless mode, where the desktop is not displayed at all, but any applications running in the virtual machine render their windows on top of the host desktop.
The result is a mixture of virtual machine application windows and your host operating system application menus on your desktop, as shown in the following screenshot:
As you can see, you may run and manage a full Windows installation on your workstation within a virtual machine. You can access the files and directories on your host if you set up Samba for file sharing on the host.
Incidentally, portions of this book were written using Microsoft Word 365, running in a Windows 10 virtual machine on a Linux host. The Docker examples that follow were executed on the Linux host. This is a great example of why you would run a virtual machine.
Note:
Microsoft allows you to buy a Windows 10 license and use it to activate Windows 10 within a virtual machine.
Apple only allows macOS to be run in a virtual machine on Apple hardware. It is a violation of their licensing terms to run macOS within a virtual machine on a PC running Windows or Linux.
Linux and most BSD variants are generally free to use on a PC or within a virtual machine on a PC.
Guest additions
For Windows and Linux guest operating systems, you can install drivers that fully integrate the guest and host operating systems. These drivers are known as guest additions and you can download these from the VirtualBox site: https://virtualbox.org. They are installed within the virtual machine as any program you install for Windows or Linux. The integration with the host is quite useful.
The guest additions display drivers that allow you to use the full resolution of the host's screen and, if you're running in windowed mode (guest desktop in a host desktop window), resizing the window will cause the guest desktop to resize to fit the new window size. If you want to use the seamless windows feature, you are required to install the guest display drivers.
The additions provide mouse pointer integration. This allows you to freely move the cursor between physical screens, from guest windows to host windows. Otherwise, the mouse would be captured by the virtual machine so that it can manage pointer events.
The guest additions also share the host and guest clipboards as if they were one clipboard. You can select and copy text in a macOS host application and then paste that copied text into a Windows application running in the virtual machine.
For Linux guests, the additions allow you to share host filesystem directories and files. This is particularly useful because you can use the host operating system tools and software to develop files seen by the host. For example, you create a shared folder on your macOS machine for your project's working directory. You can use your macOS editors to edit files in the project and, in the virtual machine, you can run Linux native compilers or tools to execute your project. Let's now begin by installing VirtualBox.
Installing VirtualBox
The URL for VirtualBox is https://www.virtualbox.org/. There, you can find documentation and downloads for the various host platforms (workstation operating systems), add-ons, see screenshots, see recommended third-party software that works with VirtualBox, and so on.
Windows installation instructions
To install the Windows installation, go to the downloads page at the VirtualBox site, download the installer for the latest version, and then, when the download is complete, double-click on it. Then, follow the onscreen instructions.
macOS installation instructions
For macOS installation, you can use Homebrew or download the installer .dmg file from the VirtualBox site and install from that. To use Homebrew, you only need to enter one command:
$ brew cask install virtualbox
Homebrew (https://brew.sh/) is the missing package manager for macOS. It is a command-line system for installing software from Homebrew's repositories. It is a terrific tool for augmenting the software shipped with macOS. The software in those repositories is updated far more frequently than the Apple software updates.
Linux installation instructions
The installation instructions for VirtualBox on Linux varies depending on the Linux distribution that you use on your workstation. Since there are so many different distributions, we'll cover Ubuntu to give you an idea of what to do and provide you with helpful pointers for installing VirtualBox on other distributions (Arch Linux, Fedora, and suchlike).
For Ubuntu, you can install VirtualBox from the Ubuntu Software Center, download a .deb file from the VirtualBox site, or use apt:
$ sudo apt install virtualbox
For Arch Linux and its variants, you can follow the instructions on the terrific Arch wiki at https://wiki.archlinux.org/index.php/VirtualBox.
For Fedora or other RPM-based Linux distributions, follow the instructions at the VirtualBox site: https://virtualbox.org. Let's now learn how to use Docker containers.