---
title: Run scripts during snapcraft builds with “scriptlets”
description: 'Please note: this blog post is over 5 years old and is out of date.
  Scriptlets have been superseded by Overrides. If you have snapped an application,
  or tried to snap an application, you know that Snapcraft heavily depends on built-in
  plugins for specific build systems and that it provides a large array of choices  […]'
url: https://canonical.com/blog/run-scripts-during-snapcraft-builds-with-scriptlets?format=md
---

1. [Blog](https://canonical.com/blog)
2. Article

---

[David Callé](https://canonical.com/blog/author/davidc3-2 "More about David Callé")

2 February 2017

# Run scripts during snapcraft builds with “scriptlets”

[Snap](https://canonical.com/blog/tag/snap)
[snapcraft](https://canonical.com/blog/tag/snapcraft)
[snapcraft.io](https://canonical.com/blog/tag/snapcraft-io)

---

Share the article

**Please note: this blog post is over 5 years old and is out of date. S*criptlets* have been superseded by [Overrides](https://snapcraft.io/docs/overrides).**

If you have snapped an application, or tried to snap an application, you know that Snapcraft heavily depends on built-in plugins for specific build systems and that it provides a large array of choices to do so. You can snap ROS apps, Python 2 and 3 apps, Go apps, Rust apps, Java apps, Linux kernels and many more, by simply declaring the right plugin name.

What if your app depends on multiple build systems? Or uses a custom build script, or maybe you want to run `make test` as part of a snapcraft build? Until recently, you had to write a custom plugin and ship it along your snapcraft.yaml, which was working fine but took time to achieve and made you ship an extra directory just for it. With Snapcraft “scriptlets”, this is over and the path to snapping is now much faster.

## Scriptlets?

Scriptlets are shell scripts, sourced directly from your snapcraft.yaml, that change the behaviour of a plugin. They can be triggered before the “build” step to run commands on your source code before building it, instead of it, to completely override the building behaviour and after: to move build artefacts around, change a config file or anything that you would need to happen after the build.

## How to use them

Scriptlets are declared using specific keywords in your snapcraft part:

* `prepare:` for commands that need to happen before the build step of a plugin
* `build:` to override a plugin’s build step
* `install:` for commands that need to happen after the build step

Have a look at [the documentation](https://snapcraft.io/docs/overrides) for more details, or see the following examples to get started: a demo of the prepare scriptlet and a real world use case with the MySQL snap.

### Real world use case: MySQL

Let’s see how the [MySQL snap](http://mysqlrelease.com/2017/01/give-me-a-mysql-and-make-it-snappy/) uses the `prepare:` keyword to pull deb packages from their server, unpack and load them into the project directory during a snapcraft build.

Source: <https://github.com/mysql/mysql-snap/blob/5.7/snapcraft.yaml>

If we look at the `parts:` section of the snapcraft.yaml we can see a `mysql-server` part, that dumps a pre-built MySQL into the snap.

```
parts:
  mysql-server:
    prepare: ./stage_binaries.sh
    build-packages: [libaio-dev, libmecab-dev, libnuma-dev, libncurses5-dev, zlib1g-dev]
    plugin: dump
    source: ./
    organize:
      staging-files/usr: usr/
    snap:
      - usr/lib/mysql/plugin/mysql_no_login.so
      - usr/lib/mysql/plugin/innodb_engine.so
      - usr/lib/mysql/plugin/mypluglib.so
      <snip>
      - usr/bin/my_print_defaults
      - usr/bin/mysqldump
      - usr/bin/mysql_tzinfo_to_sql
      - usr/bin/mysql_upgrade
      - usr/share/mysql/*
```

The script called at the `prepare` stage is `stage_binaries.sh`
(source: <https://github.com/mysql/mysql-snap/blob/5.7/stage_binaries.sh>)

```
SNAPDIR=$(pwd)
SNAPTMP=$(mktemp -d)
cd ${SNAPTMP}
MYSQL_VERSION_MAJOR=5.7
MYSQL_VERSION_FULL=5.7.17-1ubuntu16.04
FILENAME="mysql-server_${MYSQL_VERSION_FULL}_amd64.deb-bundle.tar"
wget "http://dev.mysql.com/get/Downloads/MySQL-{MYSQL_VERSION_MAJOR}/${FILENAME}"
tar -xvf "${FILENAME}"
ar x mysql-community-client_${MYSQL_VERSION_FULL}_amd64.deb
tar -xvf data.tar.xz
rm data.tar.xz
ar x mysql-community-server_${MYSQL_VERSION_FULL}_amd64.deb
tar -xvf data.tar.xz
mkdir staging-files
mv usr staging-files/
rm -rf ${SNAPDIR}/staging-files
mv staging-files ${SNAPDIR}
cd ${SNAPDIR}
```

It uses `wget` to download mysql debian packages, unpack them and copy their content into the current directory.

Once this prepare script is run, the dump plugin proceeds as usual: taking content from the `source` and including it in the final snap.

Without this scriptlet, there would have been a need for a custom plugin or several other parts that would have increased the complexity of the snapcraft.yaml.

### More examples

You will find usage snippets on the [scriptlets page at snapcraft.io](http://snapcraft.io/docs/build-snaps/scriptlets) and in the snapcraft test suite:

* [Prepare](https://github.com/snapcore/snapcraft/blob/master/integration_tests/snaps/scriptlet-prepare/snapcraft.yaml)
* [Build](https://github.com/snapcore/snapcraft/blob/master/integration_tests/snaps/scriptlet-build/snapcraft.yaml)
* [Install](https://github.com/snapcore/snapcraft/blob/master/integration_tests/snaps/scriptlet-install/snapcraft.yaml)

[Get in touch

Interested in running Ubuntu in your organization?](https://ubuntu.com/about/contact-us/form)

## Sign up for our newsletter

Get the latest Canonical news and updates in your inbox.

Work email:

\*I agree to receive information about Canonical's
products and services.

By submitting this form, I confirm that I have read and agree to [Canonical's Privacy Policy](https://canonical.com/legal/dataprivacy).

Sign up

## Share on

---

## Related posts

[### We wish you RISC-V holidays!](https://canonical.com/blog/we-wish-you-risc-v-holidays)

There are three types of computer users: the end user, the system administrator, and the involuntary system administrator. As it happens, everyone has found themselves in the...

[Igor Ljubuncic](https://canonical.com/blog/author/igorljubuncic)

21 December 2023

[### Snapcraft 8.0 and the respectable end of core18](https://canonical.com/blog/snapcraft-8-0-and-the-respectable-end-of-core18)

‘E’s not pinin’! ‘E’s passed on! This base is no more! He has ceased to be! ‘E’s expired and gone to meet ‘is maker! ‘E’s a stiff! Bereft of life, ‘e rests in peace! If you...

[Igor Ljubuncic](https://canonical.com/blog/author/igorljubuncic)

16 June 2023

[### Optimise your ROS snap – Part 6](https://canonical.com/blog/optimise-your-ros-snap-part-6)

Welcome to Part 6 of our “Optimise your ROS snap” blog series. Make sure to check Part 5. This sixth and final part will summarise every optimisation that we did. We will...

[gbeuzeboc](https://canonical.com/blog/author/gbeuzeboc)

27 April 2023

[### TurtleBot3 OpenCR firmware update from a snap](https://canonical.com/blog/turtlebot3-opencr-firmware-update-from-a-snap)

The TurtleBot3 robot is a standard platform robot in the ROS community, and it’s a reference that Canonical knows well, since we’ve used it in our tutorials. As a matter of...

[gbeuzeboc](https://canonical.com/blog/author/gbeuzeboc)

25 September 2024
