Contents

Creating and Running a Linux Virtual Machine

Design and run custom Linux guests on Apple silicon or Intel-based Mac Computers.

Overview

Running a Linux virtual machine (VM) is an additive process that starts with selecting a Linux distribution and obtaining Linux kernel and RAM disk images, and ends with instantiating and running the Linux VM on the user’s computer.

There many Linux distributions to choose from, select one that both meets your app’s needs in terms of device and application support, and supports ARM64 or 64-bit Intel-based CPUs. You’ll also need to decide how much of the host computer’s CPU resources and memory to dedicate to the VM in order to configure a VZVirtualMachineConfiguration. Unless your VM is solving self-contained computing problems, it’s likely your VM needs to interact with the user, access the network, and so on, so you’ll want to add devices to the VM to make this possible.

Finally, after creating a suitable VZVirtualMachineConfiguration, you’ll use a VZLinuxBootLoader to launch your VM and a VZVirtualMachine instance to control it.

Obtain the Kernel and RAM Disk Images

You can obtain images for many Linux distributions, including Ubuntu Linux. Most Linux distributions provide downloadable kernel and RAM disk images that can run on Mac computers; however, you’ll need to download images for your specific CPU:

  • For Apple silicon Mac computers, download ARM 64-bit images.

  • For Intel-based Mac computers, download AMD64 images.

If your app is universal, you’ll need to obtain images for both Apple silicon and Intel-based Mac computers in order to be able to run the appropriate Linux VM on the respective Apple silicon or Intel-based Mac computer.

Some Linux distributions store kernel images in a compressed format, sometimes indicated by a .gz filename extension. If the image doesn’t have a file extension, you can determine the file’s compression status using the file command. After determining the file format for the image, add the appropriate filename extension. For example, if the output shows the file format as “gzip compressed data,” add the .gz extension to the image, and then use the gzip or gunzip command to unpack the image. Running file against the unpacked image reveals details about the kernel image you’re verifying.

You can include the Linux kernel and RAM disk as resources in your Xcode project for use in your app, or implement a custom solution that allows the user to download and save images over the network.

Set Up the Linux VM Configuration

Configure a VZVirtualMachineConfiguration with the number of CPUs and amount of memory you want to use with your VM:

Add Devices to Your VM

Next, you’ll need to decide what devices your Linux VM needs. Because you can’t change or add devices to a running VM, consider what devices you’ll need to support your app’s use cases and add them to the VM’s configuration. For example, if you need:

The example below adds a serial console, and sound input and output devices to the VM’s configuration:

For more information on devices that Linux guests can support, see the Devices section on the Virtualization framework.

Create a Boot Loader

Create an instance of a VZLinuxBootLoader on the VZVirtualMachineConfiguration with a Linux kernel and a RAM disk that you load from locations on disk:

Instantiate and Run the Linux VM

Instantiate a VZVirtualMachine from the VZVirtualMachineConfiguration that you use to start, stop, and control your Linux guest:

See Also

Virtual machine setup