The pnpm is a fast and efficient package manager for Node.js that works with packages published to the npm registry. It offers an alternative to npm while using the standard package.json format. This tutorial explains how to install pnpm on Ubuntu 26.04.
Prepare environment
Before installing pnpm, make sure Node.js and Corepack are available on the system. Read the post for instructions on how to install them.
Install pnpm
Activate pnpm support through Corepack:
sudo corepack enable pnpm
This step creates a small executable wrapper command (shim) that allows Corepack to manage pnpm and direct all pnpm commands to the correct version.
Corepack is the preferred way to use pnpm because it supports project-specific versions. When the packageManager field exists in the package.json file of the project, Corepack automatically downloads and runs the declared pnpm release. If the field is absent, the globally installed pnpm version is used instead.
Install a global pnpm version to serve as the default for projects that do not define a packageManager field:
corepack install -g pnpm
Confirm that pnpm has been installed successfully by checking version:
pnpm --version
Testing pnpm
Create a directory for a test project and move into it:
mkdir myproject && cd myproject
Generate a minimal package.json file:
echo '{"name":"myproject","version":"1.0.0"}' > package.json
Install a sample dependency to verify that pnpm is working correctly:
pnpm add express
Uninstall pnpm
Turn off pnpm support in Corepack:
sudo corepack disable pnpm
Delete the Corepack pnpm cache, which holds the pnpm versions managed by Corepack:
rm -rf ~/.cache/node/corepack/v1/pnpm
Remove pnpm state data and configuration directories:
rm -rf ~/.cache/pnpm ~/.local/{share,state}/pnpm
Leave a Comment
Cancel reply