How to install node js on linux

How To Install Node.js on Ubuntu 18.04

Node.js is a JavaScript platform for general-purpose programming that allows users to build network applications quickly. By leveraging JavaScript on both the front and backend, Node.js makes development more consistent and integrated.

In this guide, you’ll learn about three different methods to install Node.js on an Ubuntu 18.04 server.

Prerequisites

This guide assumes that you are using Ubuntu 18.04. Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the initial server setup tutorial for Ubuntu 18.04.

Installing Node.js from Default Repositories with Apt

Ubuntu 18.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is 8.10.0. This will not be the latest version, but it should be stable and sufficient for quick experimentation with the language.

To get this version, you can use the apt package manager. Refresh your local package index:

Verify you’ve installed Node.js successfully by querying node for its version number:

If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you’ll also want to install npm , the Node.js package manager. You can install the npm package with apt :

This will allow you to install modules and packages to use with Node.js.

You’ve now successfully installed Node.js and npm using apt and the default Ubuntu software repositories. However, you may prefer to work with different versions of Node.js, package archives, or version managers. The next steps will discuss these elements, along with more flexible and robust methods of installation.

Installing Node.js with Apt Using a NodeSource PPA

To install a more recent version of Node.js you can add the PPA (personal package archive) maintained by NodeSource. This will have more up-to-date versions of Node.js than the official Ubuntu repositories and will allow you to choose between several available versions of the platform.

First, install the PPA in order to get access to its contents. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace 17.x with your preferred version string (if different):

You can refer to the NodeSource documentation for more information on currently available versions.

If you’d like, you can inspect the contents of this script with nano (or your preferred text editor):

Once you’re satisfied the script is safe to run, exit the text editor. If you used nano , you can exit by pressing CTRL + X . Next, run the script with sudo :

The PPA will be added to your configuration and your local package cache will be updated automatically. Now you can install the Node.js package as you did in the previous section:

Verify you’ve installed the new version by running node with the -v flag:

Читайте также:  Gimp установка плагинов linux

Unlike the one in the default Ubuntu package repositories, this nodejs package contains both node and npm , so you don’t need to install npm separately.

npm uses a configuration file in your home directory to keep track of updates. It will be created the first time you run npm . Run the following command to verify that npm is installed and to create the configuration file:

In order for some npm packages to work (those that require compiling code from source, for example), you need to install the build-essential package:

Now you have the necessary tools to work with npm packages that require compiling code from source.

In this section, you successfully installed Node.js and npm using apt and the NodeSource PPA. Next, you’ll use the Node Version Manager to install and manage multiple versions of Node.js.

Installing Node Using the Node Version Manager

An alternative for installing Node.js is to use a tool called nvm , the Node Version Manager (NVM). Rather than working at the operating system level, nvm works at the level of an independent directory within your home directory. This means that you can install multiple self-contained versions of Node.js without affecting the entire system.

Controlling your environment with nvm allows you to access the newest versions of Node.js and retain and manage previous releases. It is a different utility from apt , however, and the versions of Node.js that you manage with it are distinct from the versions you manage with apt .

To install NVM on your Ubuntu 18.04 machine, visit the project’s GitHub page. Copy the curl command from the README file that displays on the main page to get the most recent version of the installation script.

Before piping the command through to bash , it is always a good idea to audit the script to make sure it isn’t doing anything you don’t agree with. You can do that by removing the | bash segment at the end of the curl command:

Review the output and make sure you are comfortable with the changes it is making. Once you’re satisfied, run the same command with | bash appended at the end. The URL you use will change depending on the latest version of NVM, but as of right now, the script can be downloaded and executed by running the following:

This installs the nvm script to your user account. In order to use it, first source the .bashrc file:

With nvm installed, you can install isolated Node.js versions. First, ask nvm what versions of Node are available:

Output
. v14.18.2 (Latest LTS: Fermium) v15.0.0 v15.0.1 v15.1.0 v15.2.0 v15.2.1 v15.3.0 v15.4.0 v15.5.0 v15.5.1 v15.6.0 v15.7.0 v15.8.0 v15.9.0 v15.10.0 v15.11.0 v15.12.0 v15.13.0 v15.14.0 v16.0.0 v16.1.0 v16.2.0 v16.3.0 v16.4.0 v16.4.1 v16.4.2 v16.5.0 v16.6.0 v16.6.1 v16.6.2 v16.7.0 v16.8.0 v16.9.0 v16.9.1 v16.10.0 v16.11.0 v16.11.1 v16.12.0 v16.13.0 (LTS: Gallium) v16.13.1 (Latest LTS: Gallium) v17.0.0 v17.0.1 v17.1.0 v17.2.0 v17.3.0

It’s a very long list, but you can install a version of Node by inputting any of the released versions listed. For example, to get version v16.13.1, run the following:

Output
Now using node v16.13.1 (npm v8.1.2)

Sometimes nvm will switch to use the most recently installed version. But you can tell nvm to use the version you just downloaded (if different):

Читайте также:  Install flash on linux kali linux

Check the version currently being used by running the following:

If you have multiple Node versions installed, you can run ls to get a list of them:

Output
-> v16.13.1 system default -> v16.13.1 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.13.1) (default) stable -> 16.13 (-> v16.13.1) (default) lts/* -> lts/gallium (-> v16.13.1) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.8 (-> N/A) lts/fermium -> v14.18.2 (-> N/A) lts/gallium -> v16.13.1

You can also default to one of the versions:

Output
default -> 16.13.1 (-> v16.13.1)

This version will be automatically selected when a new session spawns. You can also reference it by the alias like in the following command:

Output
Now using node v16.13.1 (npm v8.1.2)

Each version of Node will keep track of its own packages and has npm available to manage these.

You can also have npm install packages to the Node.js project’s ./node_modules directory. Use the following syntax to install the express module:

Output
added 50 packages, and audited 51 packages in 4s 2 packages are looking for funding run `npm fund` for details found 0 vulnerabilities npm notice npm notice New minor version of npm available! 8.1.2 -> 8.3.0 npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.3.0 npm notice Run npm install -g npm@8.3.0 to update! npm notice

If you’d like to install the module globally, making it available to other projects using the same version of Node.js, you can add the -g flag:

Output
added 50 packages, and audited 51 packages in 1s 2 packages are looking for funding run `npm fund` for details found 0 vulnerabilities

This will install the package in:

Installing the module globally will let you run commands from the command line, but you’ll have to link the package into your local sphere to require it from within a program:

You can learn more about the options available to you with nvm by running the following:

You’ve successfully installed Node by using the Node Version Manager, nvm , to install and manage various versions of Node.

Removing Node.js

You can uninstall Node.js using apt or nvm , depending on the version you want to target. To remove the default repository version, you will use apt at the system level. This command removes the package and retains the configuration files. This is useful if you plan to install the package again in the future:

If you don’t want to save the configuration files for later use, then run the following command to uninstall the package and remove the configuration files associated with it:

As a final step, you can remove any unused packages that were automatically installed with the removed package:

To uninstall a version of Node.js that you have enabled using nvm , first determine whether or not the version you would like to remove is the current active version:

If the version you are targeting is not the current active version, you can run:

Output
Uninstalled node node_version

This command will uninstall the selected version of Node.js.

If the version you would like to remove is the current active version, you must first deactivate nvm to enable your changes:

Читайте также:  Hosts file port linux

Now you can uninstall the current version using the uninstall command used previously. This removes all files associated with the targeted version of Node.js except the cached files that can be used for reinstallment.

Conclusion

There are quite a few ways to get up and running with Node.js on your Ubuntu 18.04 server. Your circumstances will dictate which of the methods is best for your needs. While using the packaged version in Ubuntu’s repository is one method, using nvm or a NodeSource PPA offers additional flexibility.

For more information on programming with Node.js, please refer to our tutorial series How To Code in Node.js.

DigitalOcean provides multiple options for deploying Node.js applications, from our simple, affordable virtual machines to our fully-managed App Platform offering. Easily host your Node.js application on DigitalOcean in seconds.

Tutorial Series: How to Install Node.js and Create a Local Development Environment

Node.js is a JavaScript platform for general-purpose programming that allows users to build network applications quickly. By leveraging JavaScript on both the front and backend, Node.js makes development more consistent and integrated.

To get your development environment configured so you can start building Node.js applications. select the tutorial for your platform.

Источник

Downloading and installing Node.js and npm

To publish and install packages to and from the public npm registry or a private npm registry, you must install Node.js and the npm command line interface using either a Node version manager or a Node installer. We strongly recommend using a Node version manager like nvm to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.

Note: to download the latest version of npm, on the command line, run the following command:

Checking your version of npm and Node.js

To see if you already have Node.js and npm installed and check the installed version, run the following commands:

Using a Node version manager to install Node.js and npm

Node version managers allow you to install and switch between multiple versions of Node.js and npm on your system so you can test your applications on multiple versions of npm to ensure they work for users on different versions.

OSX or Linux Node version managers

Windows Node version managers

Using a Node installer to install Node.js and npm

If you are unable to use a Node version manager, you can use a Node installer to install both Node.js and npm on your system.

If you use Linux, we recommend that you use a NodeSource installer.

OS X or Windows Node installers

If you’re using OS X or Windows, use one of the installers from the Node.js download page. Be sure to install the version labeled LTS. Other versions have not yet been tested with npm.

Linux or other operating systems Node installers

If you’re using Linux or another operating system, use one of the following installers:

Or see this page to install npm for Linux in the way many Linux developers prefer.

Less-common operating systems

For more information on installing Node.js on a variety of operating systems, see this page.

Источник

Оцените статью
Adblock
detector