Introduction of Node.js
At the present time Node.js is a popular runtime environment for executing JavaScript on the server-side. It’s a key tool for web development and is known for its speed and versatility. For that reason this user-friendly step-by-step guide will walk you through the install Node.js on Ubuntu 22.04 With 3 Different Ways. Whether you’re an experienced developer or a beginner, you’ll have your Node.js environment up and running in no time. Let’s get started!.
As I have said, we’ll take you step by step through the process of installing Node.js on Ubuntu 22.04. We’ll cover three different methods: the Default repository, the NodeSource repository, and the Node Version Manager (NVM).
Why Node.js on Ubuntu 22.04?
Before we dive into the installation process, let’s briefly discuss why Node.js on Ubuntu 22.04 is an excellent choice. Node.js allows you to create scalable and efficient server-side applications, making it a top choice for web development. Ubuntu 22.04, on the other hand, is renowned for its user-friendliness and long-term support, making it a reliable platform for hosting Node.js applications.
Table of Contents
- Introduction of Node.js
- Why Node.js on Ubuntu 22.04?
- Key features of Node.js
- Prerequisites for Install Node.js
- Conclusion
Key features of Node.js
Node.js is well-known for its event-driven, non-blocking I/O model, which makes it efficient and lightweight for building real-time applications and server-side APIs. Key features of Node.js include:
- Asynchronous and Non-blocking: Node.js operates using an event loop, which allows it to handle multiple requests concurrently without blocking the execution of other code. This non-blocking I/O model makes it suitable for building fast and responsive applications.
- Server-side Development: Node.js is commonly used for server-side development to build web servers, APIs, and backend services.
- Package Manager (npm): Node.js comes with npm (Node Package Manager), which is one of the largest package repositories in the world. It allows developers to install, share, coupled with manage third-party libraries and packages needed for their projects.
- Scalability: Node.js is designed to handle a large number of concurrent connections efficiently, making it suitable for building scalable applications, real-time chat applications, gaming servers, and other applications that require high concurrency.
- Community and Ecosystem: Node.js has a vibrant and active community that continuously contributes to the growth of the platform. This has led to a rich ecosystem of libraries, frameworks, and tools that developers can leverage especially to streamline their development process.
Node.js has become incredibly popular for modern web development, especially for applications requiring real-time interactions, such as chat applications, streaming services, and online gaming platforms. Its performance, scalability, and extensive package ecosystem have made it a favorite among developers for building a wide range of applications and services.
Prerequisites for Install Node.js
Important to realize this guide assumes that you are using Ubuntu Server 22.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 :
System Update for Install Node.js
Basically It’s always a good practice to start by updating your package repository and upgrading installed packages to their latest versions. Update the package lists by running the following command:
samm@nodejs:~$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu jammy InRelease
Hit:2 http://id.archive.ubuntu.com/ubuntu jammy InRelease
Hit:3 http://id.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://id.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:5 http://id.archive.ubuntu.com/ubuntu jammy-security InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
21 packages can be upgraded. Run 'apt list --upgradable' to see them.
samm@nodejs:~$ sudo apt upgrade
In this guide, given these points you will review three different ways of getting Node.js installed on a Ubuntu Server 22.04:
- Using
apt
to install thenodejs
package from default software repository. - Using
apt
with theNodesource
software repository to install specific versions of thenodejs
package - Installing
nvm
, the Node Version Manager, and using it to install and manage multiple versions of Node.js
Method 1: Install Node.js with apt from the Default Repositories
Basically Ubuntu 22.04 already comes with a version of Node.js in its default repositories. This is pretty handy because it ensures a uniform experience when using Node.js on various systems. As of now, the version available in these repositories is v12.22.9. It might not be the absolute latest, but it’s reliable and perfectly suitable for some fast experimentation with the language.
Install Node.js with Default Repositories
To install Node.js and npm, you can use the official Ubuntu repository. To get this version, you can use the apt package manager by running the following command:
samm@nodejs:~$ sudo apt install nodejs npm
Verify Node.js Installation
Check that the install was successful by querying node for its version number using following command:
samm@nodejs:~$ node -v
Output
v12.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. The Node.js package from Ubuntu Server 22.04’s default repositories also comes with npm, the Node.js package manager. 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 dnf and the default Ubuntu software repositories. Henceforth the next section will show you how to use an alternate repository to install different versions of Node.js.
Method 2: Install Node.js from NodeSource Repository
Basically Nodesource is a Node.JS partner that focusses on helping enterprises to run production-ready Node.Js applications with performance, stability and security in mind. This method is suitable if you want to install newer versions on Node.JS and NPM.
Add the NodeSource Repository:
For the most part NodeSource provides and installation script that updates the repository and adds the Nodesource repository to the sources list. In detail about the available versions, check out the NodeSource documentation. First, you’ll need to configure the repository locally, in order to get access to its packages. So download and run the script using the curl command as follows:
samm@nodejs:~$ curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Then you should see the output similar to the following:
## Installing the NodeSource Node.js 18.x repo...
## Populating apt-get cache...
+ apt-get update
Hit:1 https://download.docker.com/linux/ubuntu jammy InRelease
Hit:2 http://id.archive.ubuntu.com/ubuntu jammy InRelease
Hit:3 http://id.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://id.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:5 http://id.archive.ubuntu.com/ubuntu jammy-security InRelease
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
Get:1 https://deb.nodesource.com/node_18.x jammy InRelease [4,563 B]
Hit:2 https://download.docker.com/linux/ubuntu jammy InRelease
Hit:3 http://id.archive.ubuntu.com/ubuntu jammy InRelease
Hit:4 http://id.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:5 http://id.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:6 http://id.archive.ubuntu.com/ubuntu jammy-security InRelease
Get:7 https://deb.nodesource.com/node_18.x jammy/main amd64 Packages [775 B]
Fetched 5,338 B in 1s (4,715 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
Install Node.js Using the NodeSource
When you try to install nodejs with nodesource, you will get thihs error because you have existing nodejs.
(Reading database ... 101348 files and directories currently installed.)
Preparing to unpack .../nodejs_18.17.0-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (18.17.0-deb-1nodesource1) over (12.22.9~dfsg-1ubuntu3) ...
dpkg: error processing archive /var/cache/apt/archives/nodejs_18.17.0-deb-1nodesource1_amd64.deb (--unpack):
trying to overwrite '/usr/include/node/common.gypi', which is also in package libnode-dev 12.22.9~dfsg-1ubuntu3
dpkg-deb: error: paste subprocess was killed by signal (Broken pipe)
Errors were encountered while processing:
/var/cache/apt/archives/nodejs_18.17.0-deb-1nodesource1_amd64.deb
needrestart is being skipped since dpkg has failed
E: Sub-process /usr/bin/dpkg returned an error code (1)
The repository 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-get remove nodejs npm
.
This won’t change your settings in any way; but only update the installed software versions. Must be remembered that third-party repositories may not always package their software in a manner that seamlessly upgrades over the default packages. If you encounter any issues, for this reason you can always go back to the original, unmodified setup as a fallback option.
samm@nodejs:~$ sudo apt-get remove nodejs
samm@nodejs:~$ sudo apt-get purge nodejs
samm@nodejs:~$ sudo apt-get autoremove
After fully remove your older Node.js packages, next install the new version of Node.js using following command:
samm@nodejs:~$ sudo apt-get install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
nodejs
0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
Need to get 0 B/28.9 MB of archives.
After this operation, 188 MB of additional disk space will be used.
Selecting previously unselected package nodejs.
(Reading database ... 80313 files and directories currently installed.)
Preparing to unpack .../nodejs_18.17.0-deb-1nodesource1_amd64.deb ...
Unpacking nodejs (18.17.0-deb-1nodesource1) ...
Setting up nodejs (18.17.0-deb-1nodesource1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning candidates...
Scanning linux images...
Verify Node.js Installation
After that verify that you’ve installed the new version, by running following command:
samm@nodejs:~$ node --version
Output
v18.17.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 dnf and the NodeSource repository. Finally the next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.
Method 3: Install Node Using the Node Version Manager (NVM)
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.
Install NVM
Before install NVM on your Ubuntu Server 22.04 machine, visit the project’s GitHub page. This will get you the most recent version of the installation script. 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:
samm@nodejs:~$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
You will be some bunch of output on the terminal as shown.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15037 100 15037 0 0 41154 0 --:--:-- --:--:-- --:--:-- 41197
=> Downloading nvm from git to '/home/samm/.nvm'
=> Cloning into '/home/samm/.nvm'...
remote: Enumerating objects: 360, done.
remote: Counting objects: 100% (360/360), done.
remote: Compressing objects: 100% (306/306), done.
remote: Total 360 (delta 40), reused 167 (delta 28), pack-reused 0
Receiving objects: 100% (360/360), 220.04 KiB | 4.31 MiB/s, done.
Resolving deltas: 100% (40/40), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository
=> Appending nvm source string to /home/samm/.bashrc
=> Appending bash_completion source string to /home/samm/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:
/usr/lib
├── [email protected]
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:
$ nvm use system
$ npm uninstall -g a_module
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Source NVM
Next, run the following commands to load nvm:
samm@nodejs:~$ export NVM_DIR="$HOME/.nvm"
samm@nodejs:~$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
samm@nodejs:~$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Verify the installed nvm version using following command:
samm@nodejs:~$ nvm -v
0.39.1
To list all the available versions of Node.js using NVM, run the command:
samm@nodejs:~$ nvm ls-remote
Output
. . .
v18.10.0
v18.11.0
v18.12.0 (LTS: Hydrogen)
v18.12.1 (LTS: Hydrogen)
v18.13.0 (LTS: Hydrogen)
v18.14.0 (LTS: Hydrogen)
v18.14.1 (LTS: Hydrogen)
v18.14.2 (LTS: Hydrogen)
v18.15.0 (LTS: Hydrogen)
v18.16.0 (LTS: Hydrogen)
v18.16.1 (LTS: Hydrogen)
v18.17.0 (Latest LTS: Hydrogen)
. . .
As can be seen it’s a very long list! Afterwards you can install a version of Node by typing any of the release versions you see.
Install Node.js with NVM
For instance, to get version v18.1.0 (an LTS release), you can type:
samm@nodejs:~$ nvm install 18.1.0
You should then see the output similar to the following:
Downloading and installing node v18.1.0...
Downloading https://nodejs.org/dist/v18.1.0/node-v18.1.0-linux-x64.tar.xz...
##################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.1.0 (npm v8.8.0)
Verify Node.js Installation
You can see the different versions you have installed by typing following command:
samm@nodejs:~$ nvm list
-> v18.1.0
v20.5.0
system
default -> node (-> v20.5.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v20.5.0) (default)
stable -> 20.5 (-> v20.5.0) (default)
lts/* -> lts/hydrogen (-> N/A)
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.1 (-> N/A)
lts/hydrogen -> v18.17.0 (-> N/A)
This shows the currently active version on the first line (-> v18.1.0), followed by some named aliases and the versions that those aliases point to.
Conclusion
Finally Congratulations! You’ve install Node.js with a few ways on your Ubuntu Server 22.04. Thus you’re now ready to develop and deploy powerful web applications using Node.js. First thing to remember this installation provides a solid foundation for creating high-performance websites, and you’re well on your way to leveraging the capabilities of this powerful runtime. Happy coding!
Also Read Our Other Guides :
- How To Install Node.js on Rocky Linux 9 With 3 Different Ways
- How To Install and Setup Node.js on Debian 11
- How To Install and Configure Go (Golang) on Ubuntu 22.04
- How To Install and Configure NFS Server Client on Ubuntu 22.
- How To Install Vagrant on Ubuntu 22.04
- How To Install VirtualBox 7.0 on Ubuntu 22.04
As I have shown, now you have learned how to install Node.js on your Ubuntu 22.04 Server.