Table of Contents
If you've stumbled upon this post it most likely means that you're struggling for a way to build your Electron app in a Linux distributable format such as the following: AppImage, deb, rpm, snap, freebsd, and so on. This guide has been written to help you with such specific task and will hopefully allow you to get the task done in less than five minutes.
Introduction
In this post we'll make use of the tool known as electron-builder, which in our opinion is currently the best available solution to package and build a ready for distribution Electron app for macOS, Windows and Linux.
Installing electron-builder is very easy, we just have to have NodeJS installed and run the following command:
> yarn add electron-builder --dev
For additional info about installing and/or configuring the tool, please refer to the electron-builder official documentation: in a nutshell, you'll have to create a package.json file and configure it to your needs, then run electron-builder from the command-line (optionally with command-line options, depending on your specific scenario) and get your Electron app built.
For the sake of simplicity, we'll take for granted that the reader already knows how to configure it to build a distributable Windows package with a package.json similar to the following one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
{ "name": "my-app", "version": "1.0.0", "description": "That's just a sample description", "repository": "https://github.com/MyApp/my-app", "homepage": "https://github.com/MyApp/my-app", "author": { "name": "MyApp Owner", }, "license": "MIT", "keywords": [ "Electron", "my-app", "MyApp" ], "main": "src/main.js", "scripts": { "start": "electron .", "pack": "electron-builder --dir", "distW": "electron-builder --ia32 --x64 -w", "dist": "electron-builder" }, "build": { "appId": "com.my.app", "productName": "My App", "win": { "target": "nsis" }, "nsis": { "oneClick": false, "perMachine": false, "allowToChangeInstallationDirectory": true }, "artifactName": "MyApp_${version}_Setup.${ext}", "forceCodeSigning": false }, "dependencies": { "@lapo/extractcms": "^1.0.3", "adm-zip": "^0.4.13", "xmldom": "^0.1.27" }, "devDependencies": { "electron": "^4.0.3", "electron-builder": "^22.8.1" } } |
As we can see, the package.json contains the required JSON keys to build the distributable package for Windows (win and nsis), but there are no references for Linux yet. Let's start from here.
Adding the Linux JSON keys
Here are the required JSON keys to build a basic Linux distributable package (in deb format, thus compatible with Debian, Ubuntu and the likes) for our Electron App:
1 2 3 4 |
"linux": { "target": "deb" }, "deb": {} |
These keys should be placed at the same level of the already-existing "win" and "nsis" keys, ideally immediately below them. Once you do that, you're ready to try and build your Electron App for Linux using the built-in multi-platrform build feature provided by electron-builder, which relies on a free public Electron Build Service that is internally called by the compiler and ideally performs the building task remotely (think of them as some free remote web services). That's great, right? As a matter of fact, it is...
...or maybe we should say it used to be, since such incredible feature is unfortunately broken since months, to the point that it seems unlikely to be fixed. Here's a collection of GitHub issues and StackOverflow threads that have been opened from users that are trying to deal with such issue:
- service.electron.build is unavailable
- Error: Cannot get, wait error=Get https://service.electron.build/find-build-agent
- service.electron.build is unavailable - Urgently please suggest an alternate
- Local build service not detected
- Unable to build AppImage on Windows - service.electron.build
- Connection to remote builder refused, while building linux package
- electron-build fails with ERR_ELECTRON_BUILDER_CANNOT_EXECUTE
... and so on.
Host your own (docker) Build Service
Until the complimentary Electron Build Service will become available again, the best workaround we can pull off is to try and host our own Build Service: luckily enough there's a great Docker image (electronuserland/builder) that can be used to achieve such result in few minutes. Here are the required steps:
- Install Docker from the Get-Docker official website.
- Download the electronuserland/builder image using the following command-prompt console command:
> docker pull electronuserland/builder
- Navigate to your Electron project's root folder ( for example, C:\MyApp )
- Run the electronuserland/builder container using the following command from the command line:
> docker run -rm -ti -v C:\MyApp\:/project -w /project electronuserland/builder
The above command will launch the Docker container with your Electron App's root folder mounted inside the /project/ virtual path. Once you get the docker's container prompt - a Linux terminal by all means - type the following commands to get inside that folder, upgrade your Electron project's Yarn packages, globally install the electron-builder package and, last but not least, build the Linux redistributable package.
> cd /project
> yarn upgrade
> yarn global add electron-builder
> electron-builder
If everything went smooth, you should be able to locate your MyApp.deb file inside the Electron project's /dist/ folder.
Conclusion
That's it, at least for now: I hope that this post will help those who're looking for a way to build their Electron App for Linux from their Windows environment in a quick and effective way.