Snapcraft Guide (Linux)
This guide provides information on how to package your Electron application for any Snapcraft environment, including the Ubuntu Software Center.
Предыстория и требования
Together with the broader Linux community, Canonical aims to fix many of the common software installation problems with the snapcraft
project. Snaps are containerized software packages that include required dependencies, auto-update, and work on all major Linux distributions without system modification.
There are three ways to create a .snap
file:
- Using Electron Forge or
electron-builder
, both tools that come withsnap
support out of the box. This is the easiest option. 2) Usingelectron-installer-snap
, which takes@electron/packager
's output. 3) Using an already created.deb
package.
В некоторых случаях вам нужно установить инструмент snapcraft
. Инструкции по установке snapcraft
для вашего конкретного дистрибутива доступны здесь.
Использование electron-installer-snap
The module works like electron-winstaller
and similar modules in that its scope is limited to building snap packages. Вы можете установить его с:
npm install --save-dev electron-installer-snap
1 Шаг: Упакуйте ваше приложение
Package the application using @electron/packager (or a similar tool). Удостоверьтесь, что вы удалили node_modules
, которые не понадобятся вам в финальной версии приложения, так как любой неиспользуемый модуль в конечном счете увеличит размер вашего приложения.
Консольный вывод должен иметь примерный вид:
.
└── dist
└── app-linux-x64
├── LICENSE
├── LICENSES.chromium.html
├── content_shell.pak
├── app
├── icudtl.dat
├── libgcrypt.so.11
├── libnode.so
├── locales
├── resources
├── v8_context_snapshot.bin
└── version
Step 2: Running electron-installer-snap
С терминала, который имеет snapcraft
в PATH
, запустить electron-installer-snap
с единственным требуемым параметром --src
, которое является местоположением вашего упакованного приложения Electron, созданного на первом шаге.
npx electron-installer-snap --src=out/myappname-linux-x64
If you have an existing build pipeline, you can use electron-installer-snap
programmatically. For more information, see the Snapcraft API docs.
const snap = require('electron-installer-snap')
snap(options)
.then(snapPath => console.log(`Created snap at ${snapPath}!`))
Using snapcraft
with @electron/packager
Step 1: Create Sample Snapcraft Project
Create your project directory and add the following to snap/snapcraft.yaml
:
name: electron-packager-hello-world
version: '0.1'
summary: Hello World Electron app
description: |
Simple Hello World Electron app as an example
base: core22
confinement: strict
grade: stable
apps:
electron-packager-hello-world:
command: electron-quick-start/electron-quick-start --no-sandbox
extensions: [gnome]
plugs:
- browser-support
- network
- network-bind
environment:
# Correct the TMPDIR path for Chromium Framework/Electron to ensure
# libappindicator has readable resources.
TMPDIR: $XDG_RUNTIME_DIR
parts:
electron-quick-start:
plugin: nil
source: https://github.com/electron/electron-quick-start.git
override-build: |
npm install electron @electron/packager
npx electron-packager . --overwrite --platform=linux --output=release-build --prune=true
cp -rv ./electron-quick-start-linux-* $SNAPCRAFT_PART_INSTALL/electron-quick-start
build-snaps:
- node/14/stable
build-packages:
- unzip
stage-packages:
- libnss3
- libnspr4
If you want to apply this example to an existing project:
- Replace
source: https://github.com/electron/electron-quick-start.git
withsource: .
. - Replace all instances of
electron-quick-start
with your project's name.
Step 2: Build the snap
$ snapcraft
<output snipped>
Snapped electron-packager-hello-world_0.1_amd64.snap
Step 3: Install the snap
sudo snap install electron-packager-hello-world_0.1_amd64.snap --dangerous
Step 4: Run the snap
electron-packager-hello-world