Skip to content

3.1 Install nodejs and npm

Tomato6966 edited this page Sep 23, 2023 · 16 revisions

INSTALL THE ESSENTIALS FIRST!

Make sure to follow: 2.2 Install Essentials

New way of installing nodejs

sudo apt-get update && sudo apt-get install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
sudo apt-get update && sudo apt-get install nodejs -y

Old way of installing nodejs

Installing nodejs v16 && npm

apt update -y
apt upgrade -y
apt install -y build-essential curl
curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
apt-get install -y nodejs

Installing nodejs v18 && npm

apt update -y
apt upgrade -y
apt install -y build-essential curl
curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
apt-get install -y nodejs


Node has a default max memory usage of less than 2GB on some 64bit systems. This can cause unexpected memory issues when running on CircleCI. It's possible to adjust the max memory ceiling using a command-line flag passed into Node:
--max-old-space-size=<memory in MB>

The flag can be difficult to ensure that it is being used, especially when node processes are forked. The best way to ensure this setting will be picked up by all node processes within the environment is to apply this setting directly to an environment variable, which can be done using Node. 8 or higher:

NODE_OPTIONS=--max-old-space-size=4096





Installing any other Version via nvm (Node-Version-Manager)

Install nvm first

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

Running the above command downloads a script and runs it. The script clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Use nvm

To download, compile, and install the latest release of node, do this:

nvm install node # "node" is an alias for the latest version

To install a specific version of node:

nvm install 16.13.0 # or 17.1.0, 8.9.1, etc

The first version installed becomes the default. New shells will start with the default version of node (e.g., nvm alias default).

You can list available versions using ls-remote:

nvm ls-remote

And then in any new shell just use the installed version:

nvm use node
nvm use 16.13.0

Set a default nodejs version

nvm alias default 16.13.0
Clone this wiki locally