Home >Computer Tutorials >Computer Knowledge >How to install Node.js and NPM in Ubuntu

How to install Node.js and NPM in Ubuntu

王林
王林forward
2024-03-21 09:41:20733browse

How to install Node.js and NPM in Ubuntu

As a developer, it is crucial to know how to install Node.js and NPM. Node.js is one of the leading web technologies and you must ensure that they are installed on your system when creating a local development environment.

Node.js and NPM go hand in hand. Although Node.js is the runtime environment, NPM is its package manager, which you use when you want to install dependencies and packages. So, how to install Node.js and NPM in Ubuntu? We'll cover three methods you can use.

Three ways to install Node.js and NPM in Ubuntu

There are three options for installing Node.js and NPM in Ubuntu. First, you can choose to use the default Ubuntu repositories to install available NPM and Node.js versions. Alternatively, you can use an installation script, which is available from the NodesuberGitHub repository. Finally, you can add the Nodesupgraph key, create a deb repository, and install the latest version or any other supported Node.js version.

Method 1: Using Ubuntu Repository

Both Node.js and NPM packages are available on the official Ubuntu repository. This option is available on Ubuntu 18 to the latest version.

First, update the apt cache.

$sudo apt update

Next, let’s start installing Node.js using the following command:

$sudo apt—get install nodejs

You need to enter your password and press "y" to confirm the installation.

Run the "nodejs-version" command to check what version of Node.js is installed. For this example we have v12.22.9. Of course, this isn't the latest version, that's because we chose to get it from the Ubuntu repository.

For NPM, install it using the following command:

$sudo apt—get install npm—y

After installation, check the NPM version.

This is the first and easiest way to install Node.js and NPM in Ubuntu.

Method 2: Using the installation script

If you visit the NodesubGithub repository, a script has been created to simplify installation on DEB and RPM systems. To use this script you must have root access or use the sudo command.

First, make sure you have curry on your system. You can install it using the following command:

$sudo apt—get install curl

Next, run the "curl" command to download the script.

$sudo curl—SLO www.example.com

Find the downloaded script and use the "chmod" command to grant it 500 file permissions.

$sudo chmod 500 nSolid_Setup_deb.sh

To run this script, you must specify the Node.js version to install. For example, to install Node.js 20, we run the script as follows:

$sudo./nsolid_setup_deb.sh 20

Finally, install Node.js from the configured repository using the "apt" command.

$sudo apt-Get Install NodeJS-y

When we check the installed Node.js and NPM versions, you will notice that we have Node.js v20, which is what we specified earlier. Their respective NPMs are also installed.

If you find this method works well for installing a specific Node.js version, you can use the same steps and then install Node.js. As for NPM, you can't specify its version. It is automatically installed with Node.js.

Method 3: Using NodesomeGPG Key

The final method of installing Node.js and NPM in Ubuntu that we will discuss is using a Nodesource GPG key.

First refresh the apt cache.

Next, install the required packages and dependencies by running the following command:

$sudo apt—get install—y ca—certificates curl gnupg

After installation, download the Nodesource GPG key and create a directory to host it in "/etc/apt". However, use cURL to create a Deb repository for Nodesource. Run the following two commands:

$sudo mkdir-p/etc/apt/keyring

$curl—fsSL www.example.com https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key|

You must then specify the node version NODE_MAJOR to install. For example, let's start by specifying v18.

$NODE_MAJOR=18

Next, use the following "echo" command to create the deb repository:

$sudo apt—get install—y ca—certificates curl gnupg

If you want to have different Node.js versions that you can switch between when building your project, add them as follows:

Likewise, update your repository to accommodate the newly added repository.

$sudo apt—get update

Finally, install Node.js.

$sudo apt-Get Install NodeJS-y

When we check the Node.js version, it should match v18, which is the first NODE_MAJOR we specified earlier.

Now, you have successfully installed Node.js and NPM in Ubuntu.

in conclusion

The method of installing Node.js and NPM depends on your goals and comfort level. There are three methods you can use. You can install Node.js and NPM from the Ubuntu repository. Alternatively, you can use an installation script. The last option is dependent on the NodesomeGPG key. All methods are included in this article.

The above is the detailed content of How to install Node.js and NPM in Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete