本文演示Ubuntu安装nodejs,当前(2017年11月13日)最新稳定版本为9.x。
#安装nodejs9.x curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
下面是安装具体步骤演示代码。(以实际代码运行结果为准,仅供参考)
root@minmin-VirtualBox:~# curl -sL https://deb.nodesource.com/setup | sudo bash - #root账号去掉sudo root@minmin-VirtualBox:~# curl -sL https://deb.nodesource.com/setup | bash - ================================================================================ ================================================================================ SCRIPT DEPRECATION WARNING This script,located at https://deb.nodesource.com/setup,used to install Node.js v0.10,is being deprecated and will eventually be made inactive. You should use the script that corresponds to the version of Node.js you wish to install. e.g. * https://deb.nodesource.com/setup_4.x — Node.js v4 LTS "Argon" (recommended) * https://deb.nodesource.com/setup_6.x — Node.js v6 Current Please see https://github.com/nodejs/LTS/ for details about which version may be appropriate for you. The NodeSource Node.js Linux distributions GitHub repository contains information about which versions of Node.js and which Linux distributions are supported and how to use the install scripts. https://github.com/nodesource/distributions ================================================================================ ================================================================================ Continuing in 10 seconds (press Ctrl-C to abort) ... ## Installing the NodeSource Node.js v0.10 repo... ## Populating apt-get cache... + apt-get update 命中:1 http://cn.archive.ubuntu.com/ubuntu xenial InRelease 获取:2 http://cn.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB] 获取:3 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB] 获取:4 http://cn.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB] 获取:5 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [653 kB] 命中:6 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial InRelease 获取:7 http://packages.elementary.io/appcenter xenial InRelease [5,894 B] 获取:8 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [618 kB] 命中:9 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial InRelease 获取:10 http://apt.postgresql.org/pub/repos/apt xenial-pgdg InRelease [46.4 kB] 获取:11 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main amd64 Packages [72.5 kB] 获取:12 http://security.ubuntu.com/ubuntu xenial-security/main amd64 DEP-11 Metadata [60.3 kB] 获取:13 http://packages.elementary.io/appcenter xenial/main amd64 Packages [39.1 kB] 获取:14 http://apt.postgresql.org/pub/repos/apt xenial-pgdg/main i386 Packages [70.6 kB] 获取:15 http://security.ubuntu.com/ubuntu xenial-security/main DEP-11 64x64 Icons [67.1 kB] 获取:16 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main Translation-en [274 kB] 获取:17 http://packages.elementary.io/appcenter xenial/main amd64 DEP-11 Metadata [31.4 kB] 获取:18 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main amd64 DEP-11 Metadata [307 kB] 获取:19 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 DEP-11 Metadata [51.4 kB] 获取:20 http://packages.elementary.io/appcenter xenial/main DEP-11 64x64 Icons [171 kB] 获取:21 http://security.ubuntu.com/ubuntu xenial-security/universe DEP-11 64x64 Icons [85.1 kB] 获取:22 http://cn.archive.ubuntu.com/ubuntu xenial-updates/main DEP-11 64x64 Icons [220 kB] 获取:23 http://cn.archive.ubuntu.com/ubuntu xenial-updates/restricted amd64 Packages [8,064 B] 获取:24 http://cn.archive.ubuntu.com/ubuntu xenial-updates/restricted i386 Packages [8,084 B] 获取:25 http://cn.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [544 kB] 获取:26 http://cn.archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [518 kB] 获取:27 http://cn.archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [221 kB] 获取:28 http://cn.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 DEP-11 Metadata [174 kB] 获取:29 http://cn.archive.ubuntu.com/ubuntu xenial-updates/universe DEP-11 64x64 Icons [245 kB] 获取:30 http://cn.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [16.2 kB] 获取:31 http://cn.archive.ubuntu.com/ubuntu xenial-updates/multiverse i386 Packages [15.3 kB] 获取:32 http://cn.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 DEP-11 Metadata [5,888 B] 获取:33 http://cn.archive.ubuntu.com/ubuntu xenial-backports/main amd64 DEP-11 Metadata [3,324 B] 获取:34 http://cn.archive.ubuntu.com/ubuntu xenial-backports/universe amd64 DEP-11 Metadata [4,588 B] 已下载 4,841 kB,耗时 19秒 (246 kB/s) 正在读取软件包列表... 完成 ## You seem to be using elementaryOS version loki. ## This maps to Ubuntu "xenial"... Adjusting for you... ## Confirming "xenial" is supported... + curl -sLf -o /dev/null 'https://deb.nodesource.com/node_0.10/dists/xenial/Release' ## Adding the NodeSource signing key to your keyring... + curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - OK ## Creating apt sources list file for the NodeSource Node.js v0.10 repo... + echo 'deb https://deb.nodesource.com/node_0.10 xenial main' > /etc/apt/sources.list.d/nodesource.list + echo 'deb-src https://deb.nodesource.com/node_0.10 xenial main' >> /etc/apt/sources.list.d/nodesource.list ## Running `apt-get update` for you... + apt-get update 命中:1 http://cn.archive.ubuntu.com/ubuntu xenial InRelease 命中:2 http://cn.archive.ubuntu.com/ubuntu xenial-updates InRelease 命中:3 http://cn.archive.ubuntu.com/ubuntu xenial-backports InRelease 命中:4 http://security.ubuntu.com/ubuntu xenial-security InRelease 命中:5 http://apt.postgresql.org/pub/repos/apt xenial-pgdg InRelease 命中:6 http://packages.elementary.io/appcenter xenial InRelease 命中:7 http://ppa.launchpad.net/elementary-os/stable/ubuntu xenial InRelease 获取:8 https://deb.nodesource.com/node_0.10 xenial InRelease [3,899 B] 获取:9 https://deb.nodesource.com/node_0.10 xenial/main Sources [682 B] 获取:10 https://deb.nodesource.com/node_0.10 xenial/main amd64 Packages [932 B] 获取:11 https://deb.nodesource.com/node_0.10 xenial/main i386 Packages [932 B] 命中:12 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu xenial InRelease 已下载 6,445 B,耗时 4秒 (1,414 B/s) 正在读取软件包列表... 完成 ================================================================================ ================================================================================ NODE.JS v0.10 DEPRECATION WARNING Node.js v0.10 will cease to be actively supported in October 2016. This means you will not continue to receive security or critical stability updates for this version of Node.js beyond that time. You should begin migration to a newer version of Node.js as soon as possible. Use the installation script that corresponds to the version of Node.js you wish to install. e.g. * https://deb.nodesource.com/setup_4.x — Node.js v4 LTS "Argon" (recommended) * https://deb.nodesource.com/setup_6.x — Node.js v6 Current Please see https://github.com/nodejs/LTS/ for details about which version may be appropriate for you. The NodeSource Node.js Linux distributions GitHub repository contains information about which versions of Node.js and which Linux distributions are supported and how to use the install scripts. https://github.com/nodesource/distributions ================================================================================ ================================================================================ Continuing in 5 seconds ... ## Run `apt-get install nodejs` (as root) to install Node.js v0.10 and npm
切换到root 账号或使用root账户权限。
root@minmin-VirtualBox:~# sudo -i #输入root用户密码 root@minmin-VirtualBox:~# apt update root@minmin-VirtualBox:~# apt install nodejs nodejs-dev nodejs-dbg debhelper
完成安装后,就可以使用npm,检查版本信息。
root@minmin-VirtualBox:~# nodejs -v v9.1.0
使用淘宝安装源替换系统默认npm,等待安装进度完成。
root@minmin-VirtualBox:~# npm install -g cnpm --registry=https://registry.npm.taobao.org [ ......] - extract:balanced-match: sill pacote balanced-match@http://registry.npm.taobao.org/balanced-match/download/balanced-matc /usr/bin/cnpm -> /usr/lib/node_modules/cnpm/bin/cnpm + cnpm@5.1.1 added 692 packages in 1202.934s
演示安装http-server。
root@minmin-VirtualBox:~# cnpm install -g http-server Downloading http-server to /usr/lib/node_modules/http-server_tmp [npminstall:get] retry GET http://registry.npm.taobao.org/http-server/download/http-server-0.10.0.tgz after 100ms,retry left 4,error: ConnectionTimeoutError: Connect timeout for 60000ms,GET https://cdn.npm.taobao.org/http-server/-/http-server-0.10.0.tgz -2 (connected: false,keepalive socket: false,agent status: {"createSocketCount":1,"createSocketErrorCount":0,"closeSocketCount":1,"errorSocketCount":0,"timeoutSocketCount":0,"requestCount":0,"freeSockets":{},"sockets":{},"requests":{}}) headers: {} Copying /usr/lib/node_modules/http-server_tmp/_http-server@0.10.0@http-server to /usr/lib/node_modules/http-server Installing http-server's dependencies to /usr/lib/node_modules/http-server/node_modules [1/8] opener@~1.4.0 installed at node_modules/_opener@1.4.3@opener [2/8] colors@1.0.3 installed at node_modules/_colors@1.0.3@colors [3/8] optimist@0.6.x installed at node_modules/_optimist@0.6.1@optimist [4/8] ecstatic@^2.0.0 installed at node_modules/_ecstatic@2.2.1@ecstatic [npminstall:get] retry GET http://registry.npm.taobao.org/corser/download/corser-2.0.1.tgz after 100ms,GET https://cdn.npm.taobao.org/corser/-/corser-2.0.1.tgz -2 (connected: false,agent status: {"createSocketCount":9,"closeSocketCount":6,"timeoutSocketCount":4,"requestCount":11,"sockets":{"cdn.npm.taobao.org:443:::::::true::":3},"requests":{}}) headers: {} [npminstall:get] retry GET http://registry.npm.taobao.org/http-proxy/download/http-proxy-1.16.2.tgz after 100ms,GET https://cdn.npm.taobao.org/http-proxy/-/http-proxy-1.16.2.tgz -2 (connected: false,"closeSocketCount":7,"sockets":{"cdn.npm.taobao.org:443:::::::true::":2},"requests":{}}) headers: {} [npminstall:get] retry GET http://registry.npm.taobao.org/union/download/union-0.4.6.tgz after 100ms,GET https://cdn.npm.taobao.org/union/-/union-0.4.6.tgz -2 (connected: false,"closeSocketCount":8,"sockets":{"cdn.npm.taobao.org:443:::::::true::":1},"requests":{}}) headers: {} [npminstall:get] retry GET http://registry.npm.taobao.org/portfinder/download/portfinder-1.0.13.tgz after 100ms,GET https://cdn.npm.taobao.org/portfinder/-/portfinder-1.0.13.tgz -2 (connected: false,"closeSocketCount":9,"requests":{}}) headers: {} [5/8] union@~0.4.3 installed at node_modules/_union@0.4.6@union [npminstall:get] retry GET http://registry.npm.taobao.org/corser/download/corser-2.0.1.tgz after 200ms,retry left 3,agent status: {"createSocketCount":14,"closeSocketCount":12,"timeoutSocketCount":6,"requestCount":17,"requests":{}}) headers: {} [npminstall:get] retry GET http://registry.npm.taobao.org/http-proxy/download/http-proxy-1.16.2.tgz after 200ms,"closeSocketCount":13,"requests":{}}) headers: {} [npminstall:get] retry GET http://registry.npm.taobao.org/mkdirp/download/mkdirp-0.5.1.tgz after 100ms,GET https://cdn.npm.taobao.org/mkdirp/-/mkdirp-0.5.1.tgz -2 (connected: false,agent status: {"createSocketCount":16,"closeSocketCount":14,"requests":{}}) headers: {} [6/8] corser@~2.0.0 installed at node_modules/_corser@2.0.1@corser [7/8] portfinder@^1.0.13 installed at node_modules/_portfinder@1.0.13@portfinder [8/8] http-proxy@^1.8.1 installed at node_modules/_http-proxy@1.16.2@http-proxy All packages installed (22 packages installed from npm registry,used 2m,speed 8.77kB/s,json 20(45.15kB),tarball 1020.36kB) [http-server@0.10.0] link /usr/bin/http-server@ -> /usr/lib/node_modules/http-server/bin/http-server [http-server@0.10.0] link /usr/bin/hs@ -> /usr/lib/node_modules/http-server/bin/http-server root@minmin-VirtualBox:~# root@minmin-VirtualBox:~# http-server & [3] 3522 root@minmin-VirtualBox:~# Starting up http-server,serving ./ Available on: http://127.0.0.1:8080 http://10.1.1.1:8080 Hit CTRL-C to stop the server root@minmin-VirtualBox:~#
参考链接:
原文链接:https://www.f2er.com/ubuntu/350601.html