Sign in Join
Wiki copytemporary

Create snap package: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Create snap package

OldNewLine
1+Snap is a containerized package format which allows the distribution of programs to all Linux distributions. A snap is in a container and has only explicitly defined access to the "outside world", e.g. some libraries of the system or the user's home directory.
2+
3+# Create snapcraft.yaml
4+
5+- Install snapcraft to your Ubuntu machine with `sudo apt install snapcraft`
6+- Create **snap** subdirectory in your project
7+- Execute `snapcraft init` to create **snap/snapcraft.yaml** initial file
8+- See [Snapcraft.yaml reference](https://snapcraft.io/docs/snapcraft-yaml-reference) for full list of supported properties.
9+- Adapt the file so that it contains all necessary instructions for building the Lazarus program snap.
10+
11+# Example files
12+
13+## Qt5
14+
15+The kde-neon extension and some tricks will help you to reduce the size of the Snap package to almost the normal binary size. The only thing to keep in mind is that Qt5 needs the libqt5pas-library. [In a snapcraft forum post](https://forum.snapcraft.io/t/reduce-size-of-qt5-app-snap/31030), we were figuring out how to create a small Lazarus Qt5 snap with all needed libraries included. You can find an example at the [Cantara project on Github](https://github.com/reckel-jm/cantara/blob/master/snap/snapcraft.yaml) or check the instruction below.
16+
17+``` yaml
18+name: myapp
19+version: 'version'
20+summary: any summary
21+description: |
22+ Any description
23+confinement: strict
24+icon: path-to-logo.png
25+base: core20
26+grade: stable
27+
28+architectures:
29+ - build-on: amd64
30+
31+parts:
32+ myapp:
33+ plugin: nil
34+ source: .
35+ source-type: local
36+ build-packages:
37+ - fpc
38+ - lazarus
39+ - lcl
40+ - lcl-utils
41+ - build-essential
42+ - libqt5pas-dev
43+ stage-packages:
44+ - libqt5pas1
45+ - libxinerama1
46+ override-build: |
47+ lazbuild -B --ws=qt5 myapp.lpi
48+ # Here you have to install (or copy) all the files of your project which you would like to distribute to $SNAPCRAFT_PART_INSTALL/ e.g. the compiled binary, icons and language files
49+ cp -r languages $SNAPCRAFT_PART_INSTALL/
50+ install myapp $SNAPCRAFT_PART_INSTALL/
51+ install myapp.desktop $SNAPCRAFT_PART_INSTALL/
52+ install myapp.ico $SNAPCRAFT_PART_INSTALL/
53+ install myapp.png $SNAPCRAFT_PART_INSTALL/
54+ stage:
55+ - lib
56+ - usr
57+ - etc
58+ # make sure that you stage the files and folders to which you have installed (copied) above!
59+ - myapp
60+ - languages
61+ - myapp.desktop
62+ - myapp.ico
63+ - myapp.png
64+ cleanup: # this will make your snap small but keeps the needed libraries
65+ after: [myapp]
66+ plugin: nil
67+ build-snaps: [kde-frameworks-5-91-qt-5-15-3-core20]
68+ override-prime: |
69+ set -eux
70+ for snap in "kde-frameworks-5-91-qt-5-15-3-core20"; do # List all content-snaps you're using here
71+ cd "/snap/$snap/current" && find . -type f,l -exec rm -f "$SNAPCRAFT_PRIME/{}" "$SNAPCRAFT_PRIME/usr/{}" \;
72+ done
73+ for cruft in bug lintian man icons; do
74+ rm -rf $SNAPCRAFT_PRIME/usr/share/$cruft
75+ done
76+ find $SNAPCRAFT_PRIME/usr/share/doc/ -type f -not -name 'copyright' -delete
77+ find $SNAPCRAFT_PRIME/usr/share -type d -empty -delete
78+ find $SNAPCRAFT_PRIME/usr/lib -type f,l -name 'libQt*.so*' ! -name 'libQt5Pas*.so*' -delete
79+
80+apps:
81+ myapp:
82+ extensions:
83+ - kde-neon # Don't remove this extension for it provides access to Qt5!
84+ command: myapp # to change: the command on which the app gets started
85+ desktop: myapp.desktop # to change: the path to the desktop file which will be used to create the entry in the start menu etc.
86+ plugs:
87+ - home
88+ - network
89+ - network-bind
90+```
91+
92+## GTK2
93+
94+``` yaml
95+name: myapp
96+version: '1.0.0'
97+summary: Short description of the package.
98+description: |
99+ Some more detailed multi-line description.
100+confinement: strict
101+base: core20
102+grade: stable
103+
104+parts:
105+ myapp:
106+ plugin: nil
107+ source: .
108+ source-type: local
109+ build-packages:
110+ - fpc
111+ - lazarus
112+ - lcl
113+ - lcl-utils
114+ stage-packages:
115+ # Autodetected dependencies
116+ - libatk1.0-0
117+ - libcairo2
118+ - libdatrie1
119+ - libfontconfig1
120+ - libfreetype6
121+ - libfribidi0
122+ - libgdk-pixbuf2.0-0
123+ - libgraphite2-3
124+ - libgtk2.0-0
125+ - libharfbuzz0b
126+ - libpango-1.0-0
127+ - libpangocairo-1.0-0
128+ - libpangoft2-1.0-0
129+ - libpixman-1-0
130+ - libpng16-16
131+ - libthai0
132+ - libx11-6
133+ - libxau6
134+ - libxcb-render0
135+ - libxcb-shm0
136+ - libxcb1
137+ - libxcomposite1
138+ - libxcursor1
139+ - libxdamage1
140+ - libxdmcp6
141+ - libxext6
142+ - libxfixes3
143+ - libxi6
144+ - libxinerama1
145+ - libxrandr2
146+ - libxrender1
147+ override-build: |
148+ lazbuild --build-mode=Release myapp.lpi
149+ install -d -m 755 $SNAPCRAFT_PART_INSTALL/usr/bin
150+ install -s -m 755 myapp $SNAPCRAFT_PART_INSTALL/usr/bin/myapp
151+ install -d -m 755 $SNAPCRAFT_PART_INSTALL/usr/share/applications
152+ install -m 755 myapp.desktop $SNAPCRAFT_PART_INSTALL/usr/share/applications
153+ stage:
154+ - etc
155+ - lib
156+ - usr
157+ - usr/bin
158+ - usr/share/myapp
159+ - usr/share/applications/myapp.desktop
160+
161+apps:
162+ myapp:
163+ command: usr/bin/myapp/myapp
164+ desktop: usr/share/applications/myapp.desktop
165+ plugs:
166+ - home
167+ - desktop
168+ - x11
169+```
170+
171+- Lazarus still uses old Gtk2 libraries. See [GTK2 Applications](https://snapcraft.io/docs/gtk2-applications) page.
172+
173+# Build Snap
174+
175+- Build snap using **snapcraft** command
176+- Install newly created snap with:
177+
178+``` bash
179+sudo snap install --dangerous myapp_1.0.0_amd64.snap
180+```
181+
182+# Publish your package in Snap Store
183+
184+``` bash
185+snapcraft register
186+snapcraft login
187+snapcraft upload myapp.snap
188+snapcraft release myapp 1 stable
189+```
190+
191+Now your snap should be released and available on **snapcraft.io** store.
192+
193+# Debug snap
194+
195+To get to console of snap virtual filesystem use:
196+
197+``` bash
198+snap run --shell myapp
199+cd $SNAP
200+```
201+
202+Type **exit** to leave snap environment. See [Debugging snaps](https://snapcraft.io/docs/debug-snaps) for more information.
203+
204+# Use newer Lazarus version
205+
206+Normally you can use Lazarus IDE version provided by current base (2.0.6 for core20). To use latest stable version with new features you can add following part into your snapcraft.yaml file. This part should be executed before myapp part. You need to update fixed URL based on required Lazarus version. Then those packages will be downloaded and installed before build.
207+
208+``` yaml
209+parts:
210+ lazarus:
211+ plugin: nil
212+ source: .
213+ source-type: local
214+ build-packages:
215+ - wget
216+ - libgtk2.0-dev
217+ override-build: |
218+ wget -nc https://downloads.sourceforge.net/project/lazarus/Lazarus%20Linux%20amd64%20DEB/Lazarus%203.2/lazarus-project_3.2.0-0_amd64.deb
219+ wget -nc https://downloads.sourceforge.net/project/lazarus/Lazarus%20Linux%20amd64%20DEB/Lazarus%203.2/fpc-laz_3.2.2-210709_amd64.deb
220+ wget -nc https://downloads.sourceforge.net/project/lazarus/Lazarus%20Linux%20amd64%20DEB/Lazarus%203.2/fpc-src_3.2.2-210709_amd64.deb
221+ apt install ./lazarus-project_3.2.0-0_amd64.deb ./fpc-laz_3.2.2-210709_amd64.deb ./fpc-src_3.2.2-210709_amd64.deb
222+ prime: [-*]
223+ myapp:
224+ after: [lazarus]
225+```
226+
227+And remove normal Lazarus build-packages from your app so they are not installed for build:
228+
229+``` yaml
230+ myapp:
231+ build-packages:
232+ - fpc
233+ - lazarus
234+ - lcl
235+ - lcl-utils
236+```
237+
238+# GTK2 theme support
239+
240+There is no snapcraft extension available for GTK2 similarly to GTK3. So GTK2 theming needs to be configured with additional lines.
241+
242+## Content plugs only
243+
244+To support GTK2 themes used by host system the app needs to have access to GTK2 themes. Standard GTK2 themes can be made available by using content plugs. Those content plugs are external shared packages so they won't make snap bigger. But the application can still print some errors into console.
245+
246+Extend your snapcraft.yaml file with the following:
247+
248+``` yaml
249+# Additional plugs to pick up the GTK theme and icons from the system
250+plugs:
251+ icon-themes:
252+ interface: content
253+ target: $SNAP/data-dir/icons
254+ default-provider: gtk-common-themes
255+ sound-themes:
256+ interface: content
257+ target: $SNAP/data-dir/sounds
258+ default-provider: gtk-common-themes
259+ gtk-2-engines:
260+ interface: content
261+ target: $SNAP/lib/gtk-2.0
262+ default-provider: gtk2-common-themes:gtk-2-engines
263+ gtk-2-themes:
264+ interface: content
265+ target: $SNAP/usr/share/themes
266+ default-provider: gtk2-common-themes:gtk-2-themes
267+
268+layout:
269+ /usr/share/themes:
270+ bind: $SNAP/usr/share/themes
271+ /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/gtk-2.0:
272+ bind: $SNAP/lib/gtk-2.0
273+```
274+
275+## Full support
276+
277+To fully support GTK2 environment we need to use snapcraft desktop helper and include various standard packages. Those packages make snap bigger by ~26 MB so there is a trade-off between size of snap packages and supported features.
278+
279+Extend your snapcraft.yaml file with the following:
280+
281+``` yaml
282+parts:
283+ desktop-gtk2:
284+ source: https://github.com/ubuntu/snapcraft-desktop-helpers.git
285+ source-subdir: gtk
286+ plugin: make
287+ make-parameters: ["FLAVOR=gtk2"]
288+ build-packages:
289+ - build-essential
290+ - libgtk2.0-dev
291+ stage-packages:
292+ - libxkbcommon0 # XKB_CONFIG_ROOT
293+ - ttf-ubuntu-font-family
294+ - dmz-cursor-theme
295+ - light-themes
296+ - adwaita-icon-theme
297+ - gnome-themes-standard
298+ - shared-mime-info
299+ - libgtk2.0-0
300+ - libgdk-pixbuf2.0-0
301+ - libglib2.0-bin
302+ - libgtk2.0-bin
303+ - unity-gtk2-module
304+ - locales-all
305+ - libappindicator1
306+ - xdg-user-dirs
307+ - ibus-gtk
308+ - libibus-1.0-5
309+ myapp:
310+ after:
311+ - desktop-gtk2
312+
313+# Additional plugs to pick up the GTK theme and icons from the system
314+plugs:
315+ icon-themes:
316+ interface: content
317+ target: $SNAP/data-dir/icons
318+ default-provider: gtk-common-themes
319+ sound-themes:
320+ interface: content
321+ target: $SNAP/data-dir/sounds
322+ default-provider: gtk-common-themes
323+ gtk-2-engines:
324+ interface: content
325+ target: $SNAP/lib/gtk-2.0
326+ default-provider: gtk2-common-themes:gtk-2-engines
327+ gtk-2-themes:
328+ interface: content
329+ target: $SNAP/usr/share/themes
330+ default-provider: gtk2-common-themes:gtk-2-themes
331+
332+environment:
333+ XDG_DATA_DIRS: $SNAP/usr/share:$XDG_DATA_DIRS
334+ GTK_PATH: $SNAP/lib/gtk-2.0
335+
336+apps:
337+ myapp:
338+ command: usr/bin/myapp
339+ command-chain:
340+ - bin/desktop-launch
341+```
342+
343+# Sound support
344+
345+Snap supports sound through **audio-playback** plug.
346+
347+``` yaml
348+apps:
349+ myapp:
350+ plugs:
351+ - audio-playback
352+```
353+
354+You can play wav and mp3 files from command line using **play** command. See [[Play Sound Multiplatform]]. Then you also need to add additional stage-packages to myapp part and setup sox player for pulseaudio:
355+
356+``` yaml
357+parts:
358+ myapp:
359+ stage-packages:
360+ - sox
361+ - libsox-fmt-mp3
362+ - libsox-fmt-pulse
363+ - libpulse0
364+
365+environment:
366+ LD_LIBRARY_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/pulseaudio
367+ PULSE_SERVER: unix:/run/user/1000/pulse/native
368+
369+layout:
370+ /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/sox:
371+ bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/sox
372+```
373+
374+# OpenGL support
375+
376+To support 3D acceleration it is required to use OpenGL plug, package related libraries and set correct path for drivers.
377+
378+``` yaml
379+parts:
380+ myapp:
381+ stage-packages:
382+ - libglu1-mesa
383+ - libgl1-mesa-dri
384+
385+apps:
386+ myapp:
387+ plugs:
388+ - opengl
389+
390+environment:
391+ LIBGL_DRIVERS_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/dri
392+```
393+
394+# SDL support
395+
396+Add needed build and stage packages.
397+
398+``` yaml
399+parts:
400+ myapp:
401+ build-packages:
402+ - libsdl2-dev
403+ - libsdl2-mixer-dev
404+ - libsdl2-image-dev
405+ stage-packages:
406+ - libsdl2-2.0-0
407+ - libsdl2-image-2.0-0
408+ - libsdl2-mixer-2.0-0
409+```
410+
411+# snapcraft.yaml file in non-standard subdirectory
412+
413+Snapcraft normally expect snapcraft.yaml file in snap directory. If your project has packaging for multiple other packaging formats, then you need to use build workaround script. Such shell script can put into directory like myapp/install/snap/local as build.sh file.
414+
415+``` bash
416+#!/bin/bash
417+ln -s install/snap ../../../snap
418+pushd ../../..
419+snapcraft
420+popd
421+rm ../../../snap
422+```
423+
424+# Register app in snap store
425+
426+- Login into <https://snapcraft.io> with your user account
427+- Register new snap package name <https://snapcraft.io/account/register-snap>
428+- Go to your newly registered package and fill all needed details <https://snapcraft.io/snaps>
429+
430+# Setup build on Launchpad
431+
432+Snap packages can be built automatically on [Launchpad](https://launchpad.net/). Open **Create snap package** link form your application branch page.
433+
434+# See also
435+
436+- [[Publish project on Launchpad]]
437+- [[Deploying Your Application]]
438+
439+# External links
440+
441+- [How To Package and Publish a Snap Application on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-package-and-publish-a-snap-application-on-ubuntu-18-04)
442+- [Sample snapcraft.yaml files using lazbuild on Github](https://github.com/search?q=filename%3Asnapcraft.yaml+%22lazbuild%22&type=Code)
443+
444+---
445+*From the Free Pascal wiki, page "Create snap package", as it stood on 2024-05-21 after a change by Chronos, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
446+Category: Deployment
447+Category: Linux

Back to history