---
title: Charmhub | Deploy Operator Libs Linux using Charmhub - The Open Operator Collection
description: Deploy the latest version of Operator Libs Linux on any cloud.
url: https://charmhub.io/operator-libs-linux/libraries/snap
---

# Operator Libs Linux

[Jon Seager](https://charmhub.io/publisher/jnsgruk "View all packages from Jon Seager")

* [Jon Seager](https://charmhub.io/publisher/jnsgruk "View all packages from Jon Seager")

Platform:

22.04

20.04

stable 2

```
juju deploy operator-libs-linux
```

[Learn to deploy on juju >](https://juju.is/docs/juju/manage-applications)

[Toggle side navigation](https://charmhub.io/operator-libs-linux/libraries/snap#drawer)

## charms.operator\_libs\_linux.v2.snap

* [*Docstrings*Docstrings](https://charmhub.io/operator-libs-linux/libraries/snap)
  [*Code*Source code](https://charmhub.io/operator-libs-linux/libraries/snap/source-code)
* + Download

    Fetch library

    ```
    charmcraft fetch-lib charms.operator_libs_linux.v2.snap
    ```

    [Download snap.py](https://charmhub.io/operator-libs-linux/libraries/snap/download)
  + *Last updated* 08 Oct 2025
  + *Revision* Library version 2.15

Legacy Charmhub-hosted snap library, deprecated in favour of `charmlibs.snap`.

WARNING: This library is deprecated and will no longer receive feature updates or bugfixes.
`charmlibs.snap` version 1.0 is a bug-for-bug compatible migration of this library.
Add 'charmlibs-snap~=1.0' to your charm's dependencies, and remove this Charmhub-hosted library.
Then replace `from charms.operator_libs_linux.v2 import snap` with `from charmlibs import snap`.
Read more:

* https://documentation.ubuntu.com/charmlibs
* https://pypi.org/project/charmlibs-snap

---

Representations of the system's Snaps, and abstractions around managing them.

The `snap` module provides convenience methods for listing, installing, refreshing, and removing
Snap packages, in addition to setting and getting configuration options for them.

In the `snap` module, `SnapCache` creates a dict-like mapping of `Snap` objects at when
instantiated. Installed snaps are fully populated, and available snaps are lazily-loaded upon
request. This module relies on an installed and running `snapd` daemon to perform operations over
the `snapd` HTTP API.

`SnapCache` objects can be used to install or modify Snap packages by name in a manner similar to
using the `snap` command from the commandline.

An example of adding Juju to the system with `SnapCache` and setting a config value:

```
try:
    cache = snap.SnapCache()
    juju = cache["juju"]

    if not juju.present:
        juju.ensure(snap.SnapState.Latest, channel="beta")
        juju.set({"some.key": "value", "some.key2": "value2"})
except snap.SnapError as e:
    logger.error("An exception occurred when installing charmcraft. Reason: %s", e.message)
```

In addition, the `snap` module provides "bare" methods which can act on Snap packages as
simple function calls. :meth:`add`, :meth:`remove`, and :meth:`ensure` are provided, as
well as :meth:`add_local` for installing directly from a local `.snap` file. These return
`Snap` objects.

As an example of installing several Snaps and checking details:

```
try:
    nextcloud, charmcraft = snap.add(["nextcloud", "charmcraft"])
    if nextcloud.get("mode") != "production":
        nextcloud.set({"mode": "production"})
except snap.SnapError as e:
    logger.error("An exception occurred when installing snaps. Reason: %s" % e.message)
```

Dependencies:
Note that this module requires `opentelemetry-api`, which is already included into
your charm's virtual environment via `ops >= 2.21`.

---

Index

* [class SnapServiceDict](https://charmhub.io/operator-libs-linux/libraries/snap#snapservicedict)
* [class SnapService](https://charmhub.io/operator-libs-linux/libraries/snap#snapservice)
* + [def \_\_init\_\_(
    self,
    daemon,
    daemon\_scope,
    enabled,
    active,
    activators)](https://charmhub.io/operator-libs-linux/libraries/snap#snapservice-__init__)
* + [def as\_dict(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snapservice-as_dict)
* [class MetaCache](https://charmhub.io/operator-libs-linux/libraries/snap#metacache)
* + [def cache(
    cls)](https://charmhub.io/operator-libs-linux/libraries/snap#metacache-cache)
* + [def cache(
    cls,
    cache)](https://charmhub.io/operator-libs-linux/libraries/snap#metacache-cache)
* + [def \_\_getitem\_\_(
    cls,
    name)](https://charmhub.io/operator-libs-linux/libraries/snap#metacache-__getitem__)
* [class Error](https://charmhub.io/operator-libs-linux/libraries/snap#error)
* + [def \_\_init\_\_(
    self,
    message)](https://charmhub.io/operator-libs-linux/libraries/snap#error-__init__)
* + [def \_\_repr\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#error-__repr__)
* + [def name(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#error-name)
* [class SnapAPIError](https://charmhub.io/operator-libs-linux/libraries/snap#snapapierror)
* + [def \_\_init\_\_(
    self,
    body,
    code,
    status,
    message)](https://charmhub.io/operator-libs-linux/libraries/snap#snapapierror-__init__)
* + [def \_\_repr\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snapapierror-__repr__)
* [class SnapState](https://charmhub.io/operator-libs-linux/libraries/snap#snapstate)
* [class SnapError](https://charmhub.io/operator-libs-linux/libraries/snap#snaperror)
* [class SnapNotFoundError](https://charmhub.io/operator-libs-linux/libraries/snap#snapnotfounderror)
* [class Snap](https://charmhub.io/operator-libs-linux/libraries/snap#snap)
* + [def \_\_init\_\_(
    self,
    name,
    state,
    channel,
    revision,
    confinement,
    apps,
    cohort)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-__init__)
* + [def \_\_eq\_\_(
    self,
    other)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-__eq__)
* + [def \_\_hash\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-__hash__)
* + [def \_\_repr\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-__repr__)
* + [def \_\_str\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-__str__)
* + [def get(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-get)
* + [def get(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-get)
* + [def get(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-get)
* + [def get(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-get)
* + [def get(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-get)
* + [def set(
    self,
    config)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-set)
* + [def unset(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-unset)
* + [def start(
    self,
    services,
    enable)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-start)
* + [def stop(
    self,
    services,
    disable)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-stop)
* + [def logs(
    self,
    services,
    num\_lines)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-logs)
* + [def connect(
    self,
    plug,
    service,
    slot)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-connect)
* + [def hold(
    self,
    duration)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-hold)
* + [def unhold(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-unhold)
* + [def alias(
    self,
    application,
    alias)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-alias)
* + [def restart(
    self,
    services,
    reload)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-restart)
* + [def name(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-name)
* + [def ensure(
    self,
    state,
    classic,
    devmode,
    channel,
    cohort,
    revision)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-ensure)
* + [def present(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-present)
* + [def latest(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-latest)
* + [def state(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-state)
* + [def state(
    self,
    state)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-state)
* + [def revision(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-revision)
* + [def channel(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-channel)
* + [def confinement(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-confinement)
* + [def apps(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-apps)
* + [def services(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-services)
* + [def held(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-held)
* + [def version(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snap-version)
* [class SnapClient](https://charmhub.io/operator-libs-linux/libraries/snap#snapclient)
* + [def \_\_init\_\_(
    self,
    socket\_path,
    opener,
    base\_url,
    timeout)](https://charmhub.io/operator-libs-linux/libraries/snap#snapclient-__init__)
* + [def get\_installed\_snaps(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snapclient-get_installed_snaps)
* + [def get\_snap\_information(
    self,
    name)](https://charmhub.io/operator-libs-linux/libraries/snap#snapclient-get_snap_information)
* + [def get\_installed\_snap\_apps(
    self,
    name)](https://charmhub.io/operator-libs-linux/libraries/snap#snapclient-get_installed_snap_apps)
* [class SnapCache](https://charmhub.io/operator-libs-linux/libraries/snap#snapcache)
* + [def \_\_init\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snapcache-__init__)
* + [def \_\_contains\_\_(
    self,
    key)](https://charmhub.io/operator-libs-linux/libraries/snap#snapcache-__contains__)
* + [def \_\_len\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snapcache-__len__)
* + [def \_\_iter\_\_(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snapcache-__iter__)
* + [def \_\_getitem\_\_(
    self,
    snap\_name)](https://charmhub.io/operator-libs-linux/libraries/snap#snapcache-__getitem__)
* + [def snapd\_installed(
    self)](https://charmhub.io/operator-libs-linux/libraries/snap#snapcache-snapd_installed)
* [def add(
  snap\_names,
  state,
  channel,
  classic,
  devmode,
  cohort,
  revision
  )](https://charmhub.io/operator-libs-linux/libraries/snap#add)
* [def add(
  snap\_names,
  state,
  channel,
  classic,
  devmode,
  cohort,
  revision
  )](https://charmhub.io/operator-libs-linux/libraries/snap#add)
* [def add(
  snap\_names,
  state,
  channel,
  classic,
  devmode,
  cohort,
  revision
  )](https://charmhub.io/operator-libs-linux/libraries/snap#add)
* [def remove(
  snap\_names
  )](https://charmhub.io/operator-libs-linux/libraries/snap#remove)
* [def remove(
  snap\_names
  )](https://charmhub.io/operator-libs-linux/libraries/snap#remove)
* [def remove(
  snap\_names
  )](https://charmhub.io/operator-libs-linux/libraries/snap#remove)
* [def ensure(
  snap\_names,
  state,
  channel,
  classic,
  devmode,
  cohort,
  revision
  )](https://charmhub.io/operator-libs-linux/libraries/snap#ensure)
* [def ensure(
  snap\_names,
  state,
  channel,
  classic,
  devmode,
  cohort,
  revision
  )](https://charmhub.io/operator-libs-linux/libraries/snap#ensure)
* [def ensure(
  snap\_names,
  state,
  channel,
  classic,
  devmode,
  cohort,
  revision
  )](https://charmhub.io/operator-libs-linux/libraries/snap#ensure)
* [def install\_local(
  filename,
  classic,
  devmode,
  dangerous
  )](https://charmhub.io/operator-libs-linux/libraries/snap#install_local)
* [def hold\_refresh(
  days,
  forever
  )](https://charmhub.io/operator-libs-linux/libraries/snap#hold_refresh)

#### class SnapServiceDict

Description

Dictionary representation returned by SnapService.as\_dict. None

#### class SnapService

Description

Data wrapper for snap services. None

Methods

SnapService.
\_\_init\_\_(

*self*

,
daemon

,
daemon\_scope

,
enabled: bool

,
active: bool

,
activators
)

SnapService.
as\_dict(

*self*
)

Description

Return instance representation as dict. None

#### class MetaCache

Description

MetaCache class used for initialising the snap cache. None

Methods

MetaCache.
cache(

cls
)

Description

Property for returning the snap cache. None

MetaCache.
cache(

cls

,
cache: SnapCache
)

Description

Setter for the snap cache. None

MetaCache.
\_\_getitem\_\_(

cls

,
name: str
)

Description

Snap cache getter. None

#### class Error

Description

Base class of most errors raised by this library. None

Methods

Error.
\_\_init\_\_(

*self*

,
message: str
)

Error.
\_\_repr\_\_(

*self*
)

Description

Represent the Error class. None

Error.
name(

*self*
)

Description

Return a string representation of the model plus class. None

#### class SnapAPIError

Description

Raised when an HTTP API error occurs talking to the Snapd server. None

Methods

SnapAPIError.
\_\_init\_\_(

*self*

,
body

,
code: int

,
status: str

,
message: str
)

SnapAPIError.
\_\_repr\_\_(

*self*
)

Description

Represent the SnapAPIError class. None

#### class SnapState

Description

The state of a snap on the system or in the cache. None

#### class SnapError

Description

Raised when there's an error running snap control commands. None

Methods

#### class SnapNotFoundError

Description

Raised when a requested snap is not known to the system. None

#### class Snap

Represents a snap package and its properties.

Description

`Snap` exposes the following properties about a snap:

* name: the name of the snap
* state: a `SnapState` representation of its install status
* channel: "stable", "candidate", "beta", and "edge" are common
* revision: a string representing the snap's revision
* confinement: "classic", "strict", or "devmode"
* version: a string representing the snap's version, if set by the snap author

Methods

Snap.
\_\_init\_\_(

*self*

,
name: str

,
state: SnapState

,
channel: str

,
revision: str

,
confinement: str

,
apps

,
cohort
)

Snap.
\_\_eq\_\_(

*self*

,
other: object
)

Description

Equality for comparison. None

Snap.
\_\_hash\_\_(

*self*
)

Description

Calculate a hash for this snap. None

Snap.
\_\_repr\_\_(

*self*
)

Description

Represent the object such that it can be reconstructed. None

Snap.
\_\_str\_\_(

*self*
)

Description

Represent the snap object as a string. None

Snap.
get(

*self*

,
key
)

Snap.
get(

*self*

,
key: str
)

Snap.
get(

*self*

,
key
)

Snap.
get(

*self*

,
key: str
)

Snap.
get(

*self*

,
key
)

Fetch snap configuration values.

Arguments

key

the key to retrieve. Default to retrieve all values for typed=True.

typed

set to True to retrieve typed values (set with typed=True).
Default is to return a string.

Snap.
set(

*self*

,
config
)

Set a snap configuration value.

Arguments

config

a dictionary containing keys and values specifying the config to set.

typed

set to True to convert all values in the config into typed values while
configuring the snap (set with typed=True). Default is not to convert.

Snap.
unset(

*self*

,
key: str
)

Unset a snap configuration value.

Arguments

key

the key to unset

Snap.
start(

*self*

,
services

,
enable: bool
)

Start a snap's services.

Arguments

services
(list)

(optional) list of individual snap services to start (otherwise all)

enable
(bool)

(optional) flag to enable snap services on start. Default `false`

Snap.
stop(

*self*

,
services

,
disable: bool
)

Stop a snap's services.

Arguments

services
(list)

(optional) list of individual snap services to stop (otherwise all)

disable
(bool)

(optional) flag to disable snap services on stop. Default `False`

Snap.
logs(

*self*

,
services

,
num\_lines: int
)

Fetch a snap services' logs.

Arguments

services
(list)

(optional) list of individual snap services to show logs from
(otherwise all)

num\_lines
(int)

(optional) integer number of log lines to return. Default `10`

Snap.
connect(

*self*

,
plug: str

,
service

,
slot
)

Connect a plug to a slot.

Description

Args:
plug (str): the plug to connect
service (str): (optional) the snap service name to plug into
slot (str): (optional) the snap service slot to plug in to

Raises:
SnapError if there is a problem encountered

Snap.
hold(

*self*

,
duration
)

Add a refresh hold to a snap.

Arguments

duration

duration for the hold, or None (the default) to hold this snap indefinitely.

Snap.
unhold(

*self*
)

Description

Remove the refresh hold of a snap. None

Snap.
alias(

*self*

,
application: str

,
alias
)

Create an alias for a given application.

Arguments

application

application to get an alias.

alias

(optional) name of the alias; if not provided, the application name is used.

Snap.
restart(

*self*

,
services

,
reload: bool
)

Restarts a snap's services.

Arguments

services
(list)

(optional) list of individual snap services to restart.
(otherwise all)

reload
(bool)

(optional) flag to use the service reload command, if available.
Default `False`

Snap.
name(

*self*
)

Description

Returns the name of the snap. None

Snap.
ensure(

*self*

,
state: SnapState

,
classic: bool

,
devmode: bool

,
channel

,
cohort

,
revision
)

Ensure that a snap is in a given state.

Description

Args:
state: a `SnapState` to reconcile to.
classic: an (Optional) boolean indicating whether classic confinement should be used
devmode: an (Optional) boolean indicating whether devmode confinement should be used
channel: the channel to install from
cohort: optional. Specify the key of a snap cohort.
revision: optional. the revision of the snap to install/refresh

While both channel and revision could be specified, the underlying snap install/refresh
command will determine which one takes precedence (revision at this time)

Raises:
SnapError if an error is encountered

Snap.
present(

*self*
)

Description

Report whether or not a snap is present. None

Snap.
latest(

*self*
)

Description

Report whether the snap is the most recent version. None

Snap.
state(

*self*
)

Description

Report the current snap state. None

Snap.
state(

*self*

,
state: SnapState
)

Set the snap state to a given value.

Description

Args:
state: a `SnapState` to reconcile the snap to.

Raises:
SnapError if an error is encountered

Snap.
revision(

*self*
)

Description

Returns the revision for a snap. None

Snap.
channel(

*self*
)

Description

Returns the channel for a snap. None

Snap.
confinement(

*self*
)

Description

Returns the confinement for a snap. None

Snap.
apps(

*self*
)

Description

Returns (if any) the installed apps of the snap. None

Snap.
services(

*self*
)

Description

Returns (if any) the installed services of the snap. None

Snap.
held(

*self*
)

Description

Report whether the snap has a hold. None

Snap.
version(

*self*
)

Description

Returns the version for a snap. None

#### class SnapClient

Snapd API client to talk to HTTP over UNIX sockets.

Description

In order to avoid shelling out and/or involving sudo in calling the snapd API,
use a wrapper based on the Pebble Client, trimmed down to only the utility methods
needed for talking to snapd.

Methods

SnapClient.
\_\_init\_\_(

*self*

,
socket\_path: str

,
opener

,
base\_url: str

,
timeout: float
)

Initialize a client instance.

Arguments

socket\_path

a path to the socket on the filesystem. Defaults to /run/snap/snapd.socket

opener

specifies an opener for unix socket, if unspecified a default is used

base\_url

base URL for making requests to the snap client. Must be an HTTP(S) URL.
Defaults to http://localhost/v2/

timeout

timeout in seconds to use when making requests to the API. Default is 30.0s.

SnapClient.
get\_installed\_snaps(

*self*
)

Description

Get information about currently installed snaps. None

SnapClient.
get\_snap\_information(

*self*

,
name: str
)

Description

Query the snap server for information about single snap. None

SnapClient.
get\_installed\_snap\_apps(

*self*

,
name: str
)

Description

Query the snap server for apps belonging to a named, currently installed snap. None

#### class SnapCache

An abstraction to represent installed/available packages.

Description

When instantiated, `SnapCache` iterates through the list of installed
snaps using the `snapd` HTTP API, and a list of available snaps by reading
the filesystem to populate the cache. Information about available snaps is lazily-loaded
from the `snapd` API when requested.

Methods

SnapCache.
\_\_init\_\_(

*self*
)

SnapCache.
\_\_contains\_\_(

*self*

,
key: object
)

Description

Check if a given snap is in the cache. None

SnapCache.
\_\_len\_\_(

*self*
)

Description

Report number of items in the snap cache. None

SnapCache.
\_\_iter\_\_(

*self*
)

Description

Provide iterator for the snap cache. None

SnapCache.
\_\_getitem\_\_(

*self*

,
snap\_name: str
)

Description

Return either the installed version or latest version for a given snap. None

SnapCache.
snapd\_installed(

*self*
)

Description

Check whether snapd has been installed on the system. None

#### def add(     snap\_names: str,     state,     channel,     classic: bool,     devmode: bool,     cohort,     revision )

#### def add(     snap\_names,     state,     channel,     classic: bool,     devmode: bool,     cohort,     revision )

#### def add(     snap\_names,     state,     channel,     classic: bool,     devmode: bool,     cohort,     revision )

Add a snap to the system.

Description

Args:
snap\_names: the name or names of the snaps to install
state: a string or `SnapState` representation of the desired state, one of
[`Present` or `Latest`]
channel: an (Optional) channel as a string. Defaults to 'latest'
classic: an (Optional) boolean specifying whether it should be added with classic
confinement. Default `False`
devmode: an (Optional) boolean specifying whether it should be added with devmode
confinement. Default `False`
cohort: an (Optional) string specifying the snap cohort to use
revision: an (Optional) string specifying the snap revision to use

Raises:
SnapError if some snaps failed to install or were not found.

#### def remove(snap\_names: str)

#### def remove(snap\_names)

#### def remove(snap\_names)

Remove specified snap(s) from the system.

Description

Args:
snap\_names: the name or names of the snaps to install

Raises:
SnapError if some snaps failed to install.

#### def ensure(     snap\_names: str,     state: str,     channel,     classic: bool,     devmode: bool,     cohort,     revision )

#### def ensure(     snap\_names,     state: str,     channel,     classic: bool,     devmode: bool,     cohort,     revision )

#### def ensure(     snap\_names,     state: str,     channel,     classic: bool,     devmode: bool,     cohort,     revision )

Ensure specified snaps are in a given state on the system.

Description

Args:
snap\_names: the name(s) of the snaps to operate on
state: a string representation of the desired state, from `SnapState`
channel: an (Optional) channel as a string. Defaults to 'latest'
classic: an (Optional) boolean specifying whether it should be added with classic
confinement. Default `False`
devmode: an (Optional) boolean specifying whether it should be added with devmode
confinement. Default `False`
cohort: an (Optional) string specifying the snap cohort to use
revision: an (Optional) integer specifying the snap revision to use

When both channel and revision are specified, the underlying snap install/refresh
command will determine the precedence (revision at the time of adding this)

Raises:
SnapError if the snap is not in the cache.

#### def install\_local(     filename: str,     classic: bool,     devmode: bool,     dangerous: bool )

Perform a snap operation.

Description

Args:
filename: the path to a local .snap file to install
classic: whether to use classic confinement
devmode: whether to use devmode confinement
dangerous: whether --dangerous should be passed to install snaps without a signature

Raises:
SnapError if there is a problem encountered

#### def hold\_refresh(     days: int,     forever: bool )

Set the system-wide snap refresh hold.

Arguments

days

number of days to hold system refreshes for. Maximum 90. Set to zero to remove hold.

forever

if True, will set a hold forever.
