Linux Command Reference ยท Packages and Services
Linux pip and npm basics
Reference for the essential pip and npm commands on Linux: install, uninstall, list, freeze, virtual environments, global installs and avoiding common mistakes.
pip installs Python packages from PyPI; npm installs JavaScript packages from the npm registry. They are not part of the Linux package system: they manage libraries for one language, usually inside a project folder, and they never talk to apt or dnf. Knowing a handful of commands covers most daily use.
The most important habit is isolation. Install project libraries into a virtual environment (Python) or into the project's node_modules (Node) instead of the whole system, so that projects with different needs cannot break each other or the operating system's own tools.
python3 -m venv .venv && . .venv/bin/activate pip install requests npm install express
Try it
The sandbox has no network, so nothing can be downloaded. The first example creates a virtual environment (an offline operation, here without pip because the sandbox lacks ensurepip) and shows how it changes PATH; the second parses a sample requirements.txt and package.json.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
pip [--user] COMMAND [OPTIONS] [PACKAGES]
npm COMMAND [OPTIONS] [PACKAGES]
| pip | npm | Description |
|---|---|---|
pip install pkg |
npm install pkg |
Install a package (npm also adds it to package.json). |
pip install -r requirements.txt |
npm install (or npm ci) |
Install everything a project lists. |
pip uninstall pkg |
npm uninstall pkg |
Remove a package. |
pip list |
npm ls |
Show what is installed. |
pip freeze > requirements.txt |
package-lock.json |
Record exact versions. |
pip show pkg |
npm view pkg |
Show package details. |
pip install -U pkg |
npm update pkg |
Upgrade a package. |
python3 -m venv .venv |
n/a | Create an isolated environment. |
pip install -e . |
npm link |
Install a project in editable (development) mode. |
pipx install tool |
npm install -g tool |
Install a command-line tool. |
Version specifiers: pkg==1.2.3 is exact, pkg>=1.2 is a minimum in pip. In npm, ^1.2.3 allows minor and patch updates, ~1.2.3 allows patch updates only.
Returns: 0 on success, non-zero on resolution failures, missing packages or network errors.
Usage notes
- Never
sudo pip install. It can overwrite files managed by the distribution and break system tools. Recent Debian, Ubuntu and Fedora releases refuse system-wide installs with an "externally managed environment" error; use a venv, orpipxfor command-line tools. - Use
python3 -m pipso you are sure thepipmatches thepython3you run. Inside an activated venv plainpipis correct. - Global npm installs need root unless you set a prefix (
npm config set prefix ~/.local). Prefer project-local installs and run tools withnpx tool. - Commit lock files.
package-lock.jsonpins every dependency;npm ciinstalls exactly that and fails if it does not matchpackage.json. For pip, tools such aspip-toolsoruvproduce lock files. node_modulesand.venvare build artifacts: keep them out of Git (.gitignore) and recreate them from the dependency file.- Security: installing a package can run code (
setup.py, npmpostinstallscripts). Install only packages you trust, and audit withnpm auditorpip-audit. - Offline or CI use:
pip downloadandnpm ci --prefer-offlinework with caches; set--index-urlor a registry mirror in restricted networks. - Compared with
apt: language managers give newer versions and per-project control, but no system-level security updates;aptpackages are older but maintained by the distribution.
Related
apt,make,snap and flatpak- Lessons: Packages and services, Environment