Linux snap Command

Linux 命令大全Linux Command Reference


snap is a software packaging and deployment system developed by Canonical for Linux distributions. It provides a secure and reliable way to install, update, and manage applications.

Core Features of snap

  1. Containerized Applications: Each snap application runs in its own sandbox environment
  2. Automatic Updates: Supports automatic background updates for applications
  3. Cross-distribution: The same snap package can run on multiple Linux distributions
  4. Dependency Inclusion: All dependencies are packaged in the snap, avoiding system dependency conflicts

Differences Between snap and Traditional Package Management

Feature snap Traditional Package Management (apt/yum)
Dependency Management Self-contained System-level sharing
Update Mechanism Automatic rolling updates Manual updates required
Isolation Strong isolation Weak isolation
Cross-distribution support Supported Not supported
Rollback capability Supported Limited support

Basic Syntax of snap Command

The basic syntax structure of the snap command is as follows:

snap [命令] [选项] [包名]

Overview of Common Commands

  • install- Install snap package
  • remove- Remove snap package
  • refresh- Update snap package
  • list- List installed snap packages
  • find- Search available snap packages
  • info- Display snap package information
  • revert- Roll back to a previous version
  • disable/enable- Disable/Enable snap package
  • services- Manage snap services

Detailed Usage of snap Command

Install snap Package

sudo snap install <package-name>

Common options:

  • --channel: Specify the installation channel (e.g., stable, candidate, beta, edge)
  • --devmode: Install in developer mode (reduces security restrictions)
  • --classic: Install in classic mode (relaxes restrictions)

Example:

sudo snap install code --classic  # 安装 VS Code

List Installed snap Packages

snap list

Example output:

Name    Version    Rev    Tracking       Publisher    Notes
code    v1.70.2    xxxxx  latest/stable  vscode✓      classic

Search snap Packages

snap find <search-term>

Example:

snap find telegram

Update snap Package

Update a single package:

sudo snap refresh <package-name>

Update all packages:

sudo snap refresh

Remove snap Package

sudo snap remove <package-name>

View snap Package Information

snap info <package-name>

The output includes detailed information such as version, channel, and description.


Advanced snap Features

Channel Management

snap provides multiple release channels:

Example

graph LR
    A[Edge] --> B[Beta]
    B --> C[Candidate]
    C --> D[Stable]

Command to switch channels:

sudo snap refresh <package-name> --channel=<channel-name>

Version Rollback

If there are problems with a new version, you can roll back to a previous version:

sudo snap revert <package-name>

Service Management

For snap packages that provide services:

View service status:

snap services

Start/Stop services:

Example

sudo snap start
sudo snap stop

Practical Application Examples

Example 1: Install and Run Nextcloud

Example

# Install Nextcloud
sudo snap install nextcloud

# Configure administrator account
sudo nextcloud.manual-install  

# Get access address
sudo nextcloud.occ config:system:get overwrite.cli.url

Example 2: Install Development Toolchain

Example

# Install Go language
sudo snap install go --classic

# Install Node.js
sudo snap install node --channel=14/stable --classic

# Install Docker
sudo snap install docker

Common Problem Solutions

Problem 1: snap Applications Start Slowly

Solution:

Example

# Preload snap core components
sudo snap install core
sudo snap install snapd

# Restart snap service
sudo systemctl restart snapd

Problem 2: Permission Errors

For applications that need access to system resources, use:

Example

# Connect the required interface
sudo snap connect : :

# For example, allow VLC to access removable devices
sudo snap connect vlc:removable-media

Problem 3: Disk Space Usage

Clean up old versions:

Example

# Set the number of versions to keep (default is 3)
sudo snap set system refresh.retain=2

# Manually clean up
sudo snap system prune

Best Practice Recommendations

  1. Prioritize using the stable channel: For production environments--channel=stable
  2. Regularly clean up old versions: Prevent excessive disk space usage
  3. Use interfaces reasonably: Only grant necessary system permissions
  4. Monitor automatic updates: For important services, consider manually controlling update timing
  5. Combine with traditional package management: For critical system components, it is still recommended to use the distribution's official sources

By mastering the snap command, you can manage applications on Linux more efficiently and enjoy the convenience and security brought by containerization.


Linux 命令大全Linux Command Reference

Other Extensions