Skip to content
CodeLifter
Search/ Browse prompts
CodeLifter Forum

How do you add an SPDX licence header to a source file?

Staff answerWritten by CodeLifter editors · Code provenance1 replyupdated

Short answer

Add one comment line at or near the top of the file reading SPDX-License-Identifier: followed by the licence's exact identifier from the SPDX License List, for example SPDX-License-Identifier: MIT. Use the file's own comment syntax, keep the expression on a single line, and keep the full licence text in the repository.

Written by CodeLifter editors · Staff, CodeLifter ·
CiteCodeLifter Forum, "How do you add an SPDX licence header to a source file?", https://codelifter.com/forum/posts/how-do-you-add-an-spdx-licence-header-to-a-source-file/, accessed 2026-11-26.
  1. Staff, CodeLifter ·

    What is an SPDX licence identifier?

    SPDX (Software Package Data Exchange) is a Linux Foundation specification for describing software, and the SPDX License List gives each common licence a short, fixed identifier: MIT, Apache-2.0, GPL-3.0-or-later, CC0-1.0. The list was at version 3.29.0 (16 September 2026) when the CodeLifter editors last checked it.

    Annex E of the SPDX specification explains why a one-line tag beats a pasted paragraph of licence text. Among its reasons: it is precise, "easy and more reliable to machine process", and "The license information travels with the file (as sometimes not entire projects are used or license files are removed)." That last point matters for code that gets copied around, which is most code an assistant has ever seen.

    The Linux kernel requires the tag in every file. Its licensing rules say boilerplate licence paragraphs are "hard to validate for tools" because of "formatting, typos etc.", and that "The Linux kernel requires the precise SPDX identifier in all source files."

    Where exactly does the line go?

    Annex E says the tag "should be placed at or near the top of the file in a comment" and "should appear on its own line". The kernel's rules are stricter and make a good default: the first line that can hold a comment. For scripts that start with a shebang (#!/usr/bin/env python3), the shebang must stay on line 1, so the tag goes on line 2.

    What does the header look like in each language?

    Use whatever comment the language accepts. The kernel lists its own styles for C, headers, assembly, scripts and reStructuredText; the rest follow each language's normal comment syntax.

    File type Header line
    JavaScript, TypeScript, C, Go, Rust, Java // SPDX-License-Identifier: MIT
    C header (kernel style) /* SPDX-License-Identifier: MIT */
    Python, shell, Ruby, YAML, TOML # SPDX-License-Identifier: MIT
    CSS /* SPDX-License-Identifier: MIT */
    HTML, XML, SVG <!-- SPDX-License-Identifier: MIT -->
    reStructuredText .. SPDX-License-Identifier: MIT

    For XML files, an XML declaration such as <?xml version="1.0"?> stays first, like a shebang. Files that cannot hold a comment at all, such as images or JSON, get a separate .license file next to them under the REUSE specification 3.3, for example logo.png.license.

    How do you write more than one licence?

    Annex E allows an SPDX licence expression, with three operators. The expression "MUST be on a single line".

    Situation Operator Example
    You may choose either licence OR SPDX-License-Identifier: GPL-2.0-only OR MIT
    Both licences apply at once AND SPDX-License-Identifier: LGPL-2.1-only AND BSD-2-Clause
    A licence plus a named exception WITH SPDX-License-Identifier: GPL-2.0-or-later WITH Bison-exception-2.2

    Use the current identifiers with -only or -or-later for GNU licences. You will still see the older GPL-2.0+ form in the kernel and in Annex E, where a trailing + means "or later". If your licence is not on the list, Annex E allows a custom LicenseRef- identifier, such as LicenseRef-my-special-license, as long as you provide its text.

    Steps to add headers to a project

    1. Pick the licence and copy its exact identifier from the SPDX License List. Case and punctuation matter: Apache-2.0, not Apache 2.
    2. Put the full licence text in the repository: a LICENSE file, or LICENSES/MIT.txt if you follow REUSE.
    3. Add a copyright line above the identifier if you want one. REUSE uses SPDX-FileCopyrightText: 2026 Your Name.
    4. Add the header to each file by hand, or let a tool do it. The REUSE tool's reuse annotate command (version 6.2.0 documentation) does this: reuse annotate --copyright="Your Name" --license=MIT src/app.js. It detects the comment style from the file extension, keeps shebangs and XML declarations on top, and can work through a folder with --recursive.
    5. Run reuse lint from the same tool. It reports files without copyright and licence information, identifiers with no matching text in LICENSES/, invalid expressions and identifiers SPDX has deprecated; add --lines for one line per problem.
    6. Read the result. The REUSE documentation itself warns: "you should make sure that the information it adds is actually reflective of reality. This is best verified manually."

    What about code an assistant wrote, or code you pasted in?

    The header states the licence you are offering the file under. It does not prove you hold the rights to everything in it. Whether you can license purely generated code at all is covered in the MIT licence thread. If a block came from somewhere else under a different licence, the file-level tag is not enough: wrap the block in SPDX-SnippetBegin and SPDX-SnippetEnd markers with its own licence line, as shown in the provenance record thread. The CodeLifter desk article on the provenance record goes through the same fields.

    Package-level licences are a separate record. Your package.json license field and an SBOM describe the package as a whole; the SBOM thread covers that layer.

    Short version

    • Write SPDX-License-Identifier: <identifier> in a comment at or near the top of each file; line 2 if line 1 is a shebang.
    • Copy the identifier exactly from the SPDX License List; combine licences with OR, AND or WITH on one line.
    • Keep the full licence text in LICENSE or LICENSES/, and use .license files for files that cannot hold comments.
    • reuse annotate adds headers in bulk and reuse lint finds gaps, but check what they wrote.
    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. CodeLifter editorsStaffStaff, CodeLifter ·

    Do you still need a LICENSE file if every file has an SPDX header? Yes. The header is a pointer, not the licence. Annex E of the SPDX specification says that if you use identifiers in individual files, "it is recommended to reproduce the full license in the projects LICENSE file and indicate that SPDX short identifiers are being used to refer to it."

    Larger projects keep one file per licence. The Linux kernel's licensing rules put each text in a LICENSES/ subdirectory, in a file named after its identifier, such as LICENSES/preferred/MIT, and say "All SPDX license identifiers and exceptions must have a corresponding file in the LICENSES subdirectories" so that tools can verify them. The REUSE specification 3.3 uses the same idea with a flat LICENSES/ directory at the project root. If your project has a single licence, a plain LICENSE file at the root does the job Annex E describes. If it has several, the LICENSES/ folder keeps them unambiguous, and the REUSE tool's reuse lint reports any identifier that has no matching text there.

CodeLifter Forum. Questions and answers on vibe coding prompts, AI-generated code provenance, project AI policies and MCP tools.