如何在 Ubuntu 上安装特定版本的 Node?

我想在 Ubuntu 12.04上安装 NodeJS 版本0.8.18。我尝试安装最新版本,然后通过使用 nvm恢复到0.8.18,但是当我运行我的代码时,显然安装的软件包和两个版本(最新版本和0.8.18)有一些问题。因为我不知道如何解决这个问题,所以我清理了 Node 安装中的机器,并考虑直接安装我感兴趣的版本(v0.8.18)。

261606 次浏览

Chris Lea has 0.8.23 in his ppa repo.

This package let you add a repository to apt-get: (You can also do this manually)

sudo apt-get install software-properties-common

Add Chris Lea's repository:

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

Update apt-get:

sudo apt-get update

Install Node.js:

sudo apt-get install nodejs=0.8.23-1chl1~precise1

I think (feel free to edit) the version number is optional if you only add node.js-legacy. If you add both legacy and ppa/chris-lea/node.js you most likely need to add the version.

FYI the available version for raring in Chris Lea's repo is currently 0.8.25

sudo apt-get install nodejs=0.8.25-2chl1~raring1

The Node.js project recently pushed out a new stable version with the 0.10.0 release Use the following command on Ubuntu 13x sudo apt-get install nodejs=0.10.18-1chl1~raring1

version 0.10 is also avaible with this ppa

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

install nodejs with:

apt-get install nodejs=0.10.25-1chl1~precise1

Thanks to my friend Julian Xhokaxhiu

NOTE: you can use NVM software to do this in a more nodejs fashionway. However i got issues in one machine that didn't let me use NVM. So i have to look for an alternative ;-)

You can manually download and install.

go to nodejs > download > other releases http://nodejs.org/dist/

choose the version you are looking for http://nodejs.org/dist/v0.8.18/

choose distro files corresponding your environmment and download (take care of 32bits/64bits version). Example: http://nodejs.org/dist/v0.8.18/node-v0.8.18-linux-x64.tar.gz

Extract files and follow instructions on README.md :

To build:

Prerequisites (Unix only):

* Python 2.6 or 2.7
* GNU Make 3.81 or newer
* libexecinfo (FreeBSD and OpenBSD only)

Unix/Macintosh:

./configure
make
make install

If your python binary is in a non-standard location or has a non-standard name, run the following instead:

export PYTHON=/path/to/python
$PYTHON ./configure
make
make install

Windows:

vcbuild.bat

To run the tests:

Unix/Macintosh:

make test

Windows:

vcbuild.bat test

To build the documentation:

make doc

To read the documentation:

man doc/node.1

Maybe you want to (must to) move the folder to a more apropiate place like /usr/lib/nodejs/node-v0.8.18/ then create a Symbolic Lynk on /usr/bin to get acces to your install from anywhere.

sudo mv /extracted/folder/node-v0.8.18 /usr/lib/nodejs/node-v0.8.18
sudo ln -s /usr/lib/nodejs/node-v0.8.18/bin/node /usr/bin/node

And if you want different release in the same machine you can use debian alternatives. Proceed in the same way posted before to download a second release. For example the latest release.

http://nodejs.org/dist/latest/ -> http://nodejs.org/dist/latest/node-v0.10.28-linux-x64.tar.gz

Move to your favorite destination, the same of the rest of release you want to install.

sudo mv /extracted/folder/node-v0.10.28 /usr/lib/nodejs/node-v0.10.28

Follow instructions of the README.md file. Then update the alternatives, for each release you have dowload install the alternative with.

sudo update-alternatives    --install genname symlink  altern  priority  [--slave  genname  symlink altern]
Add a group of alternatives  to  the  system.   genname  is  the
generic  name  for  the  master link, symlink is the name of its
symlink  in  the  alternatives  directory,  and  altern  is  the
alternative being introduced for the master link.  The arguments
after  --slave  are  the  generic  name,  symlink  name  in  the
alternatives  directory  and alternative for a slave link.  Zero
or more --slave options, each followed by three  arguments,  may
be specified.


If   the   master   symlink  specified  exists  already  in  the
alternatives system’s records, the information supplied will  be
added  as a new set of alternatives for the group.  Otherwise, a
new group, set to  automatic  mode,  will  be  added  with  this
information.   If  the group is in automatic mode, and the newly
added alternatives’ priority is higher than any other  installed
alternatives  for  this  group,  the symlinks will be updated to
point to the newly added alternatives.

for example:

sudo update-alternatives --install /usr/bin/node node /usr/lib/nodejs/node-v0.10.28 0 --slave /usr/share/man/man1/node.1.gz node.1.gz /usr/lib/nodejs/node-v0.10.28/share/man/man1/node.1

Then you can use update-alternatives --config node to choose between any number of releases instaled in your machine.

FYI, according to this page in the wiki of the nodejs github repo, Chris Lea's PPA (mentioned in several other answers) has been superseded by the NodeSource distributions as the main way of installing nodejs from source in Ubuntu:

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

This is supported for the three latest (at the time of writing this) LTS versions of Ubuntu: 10.04 (lucid), 12.04 LTS (precise) and 14.04 (trusty).

I'm not sure this will help in installing an old version of nodejs, but I'm putting this here in case it helps others who needed to install a specific (newer) version of nodejs that isn't included in their distro's repositories.

Try this way. This worked me.

  1. wget nodejs.org/dist/v0.10.36/node-v0.10.36-linux-x64.tar.gz(download file)

  2. Go to the directory where the Node.js binary was downloaded to, and then run command i.e, sudo tar -C /usr/local --strip-components 1 -xzf node-v0.10.36-linux-x64.tar.gz to install the Node.js binary package in “/usr/local/”.

  3. You can check:-

    $ node -v
    v0.10.36
    $ npm -v
    1.4.28
    

It is possible to install specific version of nodejs from nodejs official distribution with using dpkg.

For example, currently recent 4.x version is 4.2.4, but you can install previous 4.2.3 version.

curl -s -O https://deb.nodesource.com/node_4.x/pool/main/n/nodejs/nodejs_4.2.3-1nodesource1~trusty1_amd64.deb
sudo apt-get install rlwrap
sudo dpkg -i nodejs_4.2.3-1nodesource1~trusty1_amd64.deb

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 6.11.2

To check what version:

node -v

You might need to restart

yes, its a duplicate answer but I insist using n module to install a specific version(following commands installs node version 6.9.5).

npm install -g  n
n 6.9.5

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 | sh
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:

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.

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

Setting the NPM version

Simply:

npm install npm@6.14.13 -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?

Install nvm using the following commands in the same order.nvm stands for node version manager.

sudo apt-get update
sudo apt-get install build-essential checkinstall libssl-dev
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

In case the above command does not work add -k after -o- .It should be as below:

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

Then nvm ls-remote to see the available versions. In case you get N/A in return,run the following.

export NVM_NODEJS_ORG_MIRROR=http://nodejs.org/dist

alternatively you can run the following commands too

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_completion

Then nvm install #.#.# replacing # by version(say nvm 8.9.4) finally nvm use #.#.#

To install a specific version of nodejs in Ubuntu you can use below commands, just specify and replace the version number, for example, node_12.x will fetch the latest of 12.

curl https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb https://deb.nodesource.com/node_7.x $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install nodejs

Here is a list of available builds for debian: https://github.com/nodesource/distributions/tree/master/deb

For this example, lets assume you want version 14 (LTS at the time of writing)

We can download this script from github, execute it and install the version of node we want. For security reasons it's a good idea to read the script prior to executing it.

curl -sL https://raw.githubusercontent.com/nodesource/distributions/master/deb/setup_14.x | bash
apt-get install -y nodejs # may or may not require sudo based on your setup

I like this approach because it doesn't require extraneous dependencies like nvm to target specific versions

If you are building for a different distro or architecture you can find more builds here https://nodejs.org/dist/

Say you want to install Node 10,

Firstly, download and execute the Node.js 10.x installer:

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

This will add a source file for the official Node.js 10.x repo, grabs the signing key

Once the installer is done doing it’s thing, you will need to install (or upgrade) Node.js:

sudo apt install nodejs

I imagine many directed here are looking for this to add to a Dockerfile

RUN set -x \
&& curl -sL 'https://deb.nodesource.com/setup_16.x' | bash - \
&& apt-get -y install nodejs \
&& ln -s /usr/bin/nodejs /usr/local/bin/node

In ubuntu specific version of node can be installed with help of nvm

install nvm

sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.bashrc

To install a particular version of node, use the command nvm install and add the number of the version.

nvm install 10.15.2
node -v