Per the Node.js website:
# Using Debian/Ubuntu
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
Then, you will have the latest version of Node.js.
If you're not a fan of curl <url> | bash -, or are using an unsupported distribution, you can try a manual installation.
Per the Node.js website:
# Using Debian/Ubuntu
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
Then, you will have the latest version of Node.js.
If you're not a fan of curl <url> | bash -, or are using an unsupported distribution, you can try a manual installation.
Node is one of the easier projects to build. Just change the version as that continues to change.
Browse to http://nodejs.org/dist/latest/ to find out the latest package version.
cd /usr/local/src
wget http://nodejs.org/dist/latest/node-v7.2.1.tar.gz
tar -xvzf node-v7.2.1.tar.gz
cd node-v7.2.1
./configure
make
sudo make install
which node
You should see /usr/local/bin/node.
In an Ubuntu 22 I can't install node latest version
[Ubuntu] How to install a newer version of Node than the one provided by apt?
npm - How can I install Node.js version 18 on Ubuntu 22.04? - Stack Overflow
node.js - how to install a latest version of Nodejs on Ubuntu? - Stack Overflow
Videos
I installed npm with
sudo apt install npm
and then tried to use npm to install Open MCT, but I got this error:
npm ERR! notsup Required: {"node":">=14.19.1"}
npm ERR! notsup Actual: {"npm":"7.5.2","node":"v12.22.5"}So I figured I needed a newer version of node than the one provided by apt. So I visited https://nodejs.org/en/download/ and downloaded "Linux Binaries (x64)" containing a .tar file containing a few libraries and files.
Question: How do I install it?
EDIT: Formatting
I had problems with this as well. So there are many tutorials how to install different version of nodejs but they did not work in my case. However the last with nvm did.
Method 1: Install nodejs from specific source
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
by changing setup_16.x to your version, you change the version
sudo bash nodesource_setup.sh
then you test sources with
# sample on Ubuntu 22.10
$ cat /etc/apt/sources.list.d/nodesource.list
deb https://deb.nodesource.com/node_16.x focal main
deb-src https://deb.nodesource.com/node_16.x focal main
Then you install nodejs from that source (did not work in my case)
sudo apt -y install nodejs
And check installed version
node -v
This should return
v16.19.0
but in my case (Ubuntu 22.10) I got the new version
v18.7.0
Method 2: Install Node Vesrion Manager or nvm This worked in my case:
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.bashrc
Now to install an older version of nodejs with nvm
you can first check available versions by
nvm list-remote
and then install specific version by (ex. version v16.19.0)
nvm install 16.19.0
and check with:
$ node -v
v16.19.0
To install latest version of node with nvm
nvm install node
$ node -v
v18.7.0
Hope this helps someone to
Install NVM, and the install node using nvm. Here is an example.
Install nvm
ubuntu@ip-172-26-0-227:~$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 16555 100 16555 0 0 198k 0 --:--:-- --:--:-- --:--:-- 199k => Compressing and cleaning up git repository => Appending nvm source string to /home/ubuntu/.bashrc => Appending bash_completion source string to /home/ubuntu/.bashrc => Close and reopen your terminal to start using nvm or run the following to use it now: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completionSource bashrc so nvm can be loaded, or close and re open terminal.
ubuntu@ip-172-26-0-227:~$ source /home/ubuntu/.bashrc
Now, install node version, passing the version..etc.
ubuntu@ip-172-26-0-227:~$ nvm install v12.22.10 Downloading and installing node v12.22.10... Downloading https://nodejs.org/dist/v12.22.10/node-v12.22.10-linux- x64.tar.xz..100.0% Computing checksum with sha256sum Checksums matched! Now using node v12.22.10 (npm v6.14.16) Creating default alias: default -> v12.22.10
I recommend using Node Version Manager:
NB: Node Version Manager (nvm) allows you to install multiple node versions and choose between them depending on which software you want to support; that is, manage multiple node versions.
Installing Node Version Manager
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Once you have Node Version Manager installed
- Confirm the available remote versions by running
nvm ls-remote - Run in your terminal
nvm install <version>for example,nvm install 18to install version 18 of Node - List installed versions with
nvm list - Run
nvm use 18to start using version 18 of Node - Note: you can be more explicit, for example:
nvm install 14.17.0
Install nodejs v20 on Debian and Ubuntu based distributions
step 1 : Download and import the Nodesource GPG key
sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
step 2 : Create deb repository
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
optional: here we are installing nodejs v20 but if you want to install v18 change NODE_MAJOR=18 in above command. Similarly if you want to install nodejs v16 then change NODE_MAJOR=16.
step 3 : Run Update and Install
sudo apt-get update
sudo apt-get install nodejs -y
congratulations you just installed nodejs
To uninstall nodejs
sudo apt-get purge nodejs &&\
rm -r /etc/apt/sources.list.d/nodesource.list &&\
rm -r /etc/apt/keyrings/nodesource.gpg
The current LTS version can be installed via
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
In difference to the already provided answers, this evergreen link will always install the current long term supported version. So you don't need to tweak your script every couple month.
This can help:
$ sudo npm install -g n
$ sudo npm cache clean -f
$ sudo n stable
also you can replase stable with latest.
NOTE: if $ node -v shows the old version, open a new shell.