To get an app on your phone, you open the Play Store, search for its name and tap Install. The terminal has a typed version of that: a package manager. In the terminal, the app is called a package (a piece of software), and the store where packages are kept is called a registry or repository. You name the package, then the package manager fetches it from the registry, installs it together with whatever that package needs, and keeps a record of everything installed. Finding, downloading, installing, updating and removing software each take only one command.
Two levels: computer and project
There are two kinds of package manager, and you'll use both:
| Level | What it installs | The tool | Stored in |
|---|---|---|---|
| Computer | programs used everywhere: git, node, nginx |
apt (Ubuntu, Debian), brew (macOS) |
system folders, once for every project |
| Project | libraries one project's code uses | npm, pnpm, yarn (JavaScript), composer (PHP), pip (Python) |
the project's own folder |
A library is ready-made code that your project's code calls, for example to write dates or prices.
Install a program on your computer: apt and brew
The commands below install git: the first part on Ubuntu or Debian, the second part on macOS. Text that starts with # is a comment (a note that doesn't run):
# Ubuntu / Debian
sudo apt update # refresh the package list from the server
sudo apt install git # install git
git --version # check: git works now
# macOS, with Homebrew
brew install git
apt(Advanced Package Tool) is the built-in package manager of Ubuntu and Debian.aptwrites to system folders, so it needs admin rights:sudoin front means "run this as admin", and you'll be asked for your password.sudo apt updateonly refreshes the list of available packages, and installs nothing yet. Make a habit of running it beforeinstall. To upgrade everything already installed:sudo apt upgrade.brew(Homebrew) is the most popular package manager on macOS. A new Mac doesn't havebrew: install it once with the command from the brew.sh site.brewdoesn't needsudo.- On a Mac with an Apple chip (M1 or later), Homebrew is installed in
/opt/homebrew. The folder/opt/homebrew/binis not in PATH yet, so the shell cannot findbrew. At the end of the install, the installer shows a part called Next steps. It has commands that add abrew shellenvline to~/.zprofile. That line puts/opt/homebrew/bininto PATH, and zsh reads~/.zprofileevery time you open a new terminal. Run those commands. If you skip them, typingbrewin a new terminal gets the answerzsh: command not found: brew. - The commands follow the same pattern everywhere:
install,uninstall(in apt:remove),upgrade,search. Fedora (another Linux system) hasdnf, Windows haswinget.
Install a library for a project: npm
Example: an online shop project in the shop-project folder needs a library to write prices in dollar format. We'll use an example package called format-price, and when you install it, its newest version is 1.2.0. npm is the package manager for JavaScript projects:
$ npm install format-price
added 1 package, and audited 2 packages in 1s
found 0 vulnerabilities
That one command changes three things in the project folder:
package.jsongets a new line underdependencies(the packages the project needs):"format-price": "^1.2.0". This file is your project's shopping list: which packages, and which versions are allowed.node_modules/format-price/holds the library's code. This folder can be made again at any time withnpm install, so it isn't committed to Git (putnode_modules/in.gitignore, the list of files Git ignores).package-lock.jsonrecords the exact version installed,1.2.0, together with the address it was downloaded from.
The npm commands you'll use most:
npm install # install everything listed in package.json
npm install format-price # add one package
npm install -D vitest # add a package only used while developing (devDependencies)
npm uninstall format-price # remove a package
npm run dev # run the "dev" script from package.json
pnpm and yarn do the same job with slightly different command names: pnpm add format-price, yarn add format-price. PHP has composer require vendor/package, Python has pip install requests.
The ^ and ~ in front of a version
A package version is written MAJOR.MINOR.PATCH, three numbers separated by dots. This rule is called semantic versioning (semver):
- PATCH goes up (1.2.0 to 1.2.5): bug fixes.
- MINOR goes up (1.2.0 to 1.3.0): new features, old code keeps working.
- MAJOR goes up (1.3.0 to 2.0.0): there are changes that can break old code.
Before you go on, guess. Three months after you install format-price, the registry has 1.2.0, 1.2.5, 1.3.0 and 2.0.0. Your teammate clones (copies) your project without package-lock.json, and package.json says "format-price": "^1.2.0". Which version is installed on their laptop?
The mark in front of the version in package.json decides which versions may be installed:
| Written in package.json | Meaning | Allowed | Without a lock file, npm installs |
|---|---|---|---|
^1.2.0 |
1.x.x, at least 1.2.0 | 1.2.0, 1.2.5, 1.3.0 | 1.3.0 |
~1.2.0 |
1.2.x, at least 1.2.0 | 1.2.0, 1.2.5 | 1.2.5 |
1.2.0 |
exactly that | 1.2.0 | 1.2.0 |
^ is what npm writes by default every time you npm install a package. Without a lock file (a file that records exact versions, such as package-lock.json), npm picks the highest allowed version. So your teammate gets 1.3.0, not 2.0.0, because ^ never jumps a MAJOR.
Lock file: the same version on every laptop
If package-lock.json is committed too, npm install on your teammate's laptop installs the version recorded there: 1.2.0, exactly the same as yours, even though 1.3.0 has been released. "It works on my laptop" bugs become much rarer.
| Package manager | Lock file |
|---|---|
| npm | package-lock.json |
| pnpm | pnpm-lock.yaml |
| yarn | yarn.lock |
| composer | composer.lock |
On a CI server (a server that automatically runs tests and builds every time there is new code), use npm ci. This command deletes node_modules, installs exactly what the lock file says, and stops with an error if the lock file doesn't match package.json.
⚠️ Why the install fails or versions differ
-
apt installwithoutsudo. Instead of installing git, you get this error. Run it again assudo apt install git.E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root? -
Not committing the lock file, but committing
node_modules. It should be the opposite: commitpackage-lock.json, ignorenode_modules/. -
Mixing npm, pnpm and yarn in one project. Each tool makes its own lock file, so the project has several lock files, and they can disagree. Use one tool, the one that matches the lock file already in the project.
-
The wrong tool for the system. Ubuntu doesn't have
brewbuilt in, and macOS has noapt, so you getcommand not found. -
An old Node.js from
apt.aptgives the version that Ubuntu chose: Ubuntu 24.04 installs Node 18, which has had no security updates since April 2025. For Node.js, use a version manager (a program that installs and switches Node versions) such asnvmorfnm, the NodeSource repository,brew install nodeon macOS, orwinget install OpenJS.NodeJS.LTSon Windows.
TL;DR: A package manager installs, updates and removes software (packages) with one command. For programs on your computer: sudo apt install package-name on Ubuntu/Debian (needs sudo, admin rights), brew install git on macOS. For libraries (ready-made code) in a project: npm install name adds a line to package.json, fills node_modules/, and records the exact version in package-lock.json. Versions are written MAJOR.MINOR.PATCH: ^1.2.0 may go up to just before 2.0.0, ~1.2.0 only allows 1.2.x, and without a lock file (such as package-lock.json) npm installs the highest allowed version. Commit the lock file (not node_modules) to Git so every laptop gets the same versions.
⚡ Install a program on your computer
Write:
sudo apt update
sudo apt install git
Meaning: apt is the package manager (software installer) of Ubuntu and Debian. apt writes to system folders, so it needs sudo (admin rights). apt update only refreshes the package list. On macOS, use brew install git, no sudo.
⚡ Install a library in a project
Write:
npm install format-price
Meaning: npm installs libraries (ready-made code) for JavaScript projects, here format-price. package.json (your project's list of packages) gets a new line, the library's code goes into node_modules/, and the exact version is recorded in package-lock.json.
⚡ What ^ means
Guess first: versions are written MAJOR.MINOR.PATCH, and a ^ in front of a version means "may go up, as long as MAJOR stays the same". package.json says ^1.2.0, and the registry (where packages are kept) has 1.2.5, 1.3.0 and 2.0.0. Without a lock file (package-lock.json), which version gets installed?
1.3.0. Without a lock file, npm picks the highest allowed version. ^ may go up in MINOR and PATCH, but never jumps a MAJOR.
Meaning: MAJOR goes up when there are changes that can break old code. ~1.2.0 is stricter: only 1.2.x.
⚡ Commit the lock file
Meaning: A lock file such as package-lock.json records the exact version of each package. With that file committed to Git, every laptop installs exactly the same versions. node_modules/ is the opposite: it goes in .gitignore.