Обновление node.js и npm. Ubuntu
Предполагается, что обновления ставим из под пользователя с ограниченными правами, поэтому добавляется sudo к команде.
Для удобства так же будет установлено маленькое дополнение.
1) Обновляем менеджер пакетов NPM, согласно синтаксиса npm update [name [name . ]]
sudo npm install -g npm
2) Очишаем кеш NPM
sudo npm cache clean -f
3) Установим маленький вспомогательный пакет, под названием ‘n’
sudo npm install -g n
4) Теперь установим последнюю стабильную версию Node.js
sudo n stable
Так же можем установить более новый вариант, указав номер версии Node.js. На момент написания статьи это 7.9.0
sudo n 7.9.0
Ubuntu Server: Как обновить NodeJS до последней стабильной версии
Для того что бы обновить уже установленный на вашем Ubuntu Server Node.JS до последней стабильной версии нужно последовательно выполнить следующие команды:
sudo npm cache clean -f sudo npm install -g n sudo n stable
Затем выполним перепривязку командной строки к последней версии
sudo apt-get install --reinstall nodejs-legacy
Если вас не устраивает последняя стабильная версия и вам нужна самая свежая версия ноды замените sudo n stable на sudo n latest и будет вам счастье.
Как обновить NodeJS?
Использовать Bash-скрипт для управления/переключения нодой: nvm .
Версия для Windows .
Особый смак в переключениях активной версии ноды, например:
nvm list * 4.6.1 0.12.0 node -v v4.6.1 nvm use 0.12.0 Now using node v0.12.0 node -v v0.12.0
Если хочется попроще, то предлагаю использовать следующий алгоритм:
-
Очистить кэш npm :
npm cache clean -f
npm install -g n
-
Стабильная версия:
n stable
n latest
n 0.8.14
n rm 0.8.14
n - 0.8.14
Для OS X можно использовать менеджер пакетов Homebrew :
$ brew upgrade node
Отслеживать
ответ дан 27 фев 2017 в 5:07
user207618 user207618
Ответ выполняет поставленную задачу, но лучше использовать nvm , который специально для этого и был создан + он дает возможность очень просто переключаться между версиями
27 фев 2017 в 5:24
@ВасилийБарбашев, пожалуй да, можно и про него добавить. Сейчас обновлю.
– user207618
27 фев 2017 в 5:28
Когда мне на CentOs 7 требовалось обновить версию NodeJs(там по-моему изначально 6-ая стоит), поскольку в частности не устанавливался Puppeteer я обновлялся до 10-ой версии по этой статье http://blog.ivru.net/?id=221
Если коротко там написано, что NodeJs стандартной версии на CentOs 7 устанавливается выполнением команды: yum install nodejs
При этом если Вам нужна более свежая версия NodeJs чем стандартная, Вы можете перед установкой выполнить команду curl -sL https://rpm.nodesource.com/setup_10.x | sudo bash — в данном примере команды для установки будет выбрана 10-ая версия, Вы перед нажатием Enter можете отредактировать версию на нужную Вам.
Более подробно по установке читайте по ссылке, так же в этой же статье Вы сможете найти файлы необходимых репозиториев(по умолчанию или необходимых для установки) в случаи если Ваши файлы повреждены и попытка установки заканчивается ошибкой.
How can I update my nodeJS to the latest version?
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.
Nov 17, 2018 at 16:11
18 Answers 18
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
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.
105 4 4 bronze badges
answered Jun 8, 2014 at 23:14
18.4k 1 1 gold badge 14 14 silver badges 7 7 bronze badges
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.
Feb 19, 2015 at 22:30
but still nodejs —version sow 0.10.25
Dec 29, 2016 at 19:30
Jan 12, 2017 at 12:15
Not working — after following the steps, node -v still gave me the 6 version, not 10.
Jun 8, 2018 at 13:17
npm WARN using —force I sure hope you know what you are doing. I don’t know what I am doing.
Mar 12, 2019 at 4:28
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 v18.x:
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_18.x | bash - apt-get install -y nodejs
Node.js v16.x:
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_16.x | bash - apt-get install -y nodejs
Node.js v14.x:
# Using Ubuntu curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - sudo apt-get install -y nodejs # Using Debian, as root curl -sL https://deb.nodesource.com/setup_14.x | bash - apt-get install -y nodejs
Node.js v13.x (outdated):
# 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 (outdated):
# 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 (outdated):
# 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 (outdated):
# 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 (outdated):
# 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 (outdated):
# 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 (outdated):
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 (outdated):
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 (outdated):
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 (outdated):
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 (outdated):
# 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 (outdated):
# 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
1,457 2 2 gold badges 16 16 silver badges 26 26 bronze badges
answered Nov 12, 2014 at 14:54
7,275 2 2 gold badges 17 17 silver badges 25 25 bronze badges
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.
Jul 17, 2016 at 16:21
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?
Jan 23, 2019 at 11:13
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.
Jan 23, 2019 at 11:19
curl
Jun 26, 2019 at 11:26
@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
Jun 26, 2019 at 19:18
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
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
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
135 10 10 bronze badges
answered Feb 25, 2018 at 7:17
9,520 28 28 gold badges 88 88 silver badges 151 151 bronze badges
This should be the only accepted answer — in case you want to have a sane and up to date Node setup in Ubuntu.
Aug 20, 2018 at 13:35
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!
Jun 12, 2019 at 5:26
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 .
Jul 25, 2019 at 21:42
@d4nyll good remark, but it is mostly applied to multiuser systems.
Apr 2, 2020 at 14:17
While nvm can be pretty handy, be cautious that it increases bash startup time dramatically.
Jun 28, 2020 at 14:54
Note: 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
135 10 10 bronze badges
answered Feb 26, 2014 at 21:47
Walid Saad Walid Saad
413 3 3 silver badges 4 4 bronze badges
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.
Feb 27, 2014 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
Dec 22, 2014 at 11:46
@Capaj nope. that didn’t help.
Dec 24, 2014 at 0:47
Don’t recommend using this ppa any more, it seems to be rather out of date.
Dec 2, 2015 at 12:12
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.
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.
212 2 2 silver badges 11 11 bronze badges
answered Mar 31, 2014 at 14:00
379 2 2 silver badges 4 4 bronze badges
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
Mar 31, 2014 at 14:30
Apologies for my noobness. Updated answer
Mar 31, 2014 at 16:27
Aug 18, 2014 at 3:35
Don’t forget to update the url part to the latest version of nvm
Oct 21, 2016 at 6:59
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
- 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
/home/ciro/.nvm/versions/node/v0.9.0/bin/node
which vaca
/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"))'
/home/ciro/.nvm/versions/node/v0.9.0/lib/node_modules/vaca/index.js
- https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package
- https://stackoverflow.com/questions/7970793/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.
Tested in Ubuntu 17.10.
answered Feb 3, 2019 at 13:43
Ciro Santilli OurBigBook.com Ciro Santilli OurBigBook.com
27k 14 14 gold badges 109 109 silver badges 108 108 bronze badges
its worked for me
Sep 18, 2020 at 3:59
Thank you! Worked for me
May 18, 2021 at 6:17
CLEAN SIMPLE STEPS (which I use) :
- Uninstall previous version of node —
sudo apt remove node && sudo apt autoremove - Go to https://nodejs.org/en/ and download the latest version of node.
- 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 - now run this in terminal —
nano ~/.bashrc - 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.` - run a last command source ~/.bashrc
That’s it.
Now you can check your node version by node -v
answered Sep 19, 2021 at 13:11
Abhishek Kamal Abhishek Kamal
928 4 4 gold badges 12 12 silver badges 25 25 bronze badges
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 😉
4,461 1 1 gold badge 22 22 silver badges 31 31 bronze badges
answered Apr 12, 2015 at 4:43
Priyank Thakkar Priyank Thakkar
141 4 4 bronze badges
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
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs
answered May 13, 2015 at 12:17
Kunal Kapadia Kunal Kapadia
129 2 2 bronze badges
Are you installing node from scratch? The question was about updating not installing
Feb 24, 2017 at 5:56
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.
answered May 4, 2018 at 13:32
smartmouse smartmouse
188 8 8 bronze badges
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.
answered Jul 18, 2018 at 9:43
Damjan Pavlica Damjan Pavlica
388 3 3 silver badges 10 10 bronze badges
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.
answered Aug 9, 2020 at 18:42
2,330 6 6 gold badges 33 33 silver badges 53 53 bronze badges
the only answer that worked for me
Jun 5 at 21:51
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
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=$ # 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 [[ "$" == "Darwin" ]]; then OS_ARCH=darwin-x64 elif [[ "$" == "Linux" ]]; then OS_ARCH=linux-x64 else echo "ERROR - failed to recognize OS $curr_OS" exit 5 fi if [[ -z $ ]]; then echo "ERROR - failed to see env var CODE_PARENT_DIR" exit 5 fi export NODE_CODEDIR=$/nodejs export COMSUFFIX=tar.gz export NODE_NAME=node-$ export NODE_PARENT=$/$-$ export PATH=$/bin:$ export NODE_PATH=$/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 $" echo mkdir -p $ echo echo "cd $" cd $ echo # this is compiled code NOT source [ -f $-$.$ ] && rm $-$.$ # if file exists remove echo "wget -q --show-progress https://nodejs.org/download/release/$/$-$.$" wget -q --show-progress https://nodejs.org/download/release/$/$-$.$ echo echo "tar -C $ -xf $-$.$" tar -C $ -xf $-$.$ echo [ -f $-$.$ ] && rm $-$.$ # if file exists remove # . done . # which node node --version # . bottom of file install_node.sh