How do you add an SPDX licence header to a source file?
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.
Cite
CodeLifter 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.- CodeLifter editorsStaffStaff, 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: MITC header (kernel style) /* SPDX-License-Identifier: MIT */Python, shell, Ruby, YAML, TOML # SPDX-License-Identifier: MITCSS /* SPDX-License-Identifier: MIT */HTML, XML, SVG <!-- SPDX-License-Identifier: MIT -->reStructuredText .. SPDX-License-Identifier: MITFor 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.licensefile next to them under the REUSE specification 3.3, for examplelogo.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 ORSPDX-License-Identifier: GPL-2.0-only OR MITBoth licences apply at once ANDSPDX-License-Identifier: LGPL-2.1-only AND BSD-2-ClauseA licence plus a named exception WITHSPDX-License-Identifier: GPL-2.0-or-later WITH Bison-exception-2.2Use the current identifiers with
-onlyor-or-laterfor GNU licences. You will still see the olderGPL-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 customLicenseRef-identifier, such asLicenseRef-my-special-license, as long as you provide its text.Steps to add headers to a project
- Pick the licence and copy its exact identifier from the SPDX License List. Case and punctuation matter:
Apache-2.0, notApache 2. - Put the full licence text in the repository: a
LICENSEfile, orLICENSES/MIT.txtif you follow REUSE. - Add a copyright line above the identifier if you want one. REUSE uses
SPDX-FileCopyrightText: 2026 Your Name. - Add the header to each file by hand, or let a tool do it. The REUSE tool's
reuse annotatecommand (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. - Run
reuse lintfrom the same tool. It reports files without copyright and licence information, identifiers with no matching text inLICENSES/, invalid expressions and identifiers SPDX has deprecated; add--linesfor one line per problem. - 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-SnippetBeginandSPDX-SnippetEndmarkers 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.jsonlicensefield 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,ANDorWITHon one line. - Keep the full licence text in
LICENSEorLICENSES/, and use.licensefiles for files that cannot hold comments. reuse annotateadds headers in bulk andreuse lintfinds gaps, but check what they wrote.
- Pick the licence and copy its exact identifier from the SPDX License List. Case and punctuation matter:
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
1 answer
- 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 asLICENSES/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 flatLICENSES/directory at the project root. If your project has a single licence, a plainLICENSEfile at the root does the job Annex E describes. If it has several, theLICENSES/folder keeps them unambiguous, and the REUSE tool'sreuse lintreports any identifier that has no matching text there.