There are 2 main ways to install NodeJS on a Raspberry Pi. A short way and a longish way.
The short way
The short way uses a script from NodeSource that does the heavy lifting. The installation instructions can be found here but essentially you run 2 lines of code.
sudo curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
sudo apt-get install -y nodejs
sudo
means you are running the command as a super usersetup_20.x
can be changed to whatever version of node you want to use.install
installs nodejs and the flag-y
makes yes the default so you as the user won’t have to hit ‘y’ when a question appears
To confirm that node is installed, run
node -v
The long way
The slightly longer way involves downloading NodeJS directly from NodeJS. Before you do, run uname -m
to show the architecture of your Pi. It will be ARM something.
Then head over to https://nodejs.org/en/download and find the Linux Binaries (ARM) version.
Right click and copy link address (eg: https://nodejs.org/dist/v18.16.1/node-v18.16.1-linux-armv7l.tar.xz) and then use wget to download the file.
sudo wget https://nodejs.org/dist/v18.16.1/node-v18.16.1-linux-armv7l.tar.xz
Then extract it:
sudo tar -xvf https://nodejs.org/dist/v18.16.1/node-v18.16.1-linux-armv7l.tar.xz
- the
-x
flag means extract - the
-v
flag means verbose mode - the
-f
flag means perform the operation on this file
To run node from any directory, we need to copy the files we extracted to a folder that is in our PATH. To view your PATH run:
echo $PATH
cd (change directory) into your node folder you extracted and use the cp (copy) command:
sudo cp -R * /user/local/bin
Finally run node -v
to see if it was installed successfully.