40

I have having a bit of an issue with installing nodejs and npm on my linux server (which is a pi running raspbian). I had everything set up and running using

sudo apt-get install nodejs npm

All was fine and dandy, until I found out that apparently these versions are now old. So I removed them

sudo apt-get purge nodejs npm

Then I found the following answer (here) on SO and ran

curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get install -y nodejs

Running node -v have me version 0.6.19...which I'm assuming translates to version 6.19 as opposed to version 0. However, running npm -v told me that it was not installed. So I once again purged nodejs, and looked for another solution. At which point I decided to follow the stuff on nodejs's site (here). And I ran the following commands.

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

and

sudo apt-get install -y build-essential

2 issues:

1) The version installed was still 0.6.19. I would rather have version 4.x, since that's what I'm running on my dev machine (macOS Sierra).

2) I still don't have npm. Which renders nodejs useless

Any help on either (but preferably 2) would be great. Thanks in advance.

5 Answers 5

81

I really recommend you install node and npm using nvm. This is the fastest, cleanest and easiest way to do it.

That way, you install NVM simply doing:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

To test that nvm was properly installed, close and re-open Terminal and enter nvm. If you get a nvm: command not found message, your OS may not have the necessary .bash_profile file. In Terminal, enter touch ~/.bash_profile and run the above install script again.

And you are now able to install node typing:

nvm install <version>

For example

nvm install 4.2.1

if you just want to install the latest node version, you can just type

nvm install node

In order to access node and npm as sudo (in order to have <1024 ports) you should run

n=$(which node)
n=${n%/bin/node}
chmod -R 755 $n/bin/* 
sudo cp -r $n/{bin,lib,share} /usr/local 
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks @Luis González. I certainly owe you a beer. Why nvm isn't the first approach that pops up on google is beyond me. Super each and straightforward.
I'd like to add, that in order to access node and npm as sudo (in order to have <1024 ports) i had to run " n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local "
Thank you very much, Luis! Also, if you just want to install the latest node version, you can just type nvm install node instead of <version>.
this is the best way doing it
I should point out that the URL that this post recommends you pipe into sh is some random github user's code. It's not official NVM account. Here's the URL that NVM suggests you use: raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh
|
10

I wrote in the terminal the following command lines I hope it is useful for the community.

$ sudo apt install nodejs
$ curl -L https://npmjs.org/install.sh | sudo sh 

good luck!

Comments

4

Sometimes, installing a specific version of the node.js from the authorized source does not work properly. It will be better if will be installed after downloading the node package from the link(https://nodejs.org/en/) and following the steps....

  1. Unzip the compressed package using the following command:

tar --xf node-v16.13.1-linux-x64.tar.xz

  1. Make a directory inside /usr/local/:

sudo mkdir -p /usr/local/nodejs

  1. Move all unzip files to newly made dir:

sudo mv node-v16.13.1-linux-x64/* /usr/local/nodejs/ 4. Open .bashrc file using command prompt:

sudo nano ~/.bashrc

  1. Add the following command inside the .bashrc file at the end and close the file:

export PATH=$PATH:/usr/local/nodejs/bin

  1. Print the path for justification:

echo $PATH

  1. Verify the node version:

node --version

Comments

2

Below are the simple steps to proceed with the installation

  1. Open Terminal
  2. Run command to install nodejs : sudo apt install nodejs
  3. Run command to verify installation by checking version: node -v or node –version
  4. Run command to install npm: sudo apt install npm
  5. Run command to verify installation of npm: npm -v or npm –version

For reference: https://youtu.be/DGjfw4y0nTI

Comments

-1

Solution 1:

I sometimes use the following script:

#!/bin/bash

# https://github.com/nodejs/node?tab=readme-ov-file#verifying-binaries

# Error handling function
handle_error() {
    echo "Error: $1"
    exit 1
}

version_url="https://nodejs.org/download/release/latest/"

# Fetch the latest Node.js release page and extract the filename of the Linux x64 tarball
file_name=$(curl -Lfs "${version_url}" | grep -oP '(?<=href=").*linux-x64.tar.xz(?=")') || handle_error "Failed to fetch Node.js release page or extract filename."

if command -v node &>/dev/null; then
    node_version=$(node --version)

    if [ "$file_name" = "node-${node_version}-linux-x64.tar.xz" ]; then
        echo "Already up to date."
        exit 0
    fi
fi

# Construct the complete download URL
full_url="${version_url}${file_name}"

# Download the tarball
wget -q --show-progress -nc "$full_url" || handle_error "Failed to download Node.js tarball."

# Extract the tarball
# tar -xf ${file_name} || handle_error "Failed to extract Node.js tarball."

install_directory=~/node.js

mkdir -p "$install_directory"

tar -xvf ${file_name} -C "$install_directory" --strip-components=1

# Cleanup
rm -v ${file_name}

echo "Node.js installation completed successfully."

Then add the path to $PATH wherever appropriate in your case (i added in .profile and .zshenv).

# set PATH so it includes Node.js
if [ -d "$HOME/node.js" ] ; then
    export PATH="$HOME/node.js/bin:$PATH"
fi

It just download the binary of latest release from https://nodejs.org/download/release/latest/ and place them in ~/node.js.

It you want lts then place appropriate url in $version_url.

For example, https://nodejs.org/download/release/latest/ can be replaced with https://nodejs.org/download/release/latest-iron/.

You can also use this script to update, not just install.

WARNING: Not tested thoroughly. Use it at your own risk. Better, modify it according to your liking.

Solution 2:

As I use debian, the more reliable solution for me is https://deb.nodesource.com/ or, https://github.com/nodesource/distributions . (This way, i don't need to manually download and install Node.js or remember to update it separately.)

The command is:

curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - &&\
apt-get install -y nodejs

You can use setup_current.x instead of setup_lts.x.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.