1. 程式人生 > >curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - 安裝失敗

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

  1. ubuntu軟體倉庫中自帶的nodejs版本過低
$ apt-cache policy nodejs
nodejs:
  Installed: (none)
  Candidate: 4.2.6~dfsg-1ubuntu4.2
  Version table:
     4.2.6~dfsg-1ubuntu4.2 500
        500 http://mirrors.aliyun.com/ubuntu xenial-updates/universe amd64 Packages
        500 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages
     4.2.6~dfsg-1ubuntu4 500
        500 http://mirrors.aliyun.com/ubuntu xenial/universe amd64 Packages
  1. 要安裝高版本需要使用新的映象源
    但國內訪問deb.nodesource.com會出現連線超時,需要設定代理
    測試結果如下:
$ curl -sL -v https://deb.nodesource.com/setup_8.x
*   Trying 13.35.8.34...
* Connected to deb.nodesource.com (13.35.8.34) port 443 (#0)
* found 148 certificates in /etc/ssl/certs/ca-certificates.crt
* found 592 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* gnutls_handshake() failed: The TLS connection was non-properly terminated.
* Closing connection 0

Step1> 設定代理
以socks5代理為例:

$ export http_proxy=socks5://127.0.0.1:1080
$ export HTTPS_PROXY=socks5://127.0.0.1:1080

Step2> 使用指令碼進行安裝:

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

安裝記錄

## Confirming "xenial" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_8.x/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 8.x LTS Carbon repo...

+ echo 'deb https://deb.nodesource.com/node_8.x xenial main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src https://deb.nodesource.com/node_8.x xenial main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu xenial InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu xenial-backports InRelease              
Hit:4 http://mirrors.aliyun.com/ubuntu xenial-security InRelease               
Hit:5 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial InRelease        
Get:6 https://deb.nodesource.com/node_8.x xenial InRelease [4,646 B]           
Get:7 https://deb.nodesource.com/node_8.x xenial/main Sources [761 B]
Get:8 https://deb.nodesource.com/node_8.x xenial/main amd64 Packages [1,006 B]
Get:9 https://deb.nodesource.com/node_8.x xenial/main i386 Packages [1,004 B]
Fetched 7,417 B in 1s (5,551 B/s)     
Reading package lists... Done

## Run `sudo apt-get install -y nodejs` to install Node.js 8.x LTS Carbon and npm
## You may also need development tools to build native addons:
     sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
     echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
     sudo apt-get update && sudo apt-get install yarn

如果不想使用指令碼,也可以選擇手動安裝,過程如下:

$ sudo apt-get install apt-transport-https

# 使用代理下載nodesource.gpg.key
$ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
OK

# 使用清華映象源
$ sudo bash -c "echo \"deb https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_8.x xenial main\" > /etc/apt/sources.list.d/nodesource.list"
$ sudo bash -c "echo \"deb-src https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_8.x/ xenial main\" >> /etc/apt/sources.list.d/nodesource.list"

$ cat /etc/apt/sources.list.d/nodesource.list 
deb https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_8.x xenial main
deb-src https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_8.x/ xenial main

$ sudo apt-get update
$ sudo apt-get install nodejs

nodejs安裝核心步驟

key相關操作

$ apt-key list
...
pub   4096R/68576280 2014-06-13
uid                  NodeSource <[email protected]>
sub   4096R/AA01DA2C 2014-06-13
...
$sudo apt-key del 68576280
$apt-key list | grep node

參考:

  • https://nodejs.org/zh-cn/download/package-manager/
  • https://mirror.tuna.tsinghua.edu.cn/help/nodesource/