Create An Ubuntu Virtual Machine with Multipass
Tags: webdev, vm, linux
Published: 2022-09-25 14:36:44

I use an M1 MacBook Pro as my development machine and I sometimes want to prototype, test or experiment on an Ubuntu box. A local virtual machine (VM) is a perfect solution. Local VMs are quick and easy to spin up, use and then just as easily they can be reset or discarded. They can be ideal for trying things out or to use as a local Linux development environment without the expense and hassle of using a remote hosting provider.

Mac users have several options for local VMs but this article will describe a tool called [Multipass](https://github.com/canonical/multipass). This software is sponsored by Canonical, the makers of the popular [Ubuntu linux](https://en.wikipedia.org/wiki/Ubuntu) OS distribution, so it is naturally tailored to creating Ubuntu-based VMs.

## Installing MultiPass

If you don't have it already, install the [Homebrew Package Manager](https://brew.sh/) for MacOS.

Once `brew` is installed you can access it from the terminal command-line interface (CLI).

```bash
% brew --version                                                         
  Homebrew 3.6.5
  Homebrew/homebrew-core (git revision 2066e0a006e; last commit 2022-10-16)
  Homebrew/homebrew-cask (git revision fcfd0d19f9; last commit 2022-10-16)
```

Use the `brew install` command to install the `multipass` package. The installer will need your permission to do its job; to grant access enter your password when prompted.

```bash
% brew install --cask multipass
  ...
  ==> Installing Cask multipass
  ==> Running installer for multipass; your password may be necessary.
  Package installers may write to any location; options such as `--appdir` are ignored.
  Password:
  installer: Package name is multipass
  installer: Installing at base path /
  installer: The install was successful.
  🍺  multipass was successfully installed!
```

Once `multipass` is installed you can access it from the terminal CLI.

```bash
% which multipass
  /usr/local/bin/multipass
% multipass --version
  multipass   1.10.1+mac
  multipassd  1.10.1+mac
```

If you ever change your mind use `brew uninstall` to remove it.

```bash
% brew uninstall multipass
# or to uninstall and destroy all related data too
% brew uninstall --zap multipass
```

## Creating Instances

An instance is an installed virtual machine. Our goal is to have an instance with Ubuntu running in a local VM.

Your first decision is which release of Ubuntu you would like to have installed on your instance. Use `multipass find` to see a list of all the available releases.

```bash
% multipass find
  Image                       Aliases           Version          Description
  18.04                       bionic            20220921         Ubuntu 18.04 LTS
  20.04                       focal             20220920         Ubuntu 20.04 LTS
  22.04                       jammy,lts         20220923         Ubuntu 22.04 LTS
  anbox-cloud-appliance                         latest           Anbox Cloud Appliance
  charm-dev                                     latest           A development and testing environment for charmers
  docker                                        latest           A Docker environment with Portainer and related tools
  jellyfin                                      latest           Jellyfin is a Free Software Media System that puts you in control of managing and streaming your media.
  minikube                                      latest           minikube is local Kubernetes
```

Use the `multipass launch` command to then create an instance. You can allow multipass to choose all the default settings for you (including a random instance name) or you can specify as many of the creation parameters as you like.

In my case I want to download, install and launch a VM instance running Ubuntu 20.04, using my preferred name "vm-ubuntu20" with 2 virtual CPUs, 3GB RAM, and a 10GB drive.

```bash
% multipass launch --cpus 2 --mem 3G --disk 10G --name vm-ubuntu20 20.04
  Launched: vm-ubuntu20 
```

## List Your Instances

If you've followed along so far, you should have an Ubuntu VM instance installed and running now. To check that this is so, you can use `multipass ls` to see a list of all your instances.

```bash
% multipass ls                                                          
  Name                    State             IPv4             Image
  vm-ubuntu20             Running           192.168.64.2     Ubuntu 20.04 LTS
```

## Connect to an Instance Through Multipass

Use the `multipass sh` command to easily create an SSH connection to an instance, providing the name of the instance you want to connect to.

```bash
% multipass sh vm-ubuntu20
  Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.4.0-126-generic aarch64)
  ...
```

You will then be able to run any normal [BASH commands](https://www.educative.io/blog/bash-shell-command-cheat-sheet) on your VM. To exit the SSH connection use the `exit` command.

## Stopping an Instance

Use the `multipass stop` command to stop a running instance, providing the name of the instance you want to stop.

```bash
% multipass stop vm-ubuntu20
% multipass ls               
  Name                    State             IPv4             Image
  vm-ubuntu20             Stopped           --               Ubuntu 20.04 LTS
```

_NOTE: I've found that if I accidentally leave an instance running when I close the lid on my laptop, it can leave me with a badly corrupted instance the next day. This is probably due to some bug in MacOS, multipass or one of the dependencies of multipass, but it's serious enough that I am now very careful to always stop running instances before I walk away from my laptop for the day._

## Starting an Instance

Use the `multipass start` command to start a stopped instance, providing the name of the instance you want to start.

```bash
% multipass start vm-ubuntu20
% multipass ls                
  Name                    State             IPv4             Image
  vm-ubuntu20             Running           192.168.64.2     Ubuntu 20.04 LTS
```

## Delete an Instance

Use the `multipass delete` command to delete an instance, providing the name of the instance you want to delete.

```bash
% multipass stop vm-ubuntu20
% multipass delete vm-ubuntu20
```

At this point you could still [recover](https://multipass.run/docs/recover-command) the deleted instance. The `multipass purge` command will make the delete permanent.

## Other Basic Commands Instance

Also available are the `multipass restart` and `multipass suspend` commands.