Linux snap Command
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
- Containerized Applications: Each snap application runs in its own sandbox environment
- Automatic Updates: Supports automatic background updates for applications
- Cross-distribution: The same snap package can run on multiple Linux distributions
- 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 packageremove- Remove snap packagerefresh- Update snap packagelist- List installed snap packagesfind- Search available snap packagesinfo- Display snap package informationrevert- Roll back to a previous versiondisable/enable- Disable/Enable snap packageservices- 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
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 stop
Practical Application Examples
Example 1: Install and Run Nextcloud
Example
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
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
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
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
sudo snap set system refresh.retain=2
# Manually clean up
sudo snap system prune
Best Practice Recommendations
- Prioritize using the stable channel: For production environments
--channel=stable - Regularly clean up old versions: Prevent excessive disk space usage
- Use interfaces reasonably: Only grant necessary system permissions
- Monitor automatic updates: For important services, consider manually controlling update timing
- 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.
Other Extensions
Linux Command Reference