1029

I have installed nodeJS on Ubuntu with following code

sudo apt-get install nodejs

Since I am a new user for ubuntu I also ran this code too

sudo apt-get install npm

Now when I type

 nodejs --version

It shows

v0.6.19

I checked and saw latest nodeJS version is 0.10.26

How can I update my version of nodeJS to 0.10.26?

I tried with

 sudo apt-get install <packagename>
 sudo apt-get install --only-upgrade <packagename>

but no luck.

Jorge Castro
  • 69,284
  • 123
  • 459
  • 651
Kanishka Panamaldeniya
  • 10,413
  • 3
  • 13
  • 11
  • 5
    It's better to use nvm to install nodejs. It stands for "Node Version Manager". You can very easily install multiple versions of nodejs, switch between them and set a default. – Mina Michael Nov 17 '18 at 16:11

16 Answers16

1707

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/node
    
  • To 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

Osama Rizwan
  • 105
  • 4
eballeste
  • 17,282
  • 1
  • 12
  • 7
  • This should be the correct answer, also works for Mac OSX. – tszming Dec 02 '14 at 15:55
  • 67
    I would advise strongly against doing this on a production instance. It can seriously mess stuff up with your global npm packages and your ability to install new ones. – Loourr Feb 19 '15 at 22:30
  • 3
    I get an error: /usr/bin/n: line 299: curl: command not found Error: invalid version 0.12.6 – Fourat Jul 08 '15 at 17:27
  • I second @Loourr this seems to be too hacky: `npm WARN using --force I sure hope you know what you are doing.` and what is this mysterious n package? :) – matanster Sep 07 '15 at 07:13
  • 7
    I tried this... now when I run `node` I get this error `bash: /usr/bin/node: No such file or directory` what should I do ? – Yerko Palma Oct 11 '15 at 00:44
  • 1
    @Fourat just use the absolute path - for example - /usr/local/lib/node_modules/n/bin/n stable – Ankan-Zerob Jan 08 '16 at 09:19
  • 4
    Everything worked perfectly while installing, but when I checked version `sudo nodejs -v` it gave me same version as was before. – Ejaz Karim Aug 11 '16 at 17:47
  • 2
    @eballeste `sudo ln -sf /usr/local/n/versions/node//bin/node /usr/bin/node` so every time you change the node version you have to create a soft link too? – Watchmaker Oct 22 '16 at 11:46
  • if npm is misbehaving you can do: sudo ln -sf /usr/local/n/versions/node/7.0.0/bin/npm /usr/local/bin/npm – Lior Nov 08 '16 at 14:35
  • 20
    but still nodejs --version sow 0.10.25 – Mansuu.... Dec 29 '16 at 19:30
  • 17
    [David Walsh says use NVM instead](https://davidwalsh.name/nvm) – Ads20000 Jan 12 '17 at 12:15
  • After switching Node.js versions using n, npm may not work properly. This should fix it (thanks @mikemoser!): `$ curl -0 -L https://npmjs.org/install.sh | sudo sh` or `$ sudo ln -sf /usr/local/n/versions/node/7.0.0/bin/npm /usr/bin/npm` – deKajoo Feb 09 '17 at 21:30
  • This works perfectly, but there's no need to create the softlink. After running: "sudo n stable" node version is already updated – camposer Aug 04 '17 at 22:19
  • 1
    One correction. Use the -H option in sudo otherwise it tries to use locations belonging to the current user during the install process, rather than root. – David Sep 29 '17 at 13:45
  • 1
    `sudo ln -sf /usr/local/n/versions/node//bin/node /usr/bin/nodejs` caused massive problems for me. Can you explain a little further into what this line is doing and how to use it correctly. This answer as is, is unclear. – Goose Jan 04 '18 at 20:00
  • i have no idea why that line was added to my answer, my original answer was what worked for me and has been edited since, several times – eballeste Jan 04 '18 at 21:09
  • 1
    I removed the extra line which is not in fact needed. Hopefully it's not edited back in. – KthProg Mar 26 '18 at 18:49
  • 3
    Not working: `sudo: n: command not found` – Niklas Rosencrantz May 27 '18 at 09:30
  • 27
    Not working - after following the steps, `node -v` still gave me the 6 version, not 10. – Tomáš Zato - Reinstate Monica Jun 08 '18 at 13:17
  • This method of upgrading node is now unstable and should not be used. https://davidwalsh.name/upgrade-nodejs – Damjan Pavlica Jul 18 '18 at 09:45
  • This assumes the user has npm. Raspberry pi (at least the 2017-11-29 release, which isn't that old) comes with an old version of node, but not npm. The other answer that uses curl is far more portable because it is compatible with far more environments. – yeerk Jul 20 '18 at 03:05
  • sudo: n: command not found – Unkas Aug 04 '18 at 15:52
  • 2
    Works like a charm on Windows Linux Subsystem – Yoraco Gonzales Dec 04 '18 at 21:40
  • 31
    `npm WARN using --force I sure hope you know what you are doing.` I don't know what I am doing... – Jesse Reza Khorasanee Mar 12 '19 at 04:28
  • 2
    Thanks! This works also great to install the LTS version: `sudo n lts` – Daviz Apr 25 '19 at 13:49
  • You don't have to reboot, just log out an log in again, see: [https://stackoverflow.com/a/55862776/8398149](https://stackoverflow.com/a/55862776/8398149) – sneaky Oct 08 '19 at 10:23
  • What an obnoxious command name. As if I'm going to be constantly managing my node versions. – Boris Verkhovskiy Nov 13 '19 at 16:44
  • 1
    You need to restart the terminal in order to changes apply, worth it to clarify – Eugenio Dec 12 '19 at 18:13
  • Working solution is here: https://stackoverflow.com/questions/41195952/updating-nodejs-on-ubuntu-16-04/46762957/#answer-46762957 – Alex Shul Jan 11 '20 at 03:29
  • @Loourr can you elaborate on why? So you don't recommend using `n`? – Gayan Weerakutti Jan 11 '20 at 08:20
  • Can also use hash -d node to refresh the cache and point to the updated version of node. Much cleaner. https://askubuntu.com/questions/1036278/npm-is-incorrect-version-on-latest-ubuntu-18-04-installation – Ramonster Jan 21 '20 at 14:32
  • https://medium.com/@ExplosionPills/dont-use-sudo-with-npm-still-66e609f5f92 – Gryu Apr 02 '20 at 13:30
  • Seeing all these solutions for different ways one should and should not update, pitfalls and different comments how it went wrong and messed up... shesh. Makes me afraid of even touching my npm installation. What happened to "Do one thing, and do it good"? That only applies to programmers using the tools, but not the toolmakers? – brat Jan 15 '21 at 13:19
  • This answer should be flagged. Do not run on production server, it will mess up your node. – Soheil May 29 '21 at 05:17
  • `npm i node` Let each package specify it's own version. Stop worrying about what version of node is installed in your system. – Cameron Tacklind Jun 13 '21 at 20:10
  • This works! However, not sure why apt install did not install latest in first place. – AdityaKapreShrewsburyBoston Aug 24 '21 at 23:18
  • `sudo n lts` is what I needed. – Tadej Dec 02 '21 at 22:02
  • thank you. saw many comment about didn't work and think the "don't forget to restart after update" need to be bold, – Kyusuf Muhammad Mar 11 '22 at 22:29
  • Dose not work, I get `npm does not support Node.js v10.19.0` – Black Mar 29 '22 at 10:09
  • Until `sudo n latest` all worked fine. After restarting the terminal running `node - v` I get the latest. But running `nodejs --version` still gets the older one. `sudo apt-get install --reinstall nodejs-legacy` gives an error `E: Package 'nodejs-legacy' has no installation candidate` and `sudo n rm ` says that the version is `not in downloads cache` So how can I delete the old version? I suppose that originally I installed node with `sudo apt-get install nodejs` – as5 Apr 07 '22 at 19:04
  • For anyone who is having issues installing, I have a guide for installing `n` here: https://askubuntu.com/a/1403000/808646 – flen May 19 '22 at 17:55
609

Complete installation instructions have since been uploaded here by Nodesource. It is copied below for your reference. Instructions are the same for updating to the latest version.

If you don't like curl ... | sudo bash - then you can follow the manual instructions, which tells you how to add the Node.js repository to /etc/apt/sources.list.d/ yourself.

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 6.x on older distros

Node.js Current:

# Using Ubuntu
curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -fsSL https://deb.nodesource.com/setup_current.x | bash -
apt-get install -y nodejs

Node.js v13.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_13.x | bash -
apt-get install -y nodejs

Node.js v12.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -y nodejs

Node.js v11.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_11.x | bash -
apt-get install -y nodejs

Node.js v10.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt-get install -y nodejs

Node.js v9.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_9.x | bash -
apt-get install -y nodejs

Node.js v8.x:

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_8.x | bash -
apt-get install -y nodejs

Node.js v7.x:

NOTE: Debian Wheezy and Ubuntu Precise packages are NOT available for this release. Please reference running Node.js >= 4.x on older distros

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_7.x | bash -
apt-get install -y nodejs

Node.js v6.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 4.x on older distros.

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_6.x | bash -
apt-get install -y nodejs

Node.js v5.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 4.x on older distros.

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_5.x | bash -
apt-get install -y nodejs

Node.js v4.x:

NOTE: If you are using Ubuntu Precise or Debian Wheezy, you might want to read about running Node.js >= 4.x on older distros.

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

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_4.x | bash -
apt-get install -y nodejs

Node.js v0.12:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_0.12 | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_0.12 | bash -
apt-get install -y nodejs

Node.js v0.10:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_0.10 | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_0.10 | bash -
apt-get install -y nodejs

io.js v3.x:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_iojs_3.x | sudo -E bash -
sudo apt-get install -y iojs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_iojs_3.x | bash -
apt-get install -y iojs

io.js v2.x:

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_iojs_2.x | sudo -E bash -
sudo apt-get install -y iojs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_iojs_2.x | bash -
apt-get install -y iojs

io.js v1.x:

Note: this branch of io.js is not actively maintained and is not recommended for production use.

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_iojs_1.x | sudo -E bash -
sudo apt-get install -y iojs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_iojs_1.x | bash -
apt-get install -y iojs
Tofandel
  • 103
  • 4
dayuloli
  • 6,835
  • 2
  • 15
  • 25
  • For me `node --version` still said "0.12.0". I had to `rm /usr/local/bin/node`, then I was able to use the newly installed version of nodejs. – Chris Clark Jul 17 '16 at 16:21
  • Following above I ended up with no node installed any more and none installing no matter what I tried :-( – Alex Sep 17 '18 at 15:38
  • 6
    This is *installation* instructions not *upgrade* instructions. Installation implies that it's never been installed before. If you already have a version installed as the question implies, then what happens if you install a different version? Do you get version conflicts? Does the installation delete the old one? – icc97 Jan 23 '19 at 11:13
  • 3
    For all confused idiots like me, it seems that running the installation will effectively replace what ever you had before. So I think you can perform either an upgrade or a downgrade with this process. I upgraded from v8 to v10 and I spotted this message in the install logs: `Unpacking nodejs (10.15.0-1nodesource1) over (8.12.0-1nodesource1) ...`, running `node -v` after the installation happily displays `v10.15.0` which is what I want. – icc97 Jan 23 '19 at 11:19
  • 5
    `curl {anything} | sudo bash -` is a terrible security practice – krubo Jun 26 '19 at 11:26
  • 1
    @krubo I agree. The user should examine the script before running it. However, it _is_ from a reputable source (`nodesource.com`) and the connection is secured by HTTPS, so there is an adequate amount of trust you can place in those scripts. But yes, in general, check the scripts before you pipe it to `sudo bash` – dayuloli Jun 26 '19 at 19:18
  • @dayuloli — I'm pretty certain close to 0% of everyone have enough free time *and* Bash skills to read these 300 lines scripts. In practice almost everyone just looks at the domain name (like you wrote), the number of upvotes this answer has gotten, and hopes for the best. It'd be nice if instead there was a way to juts add an Apt source manually – KajMagnus Apr 01 '20 at 13:01
  • I mean sth like: `Append to /etc/apt/sources.list.d/nodesource.list these lines: deb https://deb.nodesource.com/node_12.x xenial main \n deb-src https://deb.nodesource.com/node_12.x xenial main` — thanks anyway for posting the answer :- ) – KajMagnus Apr 01 '20 at 13:05
  • @KajMagnus that is a valid concern and a good suggestion. Feel free to edit the answer or create a new answer, I think it will be a good addition to the existing answers. – dayuloli Apr 02 '20 at 13:23
  • @dayuloli — Good idea, now I edited the question (the edits are waiting for approval). – KajMagnus Apr 03 '20 at 00:14
  • this one got working for me windows wsl of ubuntu – manojadams Jan 16 '21 at 08:55
  • You can run `ubuntu` as `root` so why 2 different commands where 1 `sudo` command for `both envs` would suffice. – Timo May 14 '21 at 14:29
  • @T.Todua NodeSource is a reputable source. If you look at the **official** documentation at [Installing Node.js via package manager | Debian and Ubuntu based Linux distributions](https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions), you'll see that it reads `Node.js binary distributions are available from NodeSource.`. – dayuloli Mar 04 '22 at 23:11
  • @dayuloli my bad if so. sorry (i've deleted my comment), i didn't know node had to use 3rd party commercial company to host their binaries. not ideal in my mind. – T.Todua Mar 05 '22 at 11:14
  • just one note (as my edit was rejected by moderator) I suggest, instead of having 10 duplicate paragraphs of 99% same code, just post one codeblock and as a title, mention that `instead of 13.x` you can use `12.x , ....etc` so, very long post will be more readable. – T.Todua Mar 05 '22 at 11:17
  • @T.Todua That is a valid suggestion; but doing so will mean people can't just find their target Node.js version and just copy + paste the commands. Also feel free to author a more concise solution as a new answer. – dayuloli Mar 06 '22 at 09:53
  • Does not work, I get `Certificate verification failed: The certificate is NOT trusted. The certificate chain uses expired certificate. Could not handshake: Error in the certificate verification.` – Black Mar 29 '22 at 10:18
  • @Black Have you tried installing `ca-certificates`? (`sudo apt install ca-certificates`) – dayuloli Mar 29 '22 at 19:19
78

I also recommend using nvm instead, and also removing the already installed version to avoid conflicts in the terminal

sudo apt purge nodejs npm

then install nvm and use it

Video Explanation

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

to download and install nvm

nvm install node

should download and install the latest version of node.

To update node later on just do

nvm install node
nvm alias default node
Gryu
  • 6,727
  • 9
  • 27
  • 48
Mina Michael
  • 8,880
  • 28
  • 83
  • 149
  • 15
    This should be the only accepted answer - in case you want to have a sane and up to date Node setup in Ubuntu. – José L. Patiño Aug 20 '18 at 13:35
  • 1
    This also works the best for me (have pretty ancient nodejs on our EC2 and tried lots of suggestions and only this one made it work). Also 100% agree with the comment above! – Shan Dou Jun 12 '19 at 05:26
  • 2
    `nvm` installs Node.js on a per-user basis. Using `nvm` will not give you a system-wide `node` command. So I would advice against removing the global/system `node` so scripts running as another user can still run `node`. – d4nyll Jul 25 '19 at 21:42
  • @d4nyll good remark, but it is mostly applied to multiuser systems. – Gryu Apr 02 '20 at 14:17
  • 1
    While nvm can be pretty handy, be cautious that it increases bash startup time dramatically. – extraymond Jun 28 '20 at 14:54
  • Thanks worked to me – Andrews Gomes Apr 14 '21 at 19:10
  • It is better to use `nvm install --lts` to get the **long-term support** version. `nvm install node` willl install the latest **Current** version. ` – Yura Loginov Jun 11 '21 at 22:25
  • `npm i node`. Let each program specify in its deps what version of `node` it wants to use. Don't depend on the system node. – Cameron Tacklind Jun 13 '21 at 20:11
28

This PPA is out of date and not maintained anymore; you should consider other answers instead of this one.

You can install the latest version from PPA:

sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs
Thomas Ward
  • 68,862
  • 29
  • 169
  • 231
Walid Saad
  • 413
  • 3
  • 4
  • 16
    I think this would be a better answer if you (also) provided a non-ppa way of doing this, ppas are notoriously known for becoming outdated and unmaintained, essentially making the answer useless. – pzkpfw Feb 27 '14 at 16:21
  • @bigbadonk420 yeah, if you just googled it, probably better to skip the first line and do just update and install-worked for me with node 0.10.34 – Capaj Dec 22 '14 at 11:46
  • @Capaj nope. that didn't help... – deostroll Dec 24 '14 at 00:47
  • 8
    Don't recommend using this ppa any more, it seems to be rather out of date. – Kzqai Dec 02 '15 at 12:12
23

I use NVM to handle my Node versions. Very simple to set up and easy to use.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

To install NVM globally instead, use the following curl command instead of the one above (and presumably don't use the second command but do use the third one)

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | NVM_DIR=/usr/local/nvm bash

Then use nvm install stable (or insert a version number instead of stable) to get the latest/a specific version of Node. Use nvm use stable (or a specific version number) to use that Node version. Use nvm ls to see what Node versions you have installed and nvm uninstall stable(or a specific version number) to remove a specific version of Node.

Sources: Install, usage

Note

If you are struggling with updating npm to the latest stable version because you are stuck at a specific version of npm and every time you update it that doesn't work then you can use this method to update npm as well. As soon as you install the latest stable version of node, npm will automatically be updated to its latest stable version.

Ayush
  • 125
  • 7
amnah
  • 339
  • 1
  • 4
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Warren Hill Mar 31 '14 at 14:30
  • Apologies for my noobness. Updated answer – amnah Mar 31 '14 at 16:27
  • the given curl path is not working. I used "curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash" as given in the https://github.com/creationix/nvm – Senthil Aug 18 '14 at 03:35
  • Don't forget to update the url part to the latest version of nvm – Nidhin David Oct 21 '16 at 06:59
12

NVM (Node Version manager) with --lts

NVM was mentioned at: https://askubuntu.com/a/441527/52975 but here goes a more complete usage example, including the sane --lts version, which installs the latest Long Term Support version of Node, which is likely the one you want if you don't have more specific requirement.

Install NVM and the latest Node LTS version:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
source ~/.nvm/nvm.sh
nvm install --lts
nvm use --lts
npm --version

Now test it out with a dummy package:

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

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

We can easily switch node versions with:

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

You can then use a git tracked .nvmrc file to indicate the node version required for a given project: https://stackoverflow.com/questions/24869959/how-do-i-specify-a-local-version-of-node-for-a-project/54503474#54503474

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

so we see that everything is completely contained inside the specific node version.

Tested in Ubuntu 17.10.

Ciro Santilli OurBigBook.com
  • 23,787
  • 11
  • 100
  • 98
5

CLEAN SIMPLE STEPS (which I use) :

  1. Uninstall previous version of node -
    sudo apt remove node && sudo apt autoremove

  2. Go to https://nodejs.org/en/ and download the latest version of node.

  3. Now open the terminal and change the directory where node is downloaded and then run-
    tar -xvf node-v12.16.2-linux-x64.tar.xz

  4. now run this in terminal -
    nano ~/.bashrc

  5. and append it with the following –
    export PATH=/path/to/node-js/bin:$PATH
    Where, /path/to/node-js/ is to be replaced where you extracted node in step 3.`

  6. run a last command source ~/.bashrc

That's it.
Now you can check your node version by node -v

Abhishek Kamal
  • 764
  • 2
  • 9
  • 22
4

I tried the same list commands on my ubuntu 14.04 but it was still throwing an error.

Commands I executed were:

sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  
sudo apt-get install nodejs

and the error i was getting:

Invalid version 0.12.2
Line 299: curl not found in /bin/n

What I figured out was curl utility was not installed on my os.

I executed command:

apt-get install curl

(use sudo as prefix if you are not su)

and then repeated the steps suggest in answer and it work ;)

Faizan Akram Dar
  • 4,391
  • 1
  • 21
  • 31
2

If using n does not work, you can install the latest version of nodejs (i. e. version 8) running the following commands:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

sudo apt-get install -y nodejs

Further instructions are here.

smartmouse
  • 188
  • 8
2

Using nvm is preferred method. First install nvm:

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

Then install nodejs:

nvm install node

Now on, you can easy switch versions of node.

Damjan Pavlica
  • 368
  • 3
  • 10
2

This one installs the latest node v0.12.* from nodesource.

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

For node v4.x

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

I suggest you first remove all nodejs installs then execute the below script once you edit with the desired nodejs version and its install parent directory

see all available nodejs versions

https://nodejs.org/dist/

The below script will allow you to install any of those nodejs versions on linux or OSX

#!/bin/bash

#     usage :
#
#     edit two vars in below :  NODE_VER  and  CODE_PARENT_DIR
#
# ... execute this script as yourself unless you choose a root-owned value for var CODE_PARENT_DIR
#     whichever id you execute this as determines the id you will issue npm commands as:  npm install -g foo-bar
#
#     NOTE - nodejs comes bundled with npm ... so no need to do separate npm install
#            This script runs fine on Linux or OSX

# ...  copy all the lines starting here  .. top of copy  ....   and ending ... end of copy  ...
#      and paste into your ~/.bashrc file so proper env vars get set 

# ............... top of copy  ........................  install_node.sh

# export NODE_VER=v7.2.0  # see available versions at https://nodejs.org/dist/
# export NODE_VER=v8.5.0  # edit this line next time you need to update nodejs  
export NODE_VER=v9.3.0  # edit this line next time you need to update nodejs

# ...  pick parent dir of nodejs install  ... comment out or remove ONE of below
# export CODE_PARENT_DIR=/opt/code  # root-owned dir ... requires you to sudo prior to npm install going forward
export CODE_PARENT_DIR=${HOME}    # RECOMMENDED executing as yourself including npm install

# ......... following env vars are OK no edits needed ... only ever need to edit above vars

curr_OS=$( uname )

echo curr_OS $curr_OS

if [[ "${curr_OS}" == "Darwin" ]]; then

    OS_ARCH=darwin-x64

elif [[ "${curr_OS}" == "Linux" ]]; then

    OS_ARCH=linux-x64
else
    echo "ERROR - failed to recognize OS $curr_OS"
    exit 5
fi

if [[ -z ${CODE_PARENT_DIR} ]]; then

    echo "ERROR - failed to see env var CODE_PARENT_DIR"
    exit 5
fi

export NODE_CODEDIR=${CODE_PARENT_DIR}/nodejs
export COMSUFFIX=tar.gz
export NODE_NAME=node-${NODE_VER}
export NODE_PARENT=${NODE_CODEDIR}/${NODE_NAME}-${OS_ARCH} 

export PATH=${NODE_PARENT}/bin:${PATH}
export NODE_PATH=${NODE_PARENT}/lib/node_modules

# ............... end of copy  ........................  install_node.sh

# copy and paste above from ... top of copy ... to here into your file ~/.bashrc   

echo
echo "NODE_CODEDIR $NODE_CODEDIR<--"
echo

echo "mkdir -p ${NODE_CODEDIR}"
echo
      mkdir -p ${NODE_CODEDIR}
echo

echo "cd ${NODE_CODEDIR}"
      cd ${NODE_CODEDIR}
echo

# this is compiled code NOT source

[ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove

echo "wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}"
      wget -q --show-progress https://nodejs.org/download/release/${NODE_VER}/${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}
echo

echo "tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}"
      tar -C ${NODE_CODEDIR} -xf ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX}
echo

[ -f ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} ] && rm ${NODE_NAME}-${OS_ARCH}.${COMSUFFIX} # if file exists remove

# ...........  done ........... #

which node

node --version

# ....  bottom of file   install_node.sh
Scott Stensland
  • 13,809
  • 8
  • 52
  • 69
0

If you are behind a proxy, maybe you could get this error when you run 'sudo n stable':

Error: invalid version

You have to set the env variables like this:

export HTTP_PROXY=http://your-proxy-url:port
export HTTPS_PROXY=http://your-proxy-url:port

And then run the command passing these variables to the root user:

sudo -E n stable
luandrea
  • 51
  • 7
0

I tried to install via nvm, as described above, but faced some error in Ubuntu 18.04. Then I went through this process:

sudo apt purge nodejs npm

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

nvm install node

After running this command, you might get this error:

Command 'nvm' not found, did you mean:

In that case you can run this command:

source ~/.nvm/nvm.sh

OR you can put it in the /.bashrc or ~/.profile file to automatically load it.

After that you can run the following commands:

nvm install node
nvm alias default node
BeastOfCaerbannog
  • 10,395
  • 9
  • 41
  • 69
sayalok
  • 261
  • 2
  • 6
  • 13
0

The selected answer did not work for me, I tried restarting terminal and PC but no success.

I have installed (as recommended in the docs) npm and node with node version manager. In this case, one can update node and npm simply with

nvm install stable

I explained here how to switch to reinstall with node-verion-manager if you have installed npm/node with Node installer.

Adam
  • 2,080
  • 6
  • 31
  • 49
0

Type following command:

sudo npm install n -g
sudo n stable