What a Package Manager Does and Why Lock Files Exist
Almost no modern project is written from scratch. Software relies on libraries written by others: code for dates, web requests, testing, compression and thousands of other jobs. A package manager is the tool that finds those libraries, downloads them and keeps track of which ones your project uses. You meet it every time you run commands such as pip install, npm install, cargo add or apt install.
This article explains what a package manager does behind the scenes, why version numbers matter, and the purpose of the lock file that appears next to your project files.
The jobs of a package manager
At a basic level, a package manager does five things.
- Finds packages. It searches a registry, which is an online index of published packages.
- Downloads them. It fetches the files for a specific version.
- Resolves dependencies. Packages need other packages, which need others in turn, and the manager works out the complete set.
- Installs them. It places the files where your language or system can find them.
- Records what you chose. It writes the result into a manifest file in your project.
Different ecosystems have their own managers, but the model is consistent. System package managers such as apt install programs for the whole machine, while language package managers such as pip and npm install libraries for a project.
The manifest: what you asked for
The manifest file lists the dependencies you want directly. Examples include package.json for Node, pyproject.toml or requirements.txt for Python and Cargo.toml for Rust. It describes your intent, such as "I need a library for parsing dates."
Rather than naming one exact version, manifests usually allow a range. In many ecosystems, a symbol such as ^1.4.0 means "any compatible release from 1.4.0 up to, but not including, 2.0.0." Ranges are convenient, since bug fixes arrive automatically, but they bring uncertainty about exactly what will be installed.
Semantic versioning in brief
Many ecosystems follow semantic versioning, written as three numbers: major, minor and patch, such as 2.7.3.
- A patch change fixes bugs without altering behaviour.
- A minor change adds features in a backward compatible way.
- A major change may break existing code.
This is a convention, not a law. Authors sometimes make mistakes, and a minor update can still break something. That gap between the promise and the reality is one reason lock files exist.
The dependency tree
Your project depends on a few libraries directly, but each of those depends on others. These are called transitive dependencies, and they can easily outnumber the direct ones many times over. You never wrote them into your manifest, yet they run in your program.
The manager must pick a version of each package that satisfies everyone who needs it. Sometimes two libraries ask for conflicting versions of a third. Different managers handle that in different ways: some install both versions side by side, and others report an error and ask you to resolve it.
The problem: same manifest, different results
Imagine you install your dependencies today and everything works. A colleague installs them next month. In the meantime, a library you rely on indirectly released a new patch version. The manifest ranges still allow it, so your colleague receives different code from yours. A bug appears on one machine and not the other, and nobody changed anything in the project.
The same thing happens between your laptop, a test server and production, or between a build that ran last week and one that runs today. Repeatability is lost, and debugging becomes guesswork.
What a lock file is
A lock file records the exact version of every package that was installed, including all transitive ones, and often a checksum for each file. Common names are package-lock.json, yarn.lock, poetry.lock, Pipfile.lock, Cargo.lock and Gemfile.lock.
When a lock file exists, the manager installs exactly what it lists rather than resolving from the ranges again. Everyone who installs from the same lock file gets the same code. The manifest says what you want, and the lock file says what you got.
Why checksums matter
Many lock files include a hash of each package's contents. When the manager downloads a file, it compares the hash. If the file differs from what was recorded, the install fails. This protects against accidental changes and against some kinds of tampering, since the registry cannot quietly swap in different contents under the same version number.
Working with lock files
- Commit the lock file to version control for applications, so every developer, test run and deployment uses the same set.
- Libraries that others will install are a different case. Many library authors do not rely on a lock file, because consumers resolve their own versions. Follow the convention of your ecosystem.
- Update on purpose. Use the manager's update command to move to newer versions, review the changes, run your tests and commit the new lock file together.
- Do not edit it by hand. Let the tool generate and rewrite it.
- Resolve merge conflicts by regenerating the file with the tool after merging the manifest, not by choosing lines manually.
Related habits that help
Use isolated environments, such as Python virtual environments, so projects do not share installed packages. Check for known security problems in dependencies with the audit command your manager provides. Remove packages you no longer use, since each one is extra code to maintain and trust.
Frequently Asked Questions
Should I commit the lock file to Git?
For applications and services, yes. It makes builds repeatable. For reusable libraries, follow the usual practice of your language community, since some ecosystems prefer not to commit one.
What is the difference between install and update?
An install uses the lock file when one is present, giving you the recorded versions. An update looks for newer versions that fit your manifest ranges, installs them and rewrites the lock file.
Why is my dependency folder so large?
Because it contains your direct dependencies plus everything they depend on, sometimes through many layers. Auditing and removing unused packages helps, and so does choosing smaller libraries when you can.