SmolVM provides a simple, intuitive interface for managing microVM lifecycles. You can create VMs using auto-configuration or custom settings, control their lifecycle states, and use context managers for automatic cleanup.
The simplest way to get started is with auto-configuration. SmolVM automatically provisions an SSH-ready Alpine Linux VM:
from smolvm import SmolVM# Auto-configured VM with sensible defaultswith SmolVM() as vm: print(f"VM ID: {vm.vm_id}") print(f"IP Address: {vm.get_ip()}") result = vm.run("uname -a") print(result.stdout)
The recommended pattern is using SmolVM as a context manager for automatic lifecycle management:
with SmolVM(config) as vm: # VM automatically starts on context entry result = vm.run("hostname") print(result.output) # VM automatically stops and deletes on context exit
from smolvm import SmolVMwith SmolVM() as vm: print(f"VM {vm.vm_id} is ready") vm.run("echo 'Hello World'")# Automatic cleanup
from smolvm import SmolVMfrom smolvm.exceptions import SmolVMError, OperationTimeoutErrortry: with SmolVM(mem_size_mib=512) as vm: result = vm.run("some-command", timeout=10) if not result.ok: print(f"Command failed: {result.stderr}")except OperationTimeoutError as e: print(f"Operation timed out: {e.message}")except SmolVMError as e: print(f"VM error: {e}")
with SmolVM() as vm: # Unique identifier print(vm.vm_id) # Current status (VMState enum) print(vm.status) # VMState.RUNNING # Full VM information print(vm.info) # VMInfo object # Guest IP address print(vm.get_ip()) # Data directory print(vm.data_dir) # Check if commands can be executed print(vm.can_run_commands()) # True for SSH-capable VMs