Introduction
The license is the most important file in an open-source project, because it is the permission. Copyright law gives the author of a piece of code all rights by default. A license is the author saying: "you may do these things, if you follow these rules." Without one, you may look at the code on the screen, but you may not copy, change or share it.
This lesson is not legal advice, but every programmer should know the main families of licenses and what they ask of you, because the licenses of the libraries you use become part of your own project.
How it works
Licenses fall into two families.
Permissive licenses ask for little. The MIT and BSD licenses let you do almost anything with the code, including using it in closed, paid software, as long as you keep the copyright notice. Apache 2.0 is similar and adds a grant of patent rights.
Copyleft licenses ask you to give back. The GPL says that if you share a changed version, or a program that includes GPL code, you must share its source under the GPL too. The AGPL goes one step further and counts running the program as a service for others as sharing. A weaker form, the LGPL, applies the rule only to the library itself.
For text, pictures and lessons, there are Creative Commons licenses. CC BY-SA means: credit the authors and share changes under the same license. StepVioz's code is under MIT, and its lessons under CC BY-SA 4.0.
A license is written into a LICENSE file, and often into each source file as one line with an SPDX identifier, for example SPDX-License-Identifier: MIT. Tools read that line to list the licenses in a project.
Every library you import is a dependency, and its license applies to you. A project that is MIT can use MIT, BSD and Apache code freely. Using GPL code in it forces a choice: share your own source under the GPL, or leave the library out. A library without any license cannot be used at all.
Example
# A tiny license checker: which dependencies can a project use?
PERMISSIVE = {"MIT", "BSD-3-Clause", "Apache-2.0"}
COPYLEFT = {"GPL-3.0", "AGPL-3.0"}
dependencies = [
("requests", "Apache-2.0"),
("colorama", "BSD-3-Clause"),
("fastgrid", "GPL-3.0"),
("mystery-lib", "NONE"),
]
def kind(license_id):
if license_id in PERMISSIVE:
return "permissive"
if license_id in COPYLEFT:
return "copyleft"
return "unknown"
problems = 0
for name, license_id in dependencies:
k = kind(license_id)
if k == "permissive":
verdict = "ok"
elif k == "copyleft":
verdict = "check: you must share your source too"
problems += 1
else:
verdict = "no license: do not use"
problems += 1
print(f"{name:12} {license_id:13} {k:11} {verdict}")
print(problems, "dependency(ies) need attention")Line by line
- The two sets name the identifiers the program knows.
kind()turns an identifier intopermissive,copyleftorunknown.- The loop prints one line per dependency and counts those that need a human decision: copyleft needs a check, and an unknown or missing license is a stop.
Expected result
requests Apache-2.0 permissive ok
colorama BSD-3-Clause permissive ok
fastgrid GPL-3.0 copyleft check: you must share your source too
mystery-lib NONE unknown no license: do not use
2 dependency(ies) need attention
In the real world
Companies run exactly this kind of check before they ship software: tools list every dependency, read its license and raise an alarm on one the company policy does not allow. A project that is picking its own license usually starts from a question: do I want anyone to use this freely (MIT), or do I want improvements to come back (GPL)? Sites such as choosealicense.com help you decide.
Common mistakes
- Using code that has no license because it is on a public site. Public is not the same as permitted.
- Removing the copyright notice from MIT or BSD code. That notice is the one thing those licenses require.
- Mixing licenses without checking. Adding a GPL library to a project you want to keep under MIT creates a conflict.
Best practices
- Add a
LICENSEfile to every project on the first day, and choose a standard license rather than writing your own. - Put an SPDX line in each source file if the project asks for it.
- Check the license of a library before you add it, not after you ship.
Summary
A license is the permission that makes code open source. Permissive licenses such as MIT, BSD and Apache ask only for credit, while copyleft licenses such as the GPL ask you to share changed versions under the same license. Code without a license cannot be reused, and the license of every dependency becomes part of your own project, so check it early.
Quick check
Key takeaways
- No license means all rights reserved: you may read the code but not copy or change it.
- Permissive licenses ask for little; copyleft licenses ask you to share your changes under the same license.
- Every dependency brings its license into your project, so check it before you add one.
- A license is a legal text; for a real decision, read it or ask a lawyer.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.