Mastering Container Development on Mac: Introduction to lima-vm/lima
Lima, short for Linux virtual machines, is a tool designed to run Linux virtual machines (VMs) on macOS, similar in concept to WSL 2 on Windows. Its main focus is on creating environments specifically for running containers (like Docker or containerd).
For a software engineer using macOS, Lima is incredibly useful for several reasons
Linux Environment Parity
It allows you to develop and test your applications in a genuine Linux environment right on your Mac. This is crucial for applications that are designed to be deployed on Linux servers, helping you catch OS-specific issues early.
Container Runtime
Lima integrates well with container runtimes like containerd (which it supports by default) and Docker. This means you can run Linux containers natively, which is often faster and more resource-efficient than running Docker Desktop's built-in VM.
Seamless Integration
It automates key features that make working with the VM hassle-free
Automatic File Sharing
Your macOS project directories can be automatically shared with the Linux VM. No more complex manual configurations!
Automatic Port Forwarding
Services running inside the VM (e.g., a web server on port 8080) are automatically accessible from your macOS host machine.
Lightweight and Open Source
Lima is a lightweight, open-source alternative that utilizes QEMU for virtualization, giving you more control and transparency compared to some proprietary solutions.
In short, Lima makes it easy to have a fast, native-feeling Linux environment for containerized development on your Mac.
The easiest way to install Lima on macOS is by using Homebrew.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install lima
Once installed, you can start your first VM. Lima uses "templates" for configurations. The simplest way is to use the default template
limactl start
This command will
Download a suitable Linux image (usually a recent Ubuntu LTS or similar).
Create a VM configuration based on the default settings.
Start the VM using QEMU.
Set up file sharing and port forwarding.
The first time you run this, it will take a moment to download the image.
Once the VM is running, you'll primarily interact with it using limactl shell or by defining custom configurations.
To connect to your running Lima instance (the default one is usually named default)
limactl shell
Now you are inside the Linux VM and can use standard Linux commands
$ limactl shell
> pwd
/home/lima
> ls -l /tmp/lima
total 0
# You are now in a Linux shell!
To exit the VM shell, just type exit.
Since Lima often comes pre-configured with containerd, you can run a container directly.
$ limactl shell
> sudo crictl pull docker.io/library/hello-world
> sudo crictl runp --config /dev/null --hook-config /dev/null --runtime /usr/local/bin/containerd-shim-runc-v2 hello-world
Note: If you prefer Docker, you can initialize a VM specifically for it. Many users combine Lima with Colima (brew install colima), which uses Lima to provide a full Docker environment.
Lima allows you to define a YAML configuration file to customize the VM's resources, operating system, and shared directories.
Let's create a file named my-lima.yaml to run a Fedora VM with more RAM
# my-lima.yaml
arch: "x86_64"
memory: "4GiB" # Allocate 4GB of RAM
cpus: 4 # Use 4 CPU cores
images:
- location: "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.5.2111-20220316.0.x86_64.qcow2"
arch: "x86_64"
# Note: Use a more up-to-date image/distro if available, Fedora or recent Ubuntu are common.
mounts:
- location: "~" # Share the entire macOS home directory
writable: true
Then, start the VM with this configuration
limactl start my-lima.yaml --name=fedora-dev
You can then shell into it with
limactl shell fedora-dev
This flexibility is great for engineers who need to test on specific Linux distributions or require more resources for build processes.
I hope this gives you a clear understanding of how Lima can streamline your containerized development on macOS!