Install Node with Sudo Access
Some distributions of Linux, particularly Raspbian, don’t have recent versions of Node in their repositories, and there’s often a mismatch in versions between Node and npm. To add to the problem, some version managers also don’t support ARM architectures. While the excellent n does support ARM, it doesn’t install Node or npm in a way that is accessible via
In general, you shouldn’t be using Node or npm with
sudo, but getting the permissions right for that situation can be a huge pain, and sometimes you just need a Raspberry Pi with a hastily set up Node server to run some local projects. Getting
sudo to work with Node and npm in these instances can also be a huge pain, and the vast array of different possible answers can be daunting to sift through.
For quick, one-off setups, the following will most likely work, and only takes a few minutes – without having to worry about
sudo having access to it:
~ $ sudo apt-get install -y nodejs npm ~ $ sudo npm i -g n npm@lts ~ $ sudo n lts
The first step installs the Node and npm versions available on your distro’s repository. The second line will install the latest version of npm. NOTE: you may see a warning about a mismatch in versions while this step is running. It is safe to ignore. The third step uses n to install the lts version of Node.
That’s it! Test your Node and npm with sudo by running
~ $ sudo node -v and
~ $ sudo npm -v. Both should output their version numbers. This also works to install Node and npm for the Windows Subsystem for Linux.