Support parallel instances¶
Parallel instances lets users install a snap more than once, with separate data, configuration, services, and interface connections. This feature is enabled by default from snapd version 2.79.
If Snapd considers that your snap supports parallel instances, you should ensure that the applications, services, and hooks from multiple snap instances can run independently at the same time, without conflicts. Successful installation alone does not prove that a snap is ready for parallel instances.
As a snap developer, you should first check support for parallel instances of your snap. If your snap is not supported for parallel instances, then you don’t need to take any further action. Otherwise, ensure that you use the snapd-provided environment variables and make shared resources configurable in your snap. Finally, you can test your snap for correct behavior with parallel instances.
For more details see Parallel installs.
Check snapd support for parallel instances¶
To determine whether your snap is compatible with parallel instances, inspect snap.yaml. If your snap declares any unsupported plug, slot, or app property, snapd refuses to install multiple instances of it.
Compare your snap’s declared plugs and slots against the table in Unsupported interfaces. Plugs and slots of the same interface can have different support coverage. For example, dbus plugs are supported, while dbus slots are not.
If your snap is not compatible with parallel instances, you don’t need to worry about the rest of this guide.
Note
Be aware that advanced users have a mechanism to bypass the support checks; see Installing an unsupported snap anyway.
Use the snapd-provided environment variables¶
To make sure that multiple instances of your snap can run in parallel, all apps, hooks, and scripts must refer to the right instance. That requires using the correct environment variables. Snapd provides variables to identify an instance of the snap, as well as path variables pointing to the files it needs to access.
Identify the running instance¶
When installing a parallel instance, users choose an instance key (^[a-z0-9]{1,10}$), giving the instance a name of the form <snap>_<key>. Use SNAP_INSTANCE_NAME to refer to the name of that snap instance.
For the full list of runtime variables and example values, see Snap environment, data and namespace and Environment variables. The following table shows the most relevant variables to keep in mind.
Variable |
Use case |
|---|---|
|
Use when you need the store name of your snap |
|
Use whenever referring to the instance of the snap |
|
Use to determine if the snap is running as an instance; it will be empty if the snap is not a parallel installed instance |
Use instance-specific paths¶
To ensure that the snap instance points to the correct files, use snapd-provided paths, instead of constructing them from the hard-coded snap name. Choose the environment variables that best suit your desired functionality.
For example, a user application’s database of one specific instance can be set up this way:
mkdir -p "$SNAP_USER_COMMON"
exec "$SNAP/bin/<app-name>" --database "$SNAP_USER_COMMON/state.db"
SNAP_USER_COMMON resolves to /home/<usr>/snap/<snap>_<key>/common, making it a parallel-instance safe choice for maintaining instance-separated, user-specific, and revision-agnostic data.
See Snap environment, data and namespace for a complete list of environment variables.
For classic snaps, use the snap data variables for instance-specific state and check how you access the user’s home directory and other host paths. See Classic confinement and Parallel installs for further details.
Test parallel instances of your snap¶
Test your snap’s behavior when installed as a parallel instance to avoid unexpected issues. The tests below are some of the ways in which you can test whether parallel installed instances work correctly.
Installation
Install three parallel instances <snap>, <snap>_<key1>, <snap>_<key2>: one instance without a key and two with different keys. All three should successfully install without errors.
Adapt the commands below to install the instances from a local file:
sudo snap install --dangerous ./<snap-file>.snap
sudo snap install --dangerous --name=<snap>_<key1> ./<snap-file>.snap
sudo snap install --dangerous --name=<snap>_<key2> ./<snap-file>.snap
Concurrent operation
Run all instances and test the application’s main workflows. Verify that ports, locks, and sockets do not conflict.
Data isolation
Write different settings and data through each instance. Confirm that neither reads or changes the other’s state, including caches and external resources.
Configuration and services
Change a setting, restart a service and stop an instance. Confirm that the other instance remains unchanged and operational. Test missing configuration and resource conflicts.
Interfaces
Inspect snap connections for each instance. Confirm correct hook execution and test manual connections through snap connect. If your snap acts as a provider on some interface, check that consumers are correctly receiving data.
Refresh and revert
Refresh one instance to a newer build, then revert. Confirm that hooks, data migrations and connections do not attempt to alter the other instance.
Removal
Remove either instance and confirm that the other’s data, services, launchers and shared-resource registrations still work.