How to Install Node.js on Ubuntu?
There are 3 ways of installing Node.js on Ubuntu Server.
Installing Node.js with Apt from the Default Repositories
Node.js is a part of default repositories of Ubuntu Server and thus may be installed through Apt from default repositories. In such way, the Node.js service is installed with
root permissions which should be changes for security reasons as soon as possible.
sudo apt update- update the package lists for upgrades and updates on your Linux system
sudo apt install nodejs- install Node.js through the apt package manager
node -v- check installed node version (verification, than the node.js was installed)
Change permissions of Node.js service
Installing Node Using the Node Version Manager (Recommended)
Node Version Manager (NVM) is a piece of shell code that allows you to easily install and maintain many different independent versions of Node.js, and their associated Node packages.
- Check latest verision of NVM on GitHub.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh- Check NVM code
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash- install NVM code. The NVM is installed with permissions of a user that processing that request (you). NVM is installed to user account, specifically "~/.bashrc" directory.
source ~/.bashrc- source/reload the script for the option to use it
nvm list-remote- check available Node.js versions
nvm install v18.17.0- install requested version of Node.js, in this case Node Js v18.17.0. Node.js is installed with permissions of a user that processing that request (you)
node -v- show currently active version
As of Node.js is installed for user account only, it is known under
node -v of current user, but not under any other user nor
sudo node -v. There are 2 ways to make
node command (access to teh Node.js client) available also for other users:
There's possible to create a symlink from
/usr/local/bin/nodeto the node client in the user's account
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
- Environment variable with adding to user group (recommended)
When creating native services, you can add there a
Environmentvariable referring to the Node.js client, see
[Unit] Description="My service" Wants=network-online.target After=network-online.target [Service] User=<serviceUser> Group=<serviceUser> Type=simple Restart=always RestartSec=5 WorkingDirectory=/directory/to/service/client Environment="PATH=/home/<userNameWithNodeClient>/.nvm/versions/node/v18.17.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ExecStart=...
Note, to make the
nodeaccessible, the <serviceUser> must be added into the <userNameWithNodeClient> group.
sudo usermod -aG <userNameWithNodeClient> <serviceUser>