Install Node.js 20 LTS on Ubuntu 22.04|20.04|18.04

Thứ tư - 13/12/2023 11:07 1.314 0
Node.js là môi trường thời gian chạy JavaScript phía máy chủ đa nền tảng và miễn phí sử dụng được tạo từ công cụ JavaScript V8 của Google được sử dụng trong trình duyệt Google Chrome.
jqgeXTBpos5dtMvCD90Qe7za8YIf2gXavtwpb8AU
jqgeXTBpos5dtMvCD90Qe7za8YIf2gXavtwpb8AU

Step 1: Add Node.js 20 APT repository

Update your packages index.

sudo apt update

Import repository GPG key:

sudo apt install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

Add Node.JS 20 LTS APT repository.

NODE_MAJOR=20
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Update package index.

$ sudo apt update
Hit:1 https://mirror.hetzner.com/ubuntu/packages jammy InRelease
Hit:2 https://mirror.hetzner.com/ubuntu/packages jammy-updates InRelease
Hit:3 https://mirror.hetzner.com/ubuntu/packages jammy-backports InRelease
Hit:4 https://mirror.hetzner.com/ubuntu/security jammy-security InRelease
Get:5 https://deb.nodesource.com/node_20.x nodistro InRelease [12.1 kB]
Get:6 https://deb.nodesource.com/node_20.x nodistro/main amd64 Packages [3,449 B]
Fetched 3,449 B in 1s (4,242 B/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
44 packages can be upgraded. Run 'apt list --upgradable' to see them.

Step 2: Install Node.js, npm and yarn

The repository we just configured contains Node.js binary distributions via .deb. We can install Node.js 20.x and npm by running the following commands:

sudo apt install -y nodejs

You can also install development tools for building native addons:

sudo apt install gcc g++ make -y

If you want to install Yarn package manager, run the commands given below:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt-get install yarn -y

Verify install

$ node -v
v20.8.1

$ npm -v
10.1.0

$ yarn -v
1.22.19

Step 3: Using Node.js 20 LTS

Node.js enables you to create server-side applications using JavaScript. Let’s create our development environment to demonstrate how to create and run Node.js web application.

Create a new directory for the application.

cd ~/
mkdir mynode-app && cd mynode-app

Initialize the project by creating a package.json file which contains the metadata about the application and its dependencies required to run.

npm init

Follow the prompts to fill in the required information for the application. Confirm the file is created.

$ ls
package.json

In our application we’ll use Express framework. Let’s install this dependency – the --save option is for adding the dependency to package.json file.

$ npm install express --save

added 57 packages, and audited 58 packages in 3s

7 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Next we create a new file called app.js in the same project directory which contain the code for the web application.

vim app.js

Add the following code to create a basic Express application:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello, Node World!');
});

app.listen(3000, () => {
  console.log('App listening on port 3000!');
});

The code above creates an Express application listening for requests on port 3000. When a request is made to the root URL (/), the application sends a “Hello, Node World!” message to the client.

Run the following command to run the application.

node app.js

On a separate terminal curl IP address and port 3000 to confirm the app works.

$ curl 127.0.0.1:3000
Hello, Node World!

You should see the “Hello, Node World!” message displayed in your browser.

Conclusion

This guide has demonstrated how you can start the installation of Node.js 20 LTS on Ubuntu Linux system. As seen in this article, the process is straightforward and doesn’t required any special Linux skills. By following and customizing on the steps provided, you should be able to develop more complex applications by adding extra functionality and features. We hope this article was of great help. Follow us on our social media platforms for more juicy updates.

Tổng số điểm của bài viết là: 0 trong 0 đánh giá

Click để đánh giá bài viết
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây