65

This is the way I installed nodejs in ubuntu 14.04 LTS:

sudo add-apt-repository ppa:chris-lea/node.js

sudo apt-get install nodejs

When I checked the node version with this:

node -v

I get this

v0.10.37

But the latest version is 4.2.6 and 5.5.0. How can I get the latest or update version?

Aamu
  • 3,431
  • 7
  • 39
  • 61

14 Answers14

150
sudo apt-get install curl

For Node.js v4

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

For Node.js v5:

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

Node.js v6:

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

Node.js v7:

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

Node.js 8:

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

https://nodejs.org/en/download/package-manager/

war1oc
  • 2,705
  • 1
  • 17
  • 20
  • 1
    While I'm not a fan of piping directly into bash like this, I have to say this is my go-to method for installing up-to-date versions of nodejs on Ubuntu these days. The default apt packages for Ubuntu 14.04 are outdated and NVM is certainly not without issues due to installing to your user home directory. Appreciate the info. – Brandon K Mar 18 '16 at 02:59
  • 1
    @BrandonK I know what you mean by "not a fan". Executing a command like that is basically like saying "here, I trust you - whoever you are - with root control of my machine". "Securi-what?" There's a little bit of credibility there in that these specific commands are listed in the official Node.js installation instructions, though. – Tomislav Nakic-Alfirevic Jul 19 '16 at 08:05
  • This will not longer work on 140.4 LTS: after `curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -` and `sudo apt-get install -y nodejs` I get `$ nodejs --version v0.10.25` – loretoparisi Sep 05 '16 at 08:46
  • @loretoparisi do you have a node version manager like nvm or n? – war1oc Sep 05 '16 at 12:55
  • what is the meaning of `sudo -E bash -` what does it do? – Jas Oct 10 '16 at 19:06
  • @Jas it gives root access to the script which you are getting through curl, it's usually not a good idea to give a script root access but since this is from the official node js documentation (you may also review the script first) I believe it's ok. – war1oc Oct 12 '16 at 03:16
  • Why do we need the '-E' which eng does it preserves? – Jas Oct 12 '16 at 06:25
  • @Jas it is just an instruction so that the user's environment variables are preserved – war1oc Oct 14 '16 at 02:43
  • what is the significance of -y – Chetan Oct 16 '16 at 23:39
  • @Chetan it passes 'y' as in 'yes' to the prompts that might come, such as 'are you sure you want to install this package' etc. – war1oc Oct 17 '16 at 03:40
55

On Ubuntu 14.04.5 LTSthe easier way is

1 Install npm:

sudo apt-get install npm

  1. Install n

sudo npm install n -g

  1. Get latest version of node

sudo n latest

If you prefer to install a specific version of `node you can

2.1 List available node versions

n ls

2.2 and the install a specific version

sudo n 4.5.0

loretoparisi
  • 15,724
  • 11
  • 102
  • 146
6

There is an issue with node and npm update in Ubuntu14.04 LTS 64 bit OS. Since Google Chrome repository no longer provides 32-bit packages, 64-bit Ubuntu/Debian users will notice an error when updating the software sources, which looks as follows:

Failed to fetch http://dl.google.com/linux/chrome/deb/dists/stable/Release Unable to find expected entry 'main/binary-i386/Packages' in Release file (Wrong sources.list entry or malformed file) Some index files failed to download. They have been ignored, or old ones used instead.

So to fix this issue, the repository must be specifically set for 64-bit only. This can be done by the command

sudo sed -i -e 's/deb http/deb [arch=amd64] http/' "/etc/apt/sources.list.d/google-chrome.list"

i,e You should set it for 64 bit only before installing node. So the exact procedure to install latest node and npm will be

sudo sed -i -e 's/deb http/deb [arch=amd64] http/' "/etc/apt/sources.list.d/google-chrome.list"

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

sudo apt-get install -y nodejs

I had such an issue and got this solution from here. Hope this will help someone.

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55
  • I ran into the same problem, and in addition to what @Hari Krishnan says, I had to run the following - sudo sed -i -e 's/deb http/deb [arch=amd64] http/' "/etc/apt/sources.list.d/google.list" – learning_to_swim Mar 28 '16 at 16:58
  • This one worked for me after trying all the others :) – Drag0 Aug 17 '16 at 18:16
3

Here i am going to tell you how to install nodejs compile and install into your Linux Server.

Step 1-:

$ cd /opt/
$ wget https://nodejs.org/dist/v6.2.1/node-v6.2.1.tar.gz

Extract the tar.gz source code

$ tar -xvf node-*.tar.gz

Step 2-: Compile and install the nodejs.

$ cd node-v6.2.1
$ ./configure
$ make
$ sudo make install

Note-: If you found error “make command not found”

$ sudo apt-get update

$ sudo apt-get upgrade

$ sudo apt-get install build-essential

$ gcc -v

$ make -v
Arvind
  • 31
  • 1
1

Running Ubuntu Mate 14.04 LTS

  1. curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
  2. sudo apt-get install -y nodejs
  3. nodejs -v
Ahmed Hamed
  • 411
  • 4
  • 9
1

Checkout nvm. It manages node distributions for you, so you can have multiple projects running that use different nodejs versions.

nvm lets you choose exactly which version of node you need. With apt-get you will always only get the latest version that has been included into debian/ubuntu by those package maintainers, but those are usually very old. Especially in an area like nodejs, this is mostly not suitable.

Reto
  • 3,107
  • 1
  • 21
  • 33
J.D.
  • 1,145
  • 2
  • 15
  • 29
1

This worked for me:

sudo npm cache clean -f sudo npm install -g n sudo n stable

Hope it helps someone too :)

Herman Demsong
  • 321
  • 3
  • 7
1

Assuming you already have npm package and want to upgrade nodejs version:

sudo npm install -g n
sudo n latest

In case you don't have installed npm package then itstall it using following command:

sudo apt-get install npm

On linux.

user2719152
  • 939
  • 3
  • 11
  • 20
1

NVM (Node Version manager)

https://github.com/creationix/nvm

NVM installs both the latest stable node and npm for you

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | sh
source ~/.nvm/nvm.sh
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

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

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
  • 347,512
  • 102
  • 1,199
  • 985
0

Better way to do is,

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

based on version can change, setup_6.x into 7,8 etc

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0
wget -qO- https://deb.nodesource.com/setup_X.x | sudo bash -
sudo apt-get install -y nodejs
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

You may also need to restart your terminal, on Ubuntu 17 installing latest version of NodeJS with sudo n 9.0.0

if you check the version with node -v it won't report correctly, close the terminal, open a new terminal and check again with node -v it will be reporting correctly

Leigh Mathieson
  • 1,658
  • 2
  • 17
  • 25
0

The easiest way for me:

  1. Download the latest version of nodejs in https://nodejs.org/en/

  2. Change directory to: cd /usr/local

  3. Install the binaries, by using the following command:

    sudo tar --strip-components 1 -xJf ~/Downloads/node-v14.16.0-linux-x64.tar.xz

  4. node -v

  5. npm -v

Aliton Oliveira
  • 1,224
  • 1
  • 15
  • 26
-1

Ubuntu 14.04 contains a version of Node.js in its default repositories that can be used to easily provide a consistent experience across multiple servers. The version in the repositories is 0.10.25. This will not be the latest version, but it should be quite stable.

In order to get this version, we just have to use the apt package manager. We should refresh our local package index prior and then install from the repositories:

sudo apt-get update
sudo apt-get install nodejs

If the package in the repositories suits your needs, this is all that you need to do to get set up with Node.js. In most cases, you'll also want to also install npm, which is the Node.js package manager. You can do this by typing:

sudo apt-get install npm

This will allow you to easily install modules and packages to use with Node.js.

Because of a conflict with another package, the executable from the Ubuntu repositories is called nodejs instead of node. Keep this in mind as you are running software.

Community
  • 1
  • 1
Harish Verma
  • 548
  • 7
  • 17