Hướng dẫn cài đặt Node.js trên Ubuntu 22.04

Article ID: 1050
Cập nhật gần nhất: 19 Th06, 2024

Hướng dẫn cài đặt Node.js trên Ubuntu 22.04

Đăng nhập vào máy chủ Ubuntu 22.04.

Cài đặt nodejs ổn định từ repository Ubuntu bằng cách chạy lệnh sau.

apt install nodejs

Output:

root@crown:~# apt install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apache2-data apache2-utils
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libc-ares2 libjs-highlight.js libnode72 nodejs-doc
Suggested packages:
  npm
The following NEW packages will be installed:
  libc-ares2 libjs-highlight.js libnode72 nodejs nodejs-doc

Sau đó kiểm tra phiên bản của nodejs sau khi quá trình cài đặt hoàn tất.

root@crown:~# node --version
v14.1.0

Cài đặt phiên bản Node.js khác từ nguồn

Cài đặt trình quản lý NVM bằng cách sử dụng "wget".

root@crown:~# wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 264, done.
remote: Counting objects: 100% (264/264), done.
remote: Compressing objects: 100% (230/230), done.
remote: Total 264 (delta 31), reused 102 (delta 24), pack-reused 0
Receiving objects: 100% (264/264), 116.38 KiB | 3.88 MiB/s, done.
Resolving deltas: 100% (31/31), done.

Cập nhật Shell của bạn ~/.profile bằng cách chạy lệnh sau

source ~/.profile

Tiếp theo, Kiểm tra xem NVM đã được cài đặt hay chưa bằng cách kiểm tra phiên bản của nó.

root@vps:~# nvm --version
0.33.8

Liệt kê tất cả các phiên bản Node.js có sẵn. bạn có thể xem tất cả các phiên bản Node.js từ phiên bản đầu tiên đến phiên bản mới nhất.

root@vps:~# nvm ls-remote
        v0.1.14
        v0.1.15
        v0.1.16
        ....
        v13.12.0
        v13.13.0
        v13.14.0
        v14.0.0
        v14.1.0

Bây giờ hãy cài đặt Node.js với phiên bản bạn muốn bằng cách chạy lệnh "nvm install".

Ví dụ: Cài đăt nvm phiên bản v14.1.0

root@crown:~# nvm install 14.1.0
Downloading and installing node v14.1.0...
Downloading https://nodejs.org/dist/v14.1.0/node-v14.1.0-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v14.1.0 (npm v6.14.4)
Creating default alias: default -> 14.1.0 (-> v14.1.0)

Và kiểm tra phiên bản của nodejs sau khi cài đặt xong.

root@crown:~# node -v
v14.1.0

Bạn đã hoàn thành cài đặt Node.js. Chúc bạn thành công !

Article ID: 1050
Cập nhật gần nhất: 19 Th06, 2024
Lần sửa đổi: 3
Lượt xem: 0