How To Install Node.js on Ubuntu
Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.
In this guide, we will show you three different ways of getting Node.js installed on an Ubuntu server:
- using apt to install the nodejs package from Ubuntu’s default software repository
- using apt with an alternate PPA software repository to install specific versions of the nodejs package
- installing nvm, the Node Version Manager, and using it to install and manage multiple versions of Node.js
For many users, using apt with the default repo will be sufficient. If you need specific newer (or legacy) versions of Node, you should use the PPA repository. If you are actively developing Node applications and need to switch between node versions frequently, choose the nvm method.
Option 1 — Installing Node.js with Apt from the Default Repositories
Ubuntu 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 12.22.9. 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 first by typing:
sudo apt update
Then install Node.js:
sudo apt install nodejs
Press Y when prompted to confirm installation. If you are prompted to restart any services, press ENTER to accept the defaults and continue. Check that the install was successful by querying node for its version number:
node -v
Outputv12.22.9
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 do this by installing the npm package with apt:
sudo apt install npm
This will allow you to install modules and packages to use with Node.js.
At this point you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.
Option 2 — Installing Node.js with Apt Using a NodeSource PPA
To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v14, v16, and v18 are available as of the time of writing.
First, we will install the PPA in order to get access to its packages. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace20.xwith your preferred version string (if different).
cd ~curl -sL https://deb.nodesource.com/setup_20.x-o nodesource_setup.sh
Refer to the NodeSource documentation for more information on the available versions.
You can inspect the contents of the downloaded script with nano (or your preferred text editor):
nano nodesource_setup.sh
Running third party shell scripts is not always considered a best practice, but in this case, NodeSource implements their own logic in order to ensure the correct commands are being passed to your package manager based on distro and version requirements. If you are satisfied that the script is safe to run, exit your editor, then run the script with sudo:
sudo bash nodesource_setup.sh
The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section. It may be a good idea to fully remove your older Node.js packages before installing the new version, by using sudo apt remove nodejs npm. This will not affect your configurations at all, only the installed versions. Third party PPAs don’t always package their software in a way that works as a direct upgrade over stock packages, and if you have trouble, you can always try to revert to a clean slate.
sudo apt install nodejs
Verify that you’ve installed the new version by running node with the -v version flag:
node -v
Outputv20.18.0
The NodeSource nodejs package contains both the node binary and npm, so you don’t need to install npm separately.
At this point you have successfully installed Node.js and npm using apt and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.
Option 3 — Installing Node Using the Node Version Manager
Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.
To install NVM on your Ubuntu machine, visit the project’s GitHub page. Copy the curl command from the README file that displays on the main page. This will get you 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:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh
Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again 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 typing:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
This will install the nvm script to your user account. To use it, you must first source your .bashrc file:
source ~/.bashrc
Now, you can ask NVM which versions of Node are available:
nvm list-remote
Output. . . v20.15.0 (LTS: Iron) v20.15.1 (LTS: Iron) v20.16.0 (LTS: Iron) v20.17.0 (LTS: Iron) v20.18.0 (Latest LTS: Iron) v21.0.0 v21.0.0 v21.1.0 v21.2.0 v21.3.0 v21.4.9 v21.5.0 v21.6.0 v21.6.1 v21.6.2 v21.7.0 v21.7.1 v21.7.2 v21.7.3 v22.0.0 v22.1.0 v22.2.0 v22.3.0 v22.4.0 v22.4.1 v22.5.0 v22.5.1 v22.6.0 v22.7.0 v22.8.0 v22.9.0
It’s a very long list! You can install a version of Node by typing any of the release versions you see. For instance, to get version v20.18.0 (another LTS release), you can type:
nvm install v20.18.0
You can see the different versions you have installed by typing:
nvm list
Output-> v20.18.0 default -> v20.18.0 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v20.18.0) (default) stable -> 20.18 (-> v20.18.0) (default) lts/* -> lts/iron (-> v20.18.0) 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.12 (-> N/A) lts/fermium -> v14.21.3 (-> N/A) lts/gallium -> v16.20.2 (-> N/A) lts/hydrogen -> v18.20.4 (-> N/A) lts/iron -> v20.18.0
This shows the currently active version on the first line (-> v20.18.0), followed by some named aliases and the versions that those aliases point to.
You can install a release based on these aliases as well. For instance, to install fermium, run the following:
nvm install lts/fermium
OutputDownloading and installing node v14.21.3... Downloading https://nodejs.org/dist/v14.21.3/node-v14.21.3-linux-x64.tar.xz... ################################################################################# 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.21.3 (npm v6.14.18)
You can verify that the install was successful using the same technique from the other sections, by typing:
node -v
Outputv14.21.3
The correct version of Node is installed on our machine as we expected. A compatible version of npm is also available.
Removing Node.js
You can uninstall Node.js using apt or nvm, depending on how it was installed. To remove the version from the system repositories, use apt remove:
sudo apt remove nodejs
By default, apt remove retains any local configuration files that were created since install. If you don’t want to save the configuration files for later use, use apt purge:
sudo apt purge nodejs
To uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:
nvm current
If the version you are targeting is not the current active version, you can run:
nvm uninstallnode_version
OutputUninstalled nodenode_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’ll first need to deactivate nvm to enable your changes:
nvm deactivate
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.
The above is the detailed content of How To Install Node.js on Ubuntu. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.
