How to Install Node.js on Ubuntu 22.10

Login to the Ubuntu 22.10 server.

Install stable nodejs from the Ubuntu repository by running the following command.

apt install nodejs

Output:

root@crown:~# apt install nodejs
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libevent-pthreads-2.1-7 libmecab2 libprotobuf-lite23 mecab-ipadic
  mecab-ipadic-utf8 mecab-utils
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libc-ares2 libnode108 nodejs-doc
Suggested packages:
  npm
The following NEW packages will be installed:
  libc-ares2 libnode108 nodejs nodejs-doc
0 upgraded, 4 newly installed, 0 to remove and 1 not upgraded.
Need to get 15.0 MB of archives.

Then check the version of the nodejs once the installation is done.

node --version

Output:

root@crown:~# node --version
v18.7.0

Installing a different version of Node.js from the source

Install NVM manager by using "wget".

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

Output:

root@crown:~# wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 264, done.
remote: Counting objects: 100% (264/264), done.
remote: Compressing objects: 100% (230/230), done.
remote: Total 264 (delta 31), reused 100 (delta 24), pack-reused 0
Receiving objects: 100% (264/264), 116.37 KiB | 5.54 MiB/s, done.
Resolving deltas: 100% (31/31), done.
Note: switching to '7ad6d98cedde01809e32d56ab8ced064f6f28175'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain the commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> 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

Update your shell ~/.profile by running following command

source ~/.profile

Next, Check if NVM is installed or not by checking for its version.

nvm --version

Output:

root@crown:~# nvm --version
0.33.8

List out all available Node.js versions. you can see all Node.js versions from the first version to the latest version.

nvm ls-remote

Output:

root@vps:~# nvm ls-remote
        v0.1.14
        v0.1.15
        v0.1.16
        ....
        v13.12.0
        v13.13.0
        v13.14.0
        v14.0.0
        v14.1.0
  v15.0.0
  v15.0.1
  v15.1.0
  v15.3.0
  v16.3.0
  v16.4.0
  v16.4.1
  v17.9.0
  v17.9.1
  v18.0.0
  v18.11.0

Now install Node.js with the version you want by running "nvm install" command.

eg: Here 18.11.0

 nvm install 18.11.0

Output:

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

And check the version of the nodejs once the installation is done.

node -v

Output:

root@crown:~# node -v
v18.11.0

Done!