程序开发博客
网络技术与安全

Ubuntu 22.04安装Nodejs最新版教程

Ubuntu 22.04安装Nodejs最新版教程

In this tutorial, we are going to explain in step-by-step detail how to install and use Nodejs on Ubuntu 22.04 OS.

Nodejs is an open-source, cross-platform back-end javascript runtime environment responsible for executing javascript code. Nodejs allows developers to use javascript for server-side scripting and to produce dynamic web pages. Nodejs is developed and maintained by the OpenJS Foundation and written in C, C++, and Javascript.

In this blog post, we are going to explain the three different ways to install NodeJS on your Ubuntu 22.04. Let’s get started!

 

Table of Contents

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Update the System

Fresh installation of Ubuntu 22.04 needs the packages to be updated to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Install Nodejs from Default Ubuntu 22.04 Repository

By default, the nodejs packets are added to the default repository of Ubuntu 22.04. This is the easiest and fastest way to install Nodejs.

To install Nodejs from the default repo, execute the following command:

sudo apt install nodejs -y

After successful installation, you can check the Node version with the following command:

node -v

You should receive the following output:

root@host:~# node -v
v12.22.9

As you can see, the default version of Nodejs that can be installed by the default repository in Ubuntu 22.04 is version v12.22.9

This is not the latest version of Nodejs. So that is why in the next step, we are going to install the latest Nodejs version or a different version using the PPA (personal package archive) method.

Install Nodejs using APT with the PPA method

In this paragraph, we are going to install Nodejs with the NodeSource repository. First, we need to add the repository with the following command:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

Once added, you should receive the following output:

## Installing the NodeSource Node.js 18.x repo...


## Populating apt-get cache...

+ apt-get update
Hit:1 http://us.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (351 kB/s)
Reading package lists... Done

## Confirming "jammy" is supported...

+ curl -sLf -o /dev/null 'https://deb.nodesource.com/node_18.x/dists/jammy/Release'

## Adding the NodeSource signing key to your keyring...

+ curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | tee /usr/share/keyrings/nodesource.gpg >/dev/null

## Creating apt sources list file for the NodeSource Node.js 18.x repo...

+ echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' > /etc/apt/sources.list.d/nodesource.list
+ echo 'deb-src [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x jammy main' >> /etc/apt/sources.list.d/nodesource.list

## Running `apt-get update` for you...

+ apt-get update
Hit:1 http://us.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
Get:3 https://deb.nodesource.com/node_18.x jammy InRelease [4,563 B]
Hit:4 http://us.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:5 http://us.archive.ubuntu.com/ubuntu jammy-backports InRelease
Get:6 https://deb.nodesource.com/node_18.x jammy/main amd64 Packages [771 B]
Fetched 5,334 B in 1s (5,323 B/s)
Reading package lists... Done

## Run `sudo apt-get install -y nodejs` to install Node.js 18.x and npm
## You may also need development tools to build native addons:
     sudo apt-get install gcc g++ make
## To install the Yarn package manager, run:
     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-get update && sudo apt-get install yarn

Update the system and execute the install command:

sudo apt-get update -y

sudo apt-get install nodejs -y

Once installed, check the installed Nodejs version with the node -v command

root@host:~# node -v
v18.7.0

As you can see, the latest installed version with NodeSource repo is version v18.7.0.

Install Nodejs Using the Node Version Manager (NVM)

In this paragraph, first, we will install NVM, then we will install some Nodejs versions.

First, add the repository with the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash

Once the repo is added, we need to source to our .bashrc file.

source ~/.bashrc

Now, you can check the version of the installed NVM with the following command:

nvm -v

You should receive the following output:

root@host:~# nvm -v
0.39.2

The next step is to list all available Nodejs versions with the NVM manager:

nvm list-remote

The list will be long, and the output should be similar to this:

       v16.16.0   (Latest LTS: Gallium)
        v17.0.0
        v17.0.1
        v17.1.0
        v17.2.0
        v17.3.0
        v17.3.1
        v17.4.0
        v17.5.0
        v17.6.0
        v17.7.0
        v17.7.1
        v17.7.2
        v17.8.0
        v17.9.0
        v17.9.1
        v18.0.0
        v18.1.0
        v18.2.0
        v18.3.0
        v18.4.0
        v18.5.0
        v18.6.0
        v18.7.0
        v18.8.0
        v18.9.0
        v18.9.1
        v18.10.0
        v18.11.0
        v18.12.0 (LTS: Hydrogen)
        v18.12.1 (Latest LTS: Hydrogen)
        v19.0.0
        v19.0.1
        v19.1.0

To install some specific versions execute the following command:

nvm install v19.1.0

After successful installation, you should receive the following output:

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

Congratulations! You successfully installed Nodejs on Ubuntu 22.04 in three different ways.

Of course, if you find it difficult to understand the way of installing Nodejs on Ubuntu 22.04, you can always contact our technical support and they will do the rest for you. We are available 24/7.

If you liked this post on how to install Nodejs on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below.

翻译自英文文章:

How to Install Nodejs on Ubuntu 22.04 – RoseHosting

赞(0)
未经允许不得转载:瓦普技术博客 » Ubuntu 22.04安装Nodejs最新版教程

互联网技术动态与开发

网站搭建技术,程序开发与代码,一个分享网络技术代码VPS服务器的技术博客网站。

联系我们关于我们