<!-- source: https://docs.codemagic.io/yaml-quick-start/building-a-snap-package/ -->
<!-- last modified: 2025-05-27 -->

# Linux Snap packages

> How to build and release a snap package with codemagic.yaml

This guide will illustrate the basic steps necessary for building and publishing your app as a Snap package.

You can find a complete project showcasing these steps in our [Sample projects repository](https://github.com/codemagic-ci-cd/flutter-snapcraft-example/).



> 
> **Note**: Snap is only available on Linux instances. Make sure to have `instance_type: linux` or `instance_type: linux_x2` in your `codemagic.yaml`. See the build machine specification [here](../specs/versions-linux/).
> 



## Adding the app to Codemagic
The apps you have available on Codemagic are listed on the Applications page. Click **Add application** to add a new app.

1. If you have more than one team configured in Codemagic, select the team you wish to add the app to.
2. Connect the repository where the source code is hosted. Detailed instructions that cover some advanced options are available [here](../../getting-started/adding-apps).
3. Select the repository from the list of available repositories. Select the appropriate project type.
4. Click **Finish: Add application**

## Creating codemagic.yaml


In order to use `codemagic.yaml` for build configuration on Codemagic, it has to be committed to your repository. The name of the file must be `codemagic.yaml` and it must be located in the root directory of the repository. Detailed explanation can be found [here](../yaml/yaml-getting-started).



> 
> **Tip**
> You can find codemagic.yaml examples in [Codemagic Sample Projects](https://github.com/codemagic-ci-cd/codemagic-sample-projects/) repository.
> 



If you prefer to write your `codemagic.yaml` file from scratch, you can start with this minimal configuration.


```yaml

workflows:
    sample-workflow:
        name: Codemagic Sample Workflow
        max_build_duration: 120
        instance_type: mac_mini_m2

```





> 
> **Tip**
> You can have more than one workflow in the same `codemagic.yaml` file. If you are building for both the Android and iOS, simply enter both workflows as:
> 



```yaml

workflows:
    android-workflow-id:
        name: Android Sample Workflow
        # .......    
        # .......
        # .......  
    ios-workflow-id:
        name: iOS Sample Workflow
        # ......

```



Scan for the `codemagic.yaml` file by selecting a branch to scan and clicking the **Check for configuration** file button at the top of the page. Note that you can have different configuration files in different branches.


## Configure Snap

To set up Snap packaging, create a `snapcraft.yaml` file with the necessary configurations according to [Snapcraft guide for Flutter](https://snapcraft.io/docs/flutter-applications) or follow the general [`snapcraft.yaml` guide](https://snapcraft.io/docs/creating-snapcraft-yaml).

Optionally, run the `snapcraft snap` command locally to ensure that everything is set up.

You should store the `snapcraft.yaml` file in the repository root. Another option is to store `snapcraft.yaml` in the `.snap` folder that is located in repository root.


## Building snap packages

Include the `snapcraft snap` command in the `scripts` section of your `codemagic.yaml` file as in the example below. The output of this command is a `.snap` artifact that can later be used for publishing to the Snapcraft Snap Store.


```yaml

workflows:
  snap-build:
    name: Snapcraft Build
    instance_type: linux
    environment:
      vars:
        SNAPCRAFT_BUILD_ENVIRONMENT: host
    scripts:
      - name: Create a snap
        script: | 
          snapcraft snap --output flutter-codemagic-example.snap
    artifacts:
        - '**/*.snap'

```



Additionally, you may want to install the generated `.snap` package onto your machine for testing. The package will not be code signed unless you publish it to Snap Store, so you would need to use the `--dangerous` flag to install the package without code signing:


```bash

    snap install your-package.snap --dangerous

```




> 
> **Note**: In case you are packaging a **Flutter application**, be sure to set `SNAPCRAFT_BUILD_ENVIRONMENT` environment variable to `host`. It is required to avoid virtualization. Read more about virtualization options [here](https://flutter.dev/docs/deployment/linux). Additionally, Snapcraft manages all the dependencies according to `snapcraft.yaml` configuration. There is no need to include the Flutter version in `codemagic.yaml`.
> 




## Publishing to Snap Store

Snap packages can be published to the [Snap Store](https://snapcraft.io/).

1. Generate your Snapcraft credentials file by running the following command locally and providing your Snapcraft account username and password:

```bash

snapcraft export-login snapcraft-login-credentials

```


2. Run the following command and carefully copy/paste the output:

```Shell

cat  snapcraft-login-credentials | base64

```


3. Open your Codemagic app settings, and go to the **Environment variables** tab.
4. Enter the desired **_Variable name_**, e.g. `SNAPCRAFT_LOGIN_CREDENTIALS`.
5. Paste the `base64` encoded credentials from Step 2. as **_Variable value_**.
6. Enter the variable group name, e.g. **_snapcraft_credentials_**. Click the button to create the group.
7. Make sure the **Secret** option is selected.
8. Click the **Add** button to add the variable.

9. Add the variable group to your `codemagic.yaml` file

```yaml

  environment:
    groups:
      - snapcraft_credentials

```



10. In the `scripts` section, add steps to base64 decode the credentials file, log in to Snapcraft via CLI, build the snap package and release it to the desired channel.


```yaml

  scripts:
    - name: Authenticate with Snap Store
      script: | 
        echo $SNAPCRAFT_LOGIN_CREDENTIALS | base64 \
          --decode > /home/builder/snapcraft-login-credentials
        snapcraft login --with /home/builder/snapcraft-login-credentials
    - name: Create a Snap package
      script: | 
        snapcraft snap --output flutter-codemagic-example.snap
    - name: Publish to Snap Store
      script: | 
        snapcraft upload flutter-codemagic-example.snap --release stable

```


