今天准备在 ubuntu 服务器里面安装 nodejs 版本,ubuntu 18.04 仓库 nodejs 默认是 8.x 版本。
在 Ubuntu 18.04 的默认仓库包含了一个 Node.js 的版本,截至当前,该仓库的 node.js 版本是 8.10.0 。要安装此版本,你可以使用 apt 包管理器。先刷新你的本地包索引,通过如下命令:
sudo apt update
然后运行安装命令:
sudo apt install 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
执行如下命令:
sudo apt remove nodejs
此命令会卸载 nodejs,但是会保留配置文件,方便你以后再次安装 nodejs。
如果不想保留配置文件,继续执行:
sudo apt purge nodejs
这将会卸载 nodejs 和其相关的配置文件。
最后,你还可以移除和 nodejs 一起安装但是现在没有被使用的包:
sudo apt autoremove
还有一种通过 nvm (Node.js Version Manager) 安装 nodejs 的方法就不介绍了,服务器一般不会同时跑多个 nodejs 版本吧,太容易混乱了。
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 镜像使用帮助。
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 官方安装文档
至此,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
参与评论
手机查看
返回顶部