如何在 Ubuntu 或 CentOS 上安装 Snapcraft

什么是 Snapcraft?

Snapcraft 是一个用于构建快照的命令行实用程序。 该软件允许用户构建自己的应用程序或软件包,然后发布到 快照存储 供其他用户共享和使用! 在本教程中,我们将学习如何在 Ubuntu 和 CentOS 上安装 Snapcraft。

什么是快照?

Snap,也称为 Snappy,是一种流行的 Linux 包管理系统,提供对称为 snaps 的自包含包的访问。 Snap 与 yum 或 apt-get 等其他包管理系统的不同之处在于,它不仅仅针对一个 Linux 发行版。 嗯,对于 example是默认的包管理器 Fedora、CentOS 和 Redhat,但 Ubuntu 和 Debian 等发行版使用 apt-get 作为默认包管理器。 Snap 软件包适用于基于 Debian 和 RHEL 的发行版,因为 Snap 封装了自己的依赖项。

飞行前检查

  • CentOS 需要访问 EPEL 软件包存储库才能安装 snapd,这是安装 Snapcraft 的先决条件。 这是有关如何设置 epel 存储库的教程。
  • 这些指令分别在 CentOS 7 和 Ubuntu 18.04 LTS 服务器上执行。
  • 确保您以 root 用户身份登录到这些服务器。

CentOS 7

第 1 步:安装 snapd

首先,作为最佳实践,确保所有软件包都是最新的:

yum update -y

接下来安装 snapd:

yum install -y snapd

第 2 步:启用 Snap.socket

安装 snapd 后,需要启用 snap.socket:

[root@host ~]# systemctl enable --now snapd.socket
Created symlink from /etc/systemd/system/sockets.target.wants/snapd.socket to /usr/lib/systemd/system/snapd.socket.
[root@host ~]# 

第 3 步:启用经典 Snap 支持

某些 Snap 软件包需要有 /snap 目录。 此功能不随 Snap 本身提供,但为了确保您可以安装任何 Snap 软件包,您需要创建一个符号链接以启用经典 Snap 支持:

ln -s /var/lib/snapd/snap /snap

接下来,刷新您的终端会话:

exec bash

第 4 步:安装 Snapcraft

现在已安装 snapd,您可以安装 Snapcraft! 安装 Snapcraft 的最简单方法是使用 Snap 安装它:

[root@host ~]# snap install snapcraft --classic
2019-10-10T10:54:15-04:00 INFO Waiting for restart...
snapcraft 3.8 from Canonical✓ installed
[root@host ~]# 

安装错误

注意:在测试期间,我们在 CentOS 7 服务器上尝试使用 snap 安装 snapcraft 时确实遇到了一个错误。 错误说明:“错误:操作太早,设备尚未播种或设备型号未确认”。 我们卸载了 snapd [yum remove snapd]
然后使用重新安装它 [yum install -y snapd].

[root@host ~]# yum install -y snapd
Loaded plugins: fastestmirror, priorities, tmprepo
Loading mirror speeds from cached hostfile
 * base: mirrors.BYNSS.com
 * epel: mirrors.BYNSS.com
 * extras: mirrors.BYNSS.com
 * updates: mirrors.BYNSS.com
Resolving Dependencies
--> Running transaction check
---> Package snapd.x86_64 0:2.39.2-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

 Package Arch Version Repository Size 

Installing:
 snapd  x86_64 2.39.2-1.el7 epel 14 M

Transaction Summary

Install  1 Package

Total download size: 14 M
Installed size: 43 M
Downloading packages:
snapd-2.39.2-1.el7.x86_64.rpm |  14 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : snapd-2.39.2-1.el7.x86_64  1/1 
  Verifying  : snapd-2.39.2-1.el7.x86_64  1/1 

Installed:
  snapd.x86_64 0:2.39.2-1.el7

Complete!

然后我们再次尝试安装 Snapcraft:

[root@host ~]# snap install snapcraft --classic
error: too early for operation, device not yet seeded or device model not acknowledged

我们等了大约十分钟,然后重新运行 Snapcraft 安装:

[root@host ~]# snap install snapcraft --classic
2019-10-10T11:11:15-04:00 INFO Waiting for restart...
snapcraft 3.8 from Canonical✓ installed
[root@host ~]# 
[root@host ~]# which snapcraft
/var/lib/snapd/snap/bin/snapcraft

固定的!

接下来,验证 Snapcraft 是否已启动且可用:

[root@host ~]# snapcraft -h
<pre>Usage: snapcraft [OPTIONS] COMMAND [ARGS]...

  Snapcraft is a delightful packaging tool.

Options:
  --version    Show the version and exit.
  -d, --debug
  -h, --help   Show this message and exit.

Commands:
  build              Build artifacts defined for a part.
  clean              Remove a part's assets.
  close              Close <channel> for <snap-name>.
  create-key         Create a key to sign assertions.
  enable-ci          Enable continuous-integration systems to build and...
  expand-extensions  Display snapcraft.yaml with all extensions applied.
  export-login       Save login configuration for a store account in FILE.
  extension          Show contents of extension.
  gated              Get the list of snaps and revisions gating a snap.
  help               Obtain help for a certain topic, plugin or command.
  init               Initialize a snapcraft project.
  list-extensions    List available extensions.
  list-keys          List the keys available to sign assertions.
  list-plugins       List the available plugins that handle different types...
  list-registered    List snap names registered or shared with you.
  list-revisions     Get the history on the store for <snap-name>.
  login              Login with your Ubuntu One e-mail address and password.
  logout             Clear session credentials.
  pack               Create a snap from a directory holding a valid snap.
  prime              Final copy and preparation for the snap.
  pull               Download or retrieve artifacts defined for a part.
  push               Push <snap-file> to the store.
  push-metadata      Push metadata from <snap-file> to the store.
  register           Register <snap-name> with the store.
  register-key       Register a key with the store to sign assertions.
  release            Release <snap-name> on <revision> to the selected
                     store...
  sign-build         Sign a built snap file and assert it using the...
  snap               Create a snap.
  stage              Stage the part's built artifacts into the common...
  status             Get the status on the store for <snap-name>.
  try                Try a snap on the host, priming if necessary.
  validate           Validate a gated snap.
  version            Obtain snapcraft's version number.
  whoami             Returns your login information relevant to the store.
</pre>

如果您看到 Snapcraft 工具的上述帮助输出,您应该已经准备就绪!

[root@host ~]# snapcraft --version
snapcraft, version 3.8
[root@host ~]# 

Ubuntu 18.04 LTS

第 1 步:验证 snapd 是否可用

作为最佳实践,更新您的软件包:

apt-get update -y

安装了 snapd 的 Ubuntu 发行版 16.04 及更高版本。 验证您的 Ubuntu 发行版是否可用 snapd。 您的特定版本信息可能会有所不同,但输出应类似于以下内容:

root@host:~# snap --version
snap    2.38+18.04
snapd   2.38+18.04
series  16
ubuntu  18.04
kernel  4.15.0-50-generic
root@newmaster:~# 

如果是这样,你就准备好了! 继续下面的第二步。

如果您看到与此类似的输出:

Command' snap' not found, but can be installed with:
apt install snapd

您需要使用 apt 安装 snapd:

注意:运行“snap install snapcraft”时,如果您收到错误:“错误:此版本的 snap “snapcraft” 是使用经典限制发布的,因此可能会在 snaps 通常受限的安全沙箱之外执行任意系统更改,其中可能会使您的系统面临风险。 如果您理解并想要继续重复该命令,包括 –classic。” 只需运行“apt install snapcraft”命令而不是“snap install snapcraft”命令。 Snapcraft 自己关于安装 snapcraft 的说明使用经典限制。

apt-get install snapd -y

第 2 步:安装 Snapcraft

现在您已经验证 snapd 可用,您可以安装 Snapcraft:

root@host:~# snap install snapcraft --classic
2019-10-10T11:31:12-04:00 INFO Waiting for restart...
snapcraft 3.8 from Canonical✓ installed
root@host:~#

要验证 Snapcraft 是否已安装,请检查帮助输出。 以下命令将输出 Snapcraft 的所有可用选项。

root@host:~$ snapcraft --help
Usage: snapcraft [OPTIONS] COMMAND [ARGS]...

  Snapcraft is a delightful packaging tool.

Options:
  --version    Show the version and exit.
  -d, --debug
  --help       Show this message and exit.

Commands:
  build             Build artifacts defined for a part.
  clean             Remove content - cleans downloads, builds or install...
  cleanbuild        Create a snap using a clean environment managed by a...
  close             Close <channel> for <snap-name>.
  create-key        Create a key to sign assertions.
  define            Shows the definition for the cloud part.
  enable-ci         Enable continuous-integration systems to build and...
  expand-extensions Display snapcraft.yaml with all extensions applied.
  export-login      Save login configuration for a store account in FILE.
  extension         Show contents of extension.
  gated             Get the list of snaps and revisions gating a snap.
  help              Obtain help for a certain topic, plugin or command.
  init              Initialize a snapcraft project.
  list-extensions   List available extensions.
  list-keys         List the keys available to sign assertions.
  list-plugins      List the available plugins that handle different types...
  list-registered   List snap names registered or shared with you.
  list-revisions    Get the history on the store for <snap-name>.
  login             Login with your Ubuntu One e-mail address and password.
  logout            Clear session credentials.
  pack              Create a snap from a directory holding a valid snap.
  prime             Final copy and preparation for the snap.
  pull              Download or retrieve artifacts defined for a part.
  push              Push <snap-file> to the store.
  push-metadata     Push metadata from <snap-file> to the store.
  register          Register <snap-name> with the store.
  register-key      Register a key with the store to sign assertions.
  release           Release <snap-name> on <revision> to the selected
                    store...
  search            Searches the remote parts cache for matching parts.
  sign-build        Sign a built snap file and assert it using the...
  snap              Create a snap.
  stage             Stage the part's built artifacts into the common...
  status            Get the status on the store for <snap-name>.
  update            Updates the parts listing from the cloud.
  validate          Validate a gated snap.
  version           Obtain snapcraft's version number.
  whoami            Returns your login information relevant to the store.
root@host:~$ 

就是这样! 现在,让我们快速获取信息然后安装它;

[root@host ~]# snap info hello-world
name:      hello-world
summary:   The 'hello-world' of snaps
publisher: Canonical✓
contact:   [email protected]
license:   unset
description: |
  This is a simple hello world example.
commands:
  - hello-world.env
  - hello-world.evil
  - hello-world
  - hello-world.sh
snap-id:      buPKUD3TKqCOgLEjjHx5kSiCpIs5cMuQ
tracking:     stable
refresh-date: today at 11:40 EDT
channels:
  stable:    6.4 2019-04-17 (29) 20kB -
  candidate: 6.4 2019-04-17 (29) 20kB -
  beta:      6.4 2019-04-17 (29) 20kB -
  edge:      6.4 2019-04-17 (29) 20kB -
installed:   6.4            (29) 20kB -
[root@host ~]# 

以下是使用 snap 安装应用程序的方法:

[root@host ~]# snap install hello-world
hello-world 6.4 from Canonical✓ installed

[root@host ~]# hello-world
Hello World!
[root@host ~]#

今天就开始吧!

还有问题吗? 我们的支持团队与对网络托管技术有深入了解的人才支持,尤其是本文中讨论的那些。 如果您在执行上述步骤时感到不舒服,我们随时为您提供帮助!