ubuntu18.04安裝nodejs最新版、指定版 12.x 14.x


今天准備在 ubuntu 服務器里面安裝 nodejs 版本,ubuntu 18.04 倉庫 nodejs 默認是 8.x 版本。

1. 通過 apt 安裝 nodejs

在 Ubuntu 18.04 的默認倉庫包含了一個 Node.js 的版本,截至當前,該倉庫的 node.js 版本是 8.10.0 。要安裝此版本,你可以使用 apt 包管理器。先刷新你的本地包索引,通過如下命令:

sudo apt update

然后運行安裝命令:

sudo apt install nodejs

2. 通過 PPA 安裝指定或最新版本的 nodejs

那么就需要使用 nodesource 來安裝指定版本的 nodejs 了。其需要下載一個腳本,運行此腳本會在 ubuntu 里添加一個 nodejs 源,然后用 apt 就可以下載指定的 nodejs 了。

PPA 的全稱為 personal package archive 。要安裝 nodejs 12.x 版本,可以運行如下命令:

cd ~
curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo apt update
sudo apt install nodejs

 要安裝 nodejs 最新版本,可以運行如下命令:

cd ~
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt update
sudo apt install nodejs

3. 如何卸載 nodejs

執行如下命令:

sudo apt remove nodejs

此命令會卸載 nodejs,但是會保留配置文件,方便你以后再次安裝 nodejs。

如果不想保留配置文件,繼續執行:

sudo apt purge nodejs

這將會卸載 nodejs 和其相關的配置文件。

最后,你還可以移除和 nodejs 一起安裝但是現在沒有被使用的包:

sudo apt autoremove

4. 其他安裝 nodejs 的方式

還有一種通過 nvm (Node.js Version Manager) 安裝 nodejs 的方法就不介紹了,服務器一般不會同時跑多個 nodejs 版本吧,太容易混亂了。

5. 清華大學 nodesource 鏡像源使用方法

nodesource 有時候非常慢,建議使用清華大學的鏡像源。

sudo vim /etc/apt/sources.list.d/nodesource.list

把 https://deb.nodesource.com/node_12.x/
替換為 https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb_12.x/ 。

或者:

把 https://deb.nodesource.com/node/
替換為 https://mirrors.tuna.tsinghua.edu.cn/nodesource/deb/ 。

可以直接參考 清華大學源 nodesource 鏡像使用幫助

6. 安裝 yarn 包管理器

nodejs 自帶 npm 不太好用,建議安裝 yarn 代替。

執行:

curl -sS 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 update
sudo apt install yarn

也可以直接參考 yarn 官方安裝文檔

7. 結束語

至此,ubuntu 安裝 nodejs 就完成了,服務器可以愉快的跑 node 了。

--End--

參考資料:
https://github.com/nodesource/distributions
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04
https://mirrors.tuna.tsinghua.edu.cn/help/nodesource
https://classic.yarnpkg.com/zh-Hans/docs/install#debian-stable

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM