curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -sudo apt install nodejsCheck:
$ node --versionOutput: v10.16.3Check:
$ npm --versionOutput: 6.9.0
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -sudo apt install nodejsCheck:
$ node --versionOutput: v10.16.3Check:
$ npm --versionOutput: 6.9.0
Try using n. After installing node in any way, eg. as described below:
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
you should have npm available. Install n with:
npm install -g n
and select correct version
n lsrto list available versionsn latestto use latestn 12.0.0to use 12.0
Although "ppa:chris-lea/node.js" does a great job ( answer by Moxley Stratton is fine), I am no fan of upgrade cycles in my dev environment, having to depend on a third-party PPA, having different names of binary files(nodejs, node) which seem to mess with my scripts.
I download the node precompiled tar.gz from http://nodejs.org/dist/ and maintain by own folder with different versions(prod, dev). Maintain a symlink in PATH for node and npm. My system (64 bit) E.g
cd node_base_folder
wget http://nodejs.org/dist/v0.10.24/node-v0.10.24-linux-x64.tar.gz
tar -zvxf node-v0.10.24-linux-x64.tar.gz
cd /usr/local/bin
ln -s node_base_folder/node-v0.10.24-linux-x64/bin/node
ln -s node_base_folder/node-v0.10.24-linux-x64/bin/npm
Whenever you want to move to new node version. Just follow above steps (after changing version)
Follow the directions on the "Installing Node.js via package manager" wiki page, section "Ubuntu, Mint, elementary OS", under "Obtaining a recent version of Node...", copied here:
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install python-software-properties python g++ make nodejs
However, nodejs-legacy does not install for me either:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
nodejs-legacy
0 upgraded, 1 newly installed, 0 to remove and 6 not upgraded.
Need to get 15.4 kB of archives.
After this operation, 119 kB of additional disk space will be used.
Get:1 http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ saucy/universe nodejs-legacy all 0.10.15~dfsg1-4 [15.4 kB]
Fetched 15.4 kB in 0s (243 kB/s)
Selecting previously unselected package nodejs-legacy.
(Reading database ... 63176 files and directories currently installed.)
Unpacking nodejs-legacy (from .../nodejs-legacy_0.10.15~dfsg1-4_all.deb) ...
dpkg: error processing /var/cache/apt/archives/nodejs-legacy_0.10.15~dfsg1-4_all.deb (--unpack):
trying to overwrite '/usr/share/man/man1/node.1.gz', which is also in package nodejs 0.10.26-1chl1~saucy1
Processing triggers for man-db ...
Errors were encountered while processing:
/var/cache/apt/archives/nodejs-legacy_0.10.15~dfsg1-4_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
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.
The n module worked for me.
Run this code to clear npm’s cache, install n, and install the latest stable version of Node:
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
See: http://www.hostingadvice.com/how-to/update-node-js-latest-version/
And: https://www.npmjs.com/package/n
To install a specific version of node:
sudo n 18.17.1
To check what version:
node -v
You might need to restart
NVM (Node Version manager)
https://github.com/nvm-sh/nvm
Advantages:
allows you to use multiple versions of Node and without sudo
is analogous to Ruby RVM and Python Virtualenv, widely considered best practice in Ruby and Python communities
downloads a pre-compiled binary where possible, and if not it downloads the source and compiles one for you
Tested in Ubuntu 17.10:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.nvm/nvm.sh
nvm install 0.9.0
nvm install 0.9.9
nvm use 0.9.0
node --version
#v0.9.0
nvm use 0.9.9
node --version
#v0.9.9
For the particular case of the most recent long term support version (recommended if you can choose):
nvm install --lts
nvm use --lts
npm --version
npm install --global vaca
vaca
Since the sourcing has to be done for every new shell, the install script hacks adds some auto sourcing to the end of your .barshrc. That works, but I prefer to remove the auto-added one and add my own:
f="$HOME/.nvm/nvm.sh"
if [ -r "$f" ]; then
. "$f" &>'/dev/null'
nvm use --lts &>'/dev/null'
fi
With this setup, you get for example:
which node
gives:
/home/ciro/.nvm/versions/node/v0.9.0/bin/node
and:
which vaca
gives:
/home/ciro/.nvm/versions/node/v0.9.0/bin/vaca
and if we want to use the globally installed module:
npm link vaca
node -e 'console.log(require.resolve("vaca"))'
gives:
/home/ciro/.nvm/versions/node/v0.9.0/lib/node_modules/vaca/index.js
as mentioned at:
- NodeJS require a global module/package
- How do I import global modules in Node? I get "Error: Cannot find module <module>"?
so we see that everything is completely contained inside the specific node version.
For projects however, you are better off just using packages installed locally under node_modules and npx for executable to be able to have independent versions across projects, global usage is mostly for the Node executable itself and global CLI utilities not specific to any project.
Setting the NPM version
Simply:
npm install [email protected] -g
The executable is placed inside the current NVM version, so everything remains nice and isolated, e.g.:
which npm
gives something like:
/home/ciro/.nvm/versions/node/v14.17.0/bin/npm
How can I change the version of npm using nvm?
Use n module from npm in order to upgrade node
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
To upgrade to latest version (and not current stable) version, you can use
sudo n latest
Fix PATH:
sudo apt-get install --reinstall nodejs-legacy # fix /usr/bin/nodeTo undo:
sudo n rm 6.0.0 # replace number with version of Node that was installed sudo npm uninstall -g n
You may need to restart your terminal to see the updated node version.
Found in David Walsh blog
NodeSource provides binary distributions of Node.js; complete installation instructions can be found here. The instructions have been copied below for your reference. Instructions are the same for updating to the latest version.
Run once:
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
Run whenever you want to change the major version of Node.js:
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
Prior to August 2023: Previously versions of this answer involved the use of a series of setup_XX.x scripts that you'd run to install/update Node.js:
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt-get install -y nodejs
But as @eis pointed out, these scripts are no longer supported. To see the previous answers, please look at the edit history for this answer.
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.