Package Managers — Terminal

A package manager installs, updates and removes software (packages) with one command. For programs on your computer: sudo apt install package-name on…

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

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:

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):

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

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.

What you will learn