Skip to Content

How do I use snap command in Linux?

How do I use snap command in Linux?

The snap command is a powerful tool in Linux that allows you to easily install, configure, and manage snap packages. Snap packages are self-contained software bundles that include all the necessary dependencies and configurations for an application to run. In this comprehensive guide, we will cover everything you need to know about using the snap command in Linux.

What are Snaps and How are They Different from Apt?

Snaps are app packages that work natively across many Linux distributions. They differ from traditional Linux packages managed by apt like .deb files in a few key ways:

  • Self-contained – Snaps contain all the dependencies and libraries an app needs to run, avoiding “dependency hell.”
  • Secure – Each snap runs isolated in a secure, sandboxed environment.
  • Auto-updating – Snaps auto-update in the background for easy maintenance.
  • Cross-distribution – Snaps work consistently across many popular Linux distributions like Ubuntu, Fedora, Arch, etc.

The snap system offers a simpler, more consistent way to install and manage applications across Linux compared to traditional distro-specific packages. Snap was created by Canonical, the company behind Ubuntu.

Installing Snap on Linux

Most major Linux distributions now have native support for snaps pre-installed. But if snap is not already on your system, installing it is very easy.

On Ubuntu or other Debian-based distros, you can install snapd (the background service that runs snaps) with:

sudo apt install snapd

On Fedora or other RPM-based distros, use:

sudo dnf install snapd

On Arch Linux or Manjaro, snapd is available in the Community repository:

sudo pacman -S snapd

Once snapd is installed, run:

sudo systemctl enable --now snapd

This enables and starts the snapd service, allowing you to run snaps.

Finding Snaps

With snap installed, it’s easy to find snaps packaged for almost any application you want to install. Some sources for snaps include:

  • The default Snap Store – search here at https://snapcraft.io/store
  • Your distro’s software center may include featured snaps
  • The command line – search with `snap find [app]`

For example, to find available snaps for Visual Studio Code:

snap find visual studio code

This will display all available VS Code snaps and versions to choose from.

Installing Snaps

Once you’ve found the snap you want, installing it is very straightforward with the snap install command. For example, to install Visual Studio Code:

sudo snap install code --classic

The --classic flag here allows VS Code to integrate with your desktop and theme. Omit this if you do not need that integration.

To install the latest cutting-edge build of VS Code instead of the stable release:

sudo snap install code --edge --classic

Snaps will be installed to /var/lib/snapd/snap on your system. You can then launch GUI snaps via your distro’s application menu or start any snap from the command line.

Some things to note about installing snaps:

  • You need sudo privileges to install snaps system-wide.
  • Use --classic when needed for desktop integration.
  • Add --edge to install cutting-edge releases instead of stable.

Updating and Removing Snaps

Snaps make updating applications a breeze. To update all installed snaps to their latest versions, simply run:

sudo snap refresh

Or to update a specific snap:

  
sudo snap refresh [snap name]

Snaps will auto-refresh in the background on a regular basis. But this allows you to manually check for updates.

To remove an installed snap:

sudo snap remove [snap name]

This will completely uninstall and remove that particular snap from your system.

Managing Snaps

There are a few handy commands for managing your installed snaps:

snap list

This lists all installed snaps on your system.

  
snap info [snap name]

This shows detailed information about the specified snap, such as the version, publisher, etc.

snap changes

This will list recent changes to snaps on your system, like updates or removals.

Configuring Snaps

Many snaps allow you to configure options and parameters to customize the application. This can be done via the snap set command.

For example, to configure the snap for VLC media player you might run:

sudo snap set vlc media-dirs=$HOME/Videos:$HOME/Music

This sets VLC’s media directories to your Videos and Music folders for access to media files. Any configuration options defined by the snap author can be modified in this way.

Run snap set --help to learn more about modifying snap configurations.

Building Your Own Snaps

In addition to installing snaps from the store, you can also build your own snaps for distribution. This involves bundling your application source code along with any required libraries and assets into a .snap file.

The main steps for building a snap are:

  1. Write a snapcraft.yaml file configuring your app’s metadata, binaries, dependencies, etc.
  2. Run snapcraft in the directory with snapcraft.yaml to build the .snap file.
  3. Release your snap to the Snap Store for others to install.

The snapcraft tool handles pulling down dependencies and assets and packaging everything into a snap. Various lifecycle hooks are also available to customize the packaging process.

Distributing your own snaps gives you more control over publishing updates and managing versions. And it allows your app to be installed on any Linux distro supporting snaps with no system dependencies to worry about.

Using Snap on Servers

In addition to desktop applications, snaps can also be very useful for deploying server software. Benefits include:

  • Rapidly deploying LAMP, MEAN, and other stacks as snaps
  • Sandboxing for security isolates services
  • Avoid version conflicts between software
  • Easily install supporting tools like databases
  • Updates in the background instead of manual package management

Some examples of server apps published as snaps include Apache, MongoDB, Redis, MySQL, and hundreds more. By snap installing stacks like LAMP, you can avoid the hassle of installing each component individually.

For servers, be sure to configure snapd to use classic mode for full access to the filesystem and device interfaces. And watch out for network port conflicts between isolated snaps.

Pros and Cons of Snaps

Here is a quick rundown of some of the advantages and potential disadvantages of using snap packages:

Pros

  • Works across Linux distributions – Snaps provide distro-agnostic package management.
  • Automatic updates in the background.
  • Isolation improves security and avoids dependency conflicts.
  • All app dependencies bundled into the snap.
  • Easy to install for end users and developers.

Cons

  • Larger file sizes since dependencies are included.
  • Startup performance can be slower.
  • Background update model less transparent than apt.
  • Limited control over some system settings.

Overall snaps provide significant advantages for app distribution, security, and simplified dependency management. But traditional native packages still have a place for some system-level apps.

Snap Command Reference

Here is a quick reference guide to the main snap commands for installing, managing, and developing snaps:

Snap Installation Commands

  • snap install [snap] – Install snap package
  • snap install --classic [snap] – Install with classic confinement
  • snap install --edge [snap] – Install edge release channel
  • snap remove [snap] – Remove/uninstall snap
  • snap refresh – Refresh all snaps to latest version
  • snap refresh [snap] – Refresh specific snap
  • snap changes – List recent snap changes

Snap Management Commands

  • snap list – List installed snaps
  • snap info [snap] – Get info about snap
  • snap connections [snap] – List interface connections
  • snap services [snap] – Control snap services
  • snap aliases [snap] – Manage command aliases
  • snap set [snap] [key]=[value] – Change snap config

Snap Development Commands

  • snapcraft init – Initialize a snap directory
  • snapcraft – Build a snap from source
  • snapcraft status – Check build status
  • snapcraft login – Login to Snapcraft
  • snapcraft push – Push/release a snap
  • snapcraft logout – Logout of Snapcraft

Conclusion

Learning to use the snap command opens up powerful new options for installing, running, and distributing applications across Linux. With robust cross-distribution support, security isolation, automatic updates, and simplified dependency management, snaps are becoming a new standard for Linux app packaging and delivery.

In this guide we covered snap package basics, how to install and manage snaps, key configuration options, building your own snaps, pros and cons to be aware of, and a snap command reference cheat sheet. You should now have a firm understanding of how to leverage snaps for your needs on desktops, servers, and IoT devices.

To learn more about developing with snaps, check out the official snapcraft documentation at https://snapcraft.io/docs.