Skip to content

Guide · Open source

How to use SwiftLint: install, set up and run Swift style checks

Updated · written with AI from the project's own documentation and checked against our catalogue data

SwiftLint is an open-source tool, licensed under MIT, that reads Swift source code and reports places where it breaks widely accepted style and convention rules. It can also fix some of those issues automatically when you ask it to. This guide shows you how to install it, set it up for a project and run it from the command line or inside Xcode.

SwiftLintrealm/SwiftLint · ★ 19.8k · MIT

Quick start

  1. 1Pick an install route. Choose Homebrew, Mint, Swift Package Manager, CocoaPods or another route from the README based on how your project is set up.
  2. 2Install SwiftLint. Run brew install swiftlint for Homebrew, or mint install realm/SwiftLint for Mint.
  3. 3Add the plugin to your package or project. For Swift packages, add the SwiftLintPlugins dependency and attach the build tool plugin to each target. For Xcode, add the package by URL and the plugin under Run Build Tool Plug-ins.
  4. 4Trust the plugin. When Xcode prompts you the first time, trust and enable the plugin.
  5. 5Run the linter. Run swift package plugin swiftlint from the command line, or build the project to see violations in Xcode.
  6. 6Make sure the code compiles first. Fix build errors before linting, especially before using --fix or --autocorrect.

What SwiftLint checks and who it is for

SwiftLint enforces style guide rules that are generally accepted by the Swift community. Its rules are predominantly based on SwiftSyntax, and some rules hook into Clang and SourceKit to access type information.

It suits iOS and macOS developers who want consistent Swift style across a team, teams that want style checks to run automatically in builds or Xcode, and Swift package authors who want linting through Swift Package Manager plugins.

Install SwiftLint with a package manager

Choose the install route that fits your setup. The README lists Swift Package Manager, Xcode Package Dependency, Homebrew, CocoaPods, Mint, Bazel, a pre-built package and building from source.

For Homebrew, the README gives this command. For Mint, use the second command.

  • Homebrew: installs the latest release.
  • CocoaPods: lets you pin to a specific version rather than the latest.
  • Swift Package Manager: required if you want to use the command or build tool plugins.
brew install swiftlint

Install with Mint

If you already use Mint to manage command line tools, install SwiftLint with the command below.

mint install realm/SwiftLint

Set up SwiftLint in a Swift package or Xcode project

For Swift packages, add the SwiftLintPlugins dependency to Package.swift. The README recommends consuming the plugins from the dedicated SwiftLintPlugins repository rather than directly from the SwiftLint repository, and it says this document assumes you rely on SwiftLintPlugins.

Build tool plugins run when each target builds. If your project has several targets, add the plugin to each target you want linted. For Xcode projects, add the SwiftLintBuildToolPlugin to the Run Build Tool Plug-ins phase of the target's Build Phases.

The plugin finds the SwiftLint working directory by locating the topmost config file in the package or project directory. Avoid project structures that require the --config option.

  • Swift package: add the plugin to the target's plugins list.
  • Xcode: add the Swift package by its URL, then add the plugin under Run Build Tool Plug-ins.
  • First use: trust and enable the plugin when Xcode prompts you.
.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLintPlugins")

Run SwiftLint from the command line

With a Swift package, you can run the command plugin from the terminal. The README shows this command for that purpose.

Only lint code that compiles. SwiftLint is designed to analyze valid, compilable source, and non-compiling code can lead to unexpected and confusing results, especially with --fix or --autocorrect.

swift package plugin swiftlint

Avoid running SwiftLint on code that does not compile

It may seem sensible to run SwiftLint before compiling so a build exits early on violations. The README advises against relying on this, because SwiftLint expects compilable source. Build the project first, then lint it, and be especially careful before using automatic fixes on code that fails to build.

What people use it for

  • Keep Swift style consistent across a team without relying only on manual code review.
  • Surface style violations in the Xcode editor during builds.
  • Lint a Swift package automatically through its build tool plugin.
  • Run an on-demand lint from the command line using the Swift Package command plugin.

Frequently asked questions

How do I install SwiftLint?

The README gives two direct commands: brew install swiftlint for Homebrew and mint install realm/SwiftLint for Mint. You can also add it through Swift Package Manager, CocoaPods, Bazel, a pre-built package or by building from source.

Is SwiftLint free?

SwiftLint is open source and released under the MIT licence, as stated in the project's details. Check the repository's licence file for the full terms.

What do I need to run SwiftLint?

You need a Swift toolchain or Xcode for building or using the plugins, plus one of Homebrew, CocoaPods, Mint or Swift Package Manager, depending on your chosen route. For building from source, the README also requires Bazel and a recent Swift toolchain on your PATH.

Why does SwiftLint say my code has errors?

SwiftLint is designed for valid, compilable Swift source. Non-compiling code can produce unexpected or confusing results, so fix build errors before you lint.

Can SwiftLint fix my code automatically?

Yes, for some issues. The README mentions the --fix and --autocorrect command line arguments, and warns that they work best on code that compiles.

How does SwiftLint compare to swift-format or SwiftFormat?

SwiftLint focuses on reporting style and convention violations, and it can auto-fix some of them. The project lists swift-format, SwiftFormat and the Swift Style Guide as comparable projects, so compare their features against your needs before choosing.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

SwiftLint suits iOS and macOS teams and Swift package authors who want style checks to run automatically in builds or Xcode. It is a poor fit if you only want to run Swift code and have no interest in style rules, or if your code does not compile yet, since the README warns about confusing results in that case.

Install options & downloads