Run Flutter Applications on Ubuntu Core

Overview

Since 2000, Qt is dominating the devices where a UI is required. Recently, Flutter introduced native Linux support over x11 and/or Wayland. Thus, now it is possible to run Flutter Apps on Linux.

What you’ll learn

In this tutorial, you will learn how to generate, build and snap a Flutter applications on Ubuntu Core by leveraging Ubuntu-Frame.

What you’ll need

  • An Ubuntu desktop running any current release of Ubuntu or an Ubuntu Virtual Machine on another OS.

  • A ‘Target Device’ from one of the following:

    • A device running Ubuntu Core.
      This guide shows you you how to set up a supported device. If there’s no supported image that fits your needs you can create your own image.
    • Using a Virtual Machine (VM) You don’t need to have a physical “Target Device”, you can follow the tutorial with Ubuntu Core in a VM. This guide shows you how to set up an Ubuntu Core VM.
    • Using Ubuntu Classic You don’t have to use Ubuntu Core, you can use also a “Target Device” with Ubuntu Classic. Read this guide to understand how to run kiosk snaps on your desktop, as the particular details won’t be repeated here.

Getting Started

To be able to create and build Flutter applications, you would need to have Flutter and its tools installed. There are different ways of installing Flutter on your Ubuntu Classic but the recommended way is using the snap.

sudo snap install flutter --classic

In addition to that snapcraft tool must be installed so that you can create snap out of your Flutter application. You can use the following command to install snapcraft in your Ubuntu Classic system.

sudo snap install snapcraft --classic

Now, we are ready to create our Flutter application.

Creating and Building Flutter Applications

Flutter does not enable desktop support by default. You have to explicitly configure your Flutter CLI tool. The rest is creating the project and running it. Easy peasy !

flutter config --enable-linux-desktop # enable desktop support
flutter create flutterdemo # your flutter application name
cd flutterdemo # Go to your app's root directory
flutter run -d linux # Start your flutter application.

Once you run the above commands, you have to see the following screen on your Ubuntu Classic.

Perfect. Now our Flutter application is working fine on Ubuntu Classic. As a next step, we have create snap to be able to run it on Ubuntu Core.

Creating the Snap

Creating a snap package from your existing applications is quite easy task thanks to the snapcraft CLI tool.

cd your-app-directory # go to your Flutter app's root directory
snapcraft init # initiate your snap project

Now, it needs a little bit of attention. Here is the initial version of the snapcraft.yaml file of our application. Let’s call our application as super-cool-app :slight_smile:

name: super-cool-app
version: 0.1.0
summary: Super Cool App
description: Super Cool App

confinement: strict
base: core18
grade: stable

slots:
 dbus-super-cool-app:
   interface: dbus
   bus: session
   name: com.example.flutterdemo # replace with your app name
   
apps:
 super-cool-app:
   command: flutterdemo # replace with your app name
   extensions: [flutter-master] 
   plugs:
   - network
   slots:
     - dbus-super-cool-app
parts:
 super-cool-app:
   source: .
   plugin: flutter
   flutter-target: lib/main.dart # The main entry-point file of the application

Our snapcraft.yaml is ready to be built. It is suggested to use lxd while building snaps so that your machine will not be polluted.

snapcraft --use-lxd

If everything goes well, you will have your snap package in your directory. You can install it by using the following command. We have to use dangerous flag since we did not deploy our application to the Snap Store.

sudo snap install <your-app-name.snap> --dangerous # install your snap to your ubuntu classic system
super-cool-app # run your snap, replace with your own snap app

Now, you should be able to see it working on the Desktop. That’s great. You turned your application into the snap package format and made it possible to run on your Desktop :slight_smile:

But, we have to adjust our snapcraft.yaml file more to make it run on Ubuntu Core by leveraging the ubuntu-frame.

Adjustments for Ubuntu Core

There are three points that we need to take care of in order to run the Flutter applications on Ubuntu Core.

  • Currently, the Ubuntu Core platform does not have the required fonts that are needed by Flutter applications.

  • UI apps generally must be available upon boot and therefore to satisfy this we have to create a daemon.

  • Since Flutter apps are running on top of ubuntu-frame, we have to make necessary adjustments for ubuntu-frame integration.

Here is the snapcraft.yaml where all the mentioned issues are addressed.

name: super-cool-app
version: 0.1.0
summary: Super Cool App
description: Super Cool App that does everything!

confinement: strict
base: core18
grade: stable

apps:
  daemon:
    daemon: simple
    restart-condition: always
    command-chain:
      - bin/run-daemon
      - bin/wayland-launch
    command: bin/flutterdemo
    extensions: [flutter-master] # Where "master" defines which Flutter channel to use for the build

  super-cool-app:
    command-chain:
      - bin/wayland-launch
    command: flutterdemo
    extensions: [flutter-master] # Where "master" defines which Flutter channel to use for the build

parts:
  super-cool-app:
    source: .
    plugin: flutter
    flutter-target: lib/main.dart # The main entry-point file of the application

  mir-kiosk-snap-launch:
    plugin: dump
    source: https://github.com/MirServer/mir-kiosk-snap-launch.git
    override-build:  $SNAPCRAFT_PART_BUILD/build-with-plugs.sh opengl pulseaudio wayland
    stage-packages:
      - inotify-tools

  assets:
    plugin: nil
    stage-packages:
    - dmz-cursor-theme
    - fonts-dejavu
    - fonts-freefont-ttf
    - fonts-ubuntu

layout:
  /usr/share/fonts:
    bind: $SNAP/usr/share/fonts
  /etc/fonts:
    bind: $SNAP/etc/fonts
  /usr/share/icons:
    bind: $SNAP/usr/share/icons

Re-create your snap after these modifications and now we are ready to test it on Ubuntu Core.

Running Your Flutter Application Snap on Ubuntu Core

You can use qemu and pre-built Ubuntu Core18 or Ubuntu Core20 images that are available to test the generated snap.

Assuming you already have Ubuntu Core system up and running, just install ubuntu-frame using

snap install ubuntu-frame

Then, transfer your snap via scp. Finally install it by using;

sudo snap install <your-app-name.snap> --dangerous

After a couple of seconds, your application will appear on your screen!

Congratulations!

You made it!

If you are looking for more advanced Flutter Applications, you can find the reference snapcraft.yaml file for the Flutter Gallery Demo app that is provided by Flutter community to show case most of the features that Flutter offers.

3 Likes