How to package and upload a snap with components

Components are parts of a snap that can be built and uploaded in conjunction with a snap and later optionally installed beside it. Components are defined with a top-level components keyword in a snapcraft.yaml.

Start with a simple snapcraft.yaml:

name: hello-components
version: "1.0"
summary: A snap with a component
description: A simple snap with a component containing translations
base: core24
confinement: strict

parts:
  application:
    source: src
    plugin: dump

And create the following project tree:

.
└── src
    └── my-app

my-app can be an empty file. The contents are not important for this how-to guide.

To create a component, define a component called translations under a new top-level components keyword. We will also add a new part that dumps the contents of the translations directory:

name: hello-components
version: "1.0"
summary: A snap with a component
description: A simple snap with a component containing translations
base: core24
confinement: strict

components:
  translations:
    type: test
    summary: Translations modules
    description: Translations modules
    version: "1.0"

parts:
  application:
    source: src
    plugin: dump

  translations:
    source: translations
    plugin: dump

Next, create a translations directory with a file called la:

.
├── src
│   └── my-app
└── translations
    └── la

la can also be an empty file.

Pack the snap with:

snapcraft pack

This will produce 2 artefacts, the snap and the component:

  • hello-components_1.0_amd64.snap

  • hello-components+translations_1.0.comp

The my-app and la files are staged, primed, and packed in the snap artefact. The component artefact has no payload. It is empty except for a metadata file meta/component.yaml.

To move the la translation file to the component artefact, use the organize keyword for the translations part:

name: hello-components
version: "1.0"
summary: A snap with a component
description: A simple snap with a component containing translations
base: core24
confinement: strict

components:
  translations:
    type: test
    summary: Translations modules
    description: Translations modules
    version: "1.0"

parts:
  application:
    source: src
    plugin: dump

  translations:
    source: translations
    plugin: dump
    organize:
      la: (component/translations)

The parentheses around (component/translations) indicate that the files should be organized into the translations component’s install directory.

Pack the snap again with:

snapcraft pack

This will produce two artefacts again but the component now contains the la translation file.

To upload the snap and the component to the store, specify the snap file and the component file for the translations component.

snapcraft upload hello-components_1.0_amd64.snap \
  --component translations=hello-components+translations_1.0.comp

The store expects a snap and its components to be uploaded together.

The component has to be uploaded alongside the snap.