依木食品商场 發表於 2020-3-3 15:03:00

Ubuntu中安装最新 Node.js 和 npm

<h2 id="背景">背景</h2>
<p>为了安装GNU QEMU Eclipse,需要安装比较新的nodejs</p>
<pre><code class="language-bash">$ npm install --global xpm@latest
▌ ╢░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░╟
WARN engine xpm@0.5.0: wanted: {"node":"&gt;7.7.0"} (current: {"node":"4.2.6","npm":"3.5.2"})

</code></pre>
<blockquote>
<p>参考:Ubuntu18安装最新版nodejs、Ubuntu16.04安装最新版nodejs</p>
</blockquote>
<h2 id="更新ubuntu软件源">更新ubuntu软件源</h2>
<pre><code class="language-bash"># Ubuntu 16.04 TLS,执行以下命令:
sudo apt-get update
sudo apt-get install -y python-software-properties software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update


# Ubuntu 18.04 TLS,执行以下命令:
sudo apt-get update
sudo apt-get install -y software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
</code></pre>
<h2 id="安装nodejs和npm">安装NodeJS和NPM</h2>
<h3 id="安装命令">安装命令</h3>
<pre><code class="language-bash"># Ubuntu 16.04 TLS,执行以下命令:
sudo apt-get install nodejs
sudo apt install nodejs-legacy
sudo apt install npm


# Ubuntu 18.04 TLS,执行以下命令:
sudo apt-get install nodejs
sudo apt install libssl1.0-dev nodejs-dev node-gyp npm
</code></pre>
<h3 id="配置路径">配置路径</h3>
<h4 id="修改prefixcache路径">修改prefix、cache路径</h4>
<pre><code>mkdir nodejs的安装路径/node_cache
mkdir nodejs的安装路径/node_global
npm config set prefix "nodejs的安装路径/node_global"
npm config set cache "nodejs的安装路径/node_cache"
</code></pre>
<p>在自己的环境变量中添加</p>
<pre><code class="language-bash">#node
export NODE_HOME=nodejs的安装路径
export PATH=$NODE_HOME/bin:$PATH

#npm
export NODE_PATH=nodejs的安装路径/node_global/node_modules
</code></pre>
<h3 id="更新npm的包镜像源">更新npm的包镜像源</h3>
<p>方便快速下载</p>
<pre><code class="language-bash">sudo npm config set registry https://registry.npm.taobao.org
sudo npm config list
</code></pre>
<h3 id="安装n管理器用于管理nodejs版本">安装n管理器(用于管理nodejs版本)</h3>
<pre><code class="language-bash">sudo npm install n -g

# 安装最新的nodejs(stable版本)
sudo n stable
</code></pre>
<h3 id="验证安装">验证安装</h3>
<p>使用下面的命令查看NodeJS和NPM的版本,若正确安装的话都可以看到相关的版本号信息。</p>
<pre><code class="language-bash">sudo node -v
sudo npm -v
</code></pre>
<p>安装完最好重启一下:<code>sudo reboot</code>或者重启<code>shell</code></p>
<h2 id="q--a">Q &amp; A</h2>
<h3 id="验证安装查看的版本号时加sudo和不加sudo的命令结果不一样">验证安装查看的版本号时,加sudo和不加sudo的命令,结果不一样</h3>
<blockquote>
<p>这应该是终端或远程连接终端的缓存问题,加sudo命令看到的是最新的版本号,不加sudo命令看到的是前一安装版本的版本号,关闭相关终端或远程连接终端,然后重新连接即可解决这个问题。</p>
</blockquote>
<h3 id="apt-get-update找不到文件">apt-get update找不到文件</h3>
<pre><code>// error code
W: The repository 'http://ppa.launchpad.net/chris-lea/node.js/ubuntu xenial Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: Failed to fetch http://ppa.launchpad.net/chris-lea/node.js/ubuntu/dists/xenial/main/binary-amd64/Packages404Not Found
E: Some index files failed to download. They have been ignored, or old ones used instead.

</code></pre>
<blockquote>
<p>// A: 修改node相关文件:/etc/apt/sources.list.d/chris-lea-ubuntu-node_js-xenial.list<br>
deb http://ppa.launchpad.net/chris-lea/node.js/ubuntu trusty main<br>
deb-src http://ppa.launchpad.net/chris-lea/node.js/ubuntu trusty main</p>
</blockquote>
<h3 id="node--v异常">node -v异常</h3>
<pre><code>node -v
-bash: /usr/local/bin/node: Permission denied

sudo node -v
Segmentation fault (core dumped)
</code></pre>
<blockquote>
<p>// A: 重新安装nodejs<br>
手动移除 /usr/local/lib/node_modules整个目录<br>
手动移除 /usr/local/bin/node<br>
手动移除 /usr/local/bin/n<br>
手动移除 /usr/local/bin/npm</p>
</blockquote>
<h3 id="package-software-properties-common-has-no-installation-candidate异常"><code>Package 'software-properties-common' has no installation candidate</code>异常</h3>
<pre><code class="language-bash">sudo apt-get install -y python-software-properties software-properties-common

E: Package 'python-software-properties' has no installation candidate
</code></pre>
<blockquote>
<p>原因详见 https://askubuntu.com/questions/422975/e-package-python-software-properties-has-no-installation-candidate?rq=1 ,调整为<code>sudo apt-get install -y software-properties-common</code>即可</p>
</blockquote>


</div>
<div id="MySignature" role="contentinfo">
    如果说我的文章对你有用,只不过是我站在巨人的肩膀上再继续努力罢了。<br>若在页首无特别声明,本篇文章由 Schips 经过整理后发布。<br>博客地址:https://www.cnblogs.com/schips/<br><br>
来源:https://www.cnblogs.com/schips/p/ubuntu_install_newest_node-js_and_npm.html
頁: [1]
查看完整版本: Ubuntu中安装最新 Node.js 和 npm