How do you install and authenticate the Snyk CLI?
Quick Answer
The Snyk CLI is installed via npm ('npm install -g snyk'), Homebrew ('brew install snyk'), or as a standalone binary. Authentication is done by running 'snyk auth', which opens a browser for you to log in to your Snyk account and generates an API token that the CLI stores locally.
Detailed Answer
Think of installing the Snyk CLI as putting a security scanner on your developer workbench. Just as a mechanic needs a diagnostic tool plugged into the car to read error codes, developers need the Snyk CLI installed locally to scan their projects for vulnerabilities before pushing code. The authentication step is like registering the tool with the manufacturer so it can access the latest database of known issues.
The Snyk CLI can be installed through several methods depending on your operating system and preferences. The most common method is via npm: 'npm install -g snyk' installs it globally so the 'snyk' command is available in any terminal. On macOS, 'brew install snyk' uses Homebrew and keeps the CLI updated through 'brew upgrade snyk.' For environments without Node.js or Homebrew, Snyk provides standalone binaries for Linux, macOS, and Windows that you download directly from GitHub releases. In CI/CD environments, the npm install method is most common, or teams use the official Snyk Docker image (snyk/snyk) which comes with the CLI pre-installed.
Authentication links your CLI to your Snyk account, which determines your organization, project settings, and vulnerability database access. Running 'snyk auth' opens your default web browser to the Snyk login page. After you log in (or sign up), Snyk generates an API token and the CLI stores it in your home directory at ~/.config/configstore/snyk.json. For CI/CD pipelines where a browser is not available, you set the SNYK_TOKEN environment variable to an API token or service account token generated from the Snyk dashboard under Settings > General > API Token. Service account tokens are preferred for automation because they are not tied to a personal account and can be scoped to specific organizations.
In a production setup for the order-processing-service team, the CLI installation is typically scripted into the CI pipeline configuration. A GitHub Actions workflow might include a step that installs Snyk via npm and authenticates using a SNYK_TOKEN stored in repository secrets. The payments-api team might use the Snyk GitHub integration instead, which does not require CLI installation at all because Snyk scans are triggered automatically through webhooks. However, the CLI remains essential for local development, where engineers run 'snyk test' before committing code, and for advanced use cases like scanning container images or IaC files that the Git integration does not cover by default.
A gotcha beginners encounter is token scope. A personal API token gives the CLI access to all organizations your account belongs to, which may be too broad. Service account tokens are scoped to a single organization, following the principle of least privilege. Another common issue is proxy configuration: in corporate environments behind a proxy, you need to set the HTTP_PROXY and HTTPS_PROXY environment variables before running Snyk commands, or the CLI cannot reach the Snyk API to look up vulnerabilities.