NodeJS is the most popoular JavaScirpt runtime environment in the development world. There are also a bunch of nodejs alternatives like Deno and Bun. But today we will be focusing on nodejs only.
There are several ways to install nodejs in a Linux based system:
Using your package manager:
This is the most straightforward and easiest option too install nodejs in your system. In my case, I am using Ubuntu (Debian based) systems, so I will use apt
to install my nodejs.
sudo apt install nodejs
But this has a limitation: the latest version of nodejs for apt
is v12.22.12, which is nowhere near the latest LTS, so this version is very behind and we may require a newer version of nodejs to run our JavaScript program. Which is where the next method comes in:
Using Node Version Manager (NVM):
NVM is a very useful CLI tool to manage mutlple versions of nodejs. It installs the nvm
binary for the current user, which can easily manage node installations using simple commands.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
The above command downloads an installation script for nvm
and executes the script as a bash shell.. It also attempts to add some path to your .bashrc
file if you are using bash
. But if you are using oh-my-zsh
for your zsh
plugins management, then you just need to add nvm
among the other enabled plugins list in your ~/.zshrc
file.
Here are some nvm
commands to mange your nodejs installations:
nvm list-remote # Lists available remote sources.
nvm install v16.14.0 # Install a specific version.
nvm list # List all available versions (including remote and installed).
nvm install lts/iron # You can also install it this way using the codename.
nvm uninstall <codename/version_no> # Uninstall a specific version.
nvm use <codename/version_no> # Switch to a specific version.
Using asdf:
This is basically the same with nvm
, but if you use other multi-versioned tools, (say Flutter) we can also use asdf
to mange Flutter and NodeJs versions at the same time.
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0
Clone asdf
to use it, but you need to add some lines to your .bashrc
or .zshrc
in order for your shell to remember the asdf
binary:
# FOR BASH SHELL in ~/.bashrc file:
. "$HOME/.asdf/asdf.sh"
. "$HOME/.asdf/completions/asdf.bash"
# FOR ZSH in the ~/.zshrc file
. "$HOME/.asdf/asdf.sh"# append completions to fpath
fpath=(${ASDF_DIR}/completions $fpath)
# initialise completions with ZSH's compinit
autoload -Uz compinit && compinit
The commands for asdf
are basically the same with nvm
and you can read more about it here.
Manually downloading pre-built binary:
This cannot be very beginner-friendly because it involves using the wget command and symlinking some binaries to different directories. If you are familiar and comfortable doing these things, then it will be an easy instruction to follow.
First we will visit NodeJS website and go to the 'Downloads' tab. There, select the 'Prebuilt Binaries' and select the version of nodejs you want on the dropdown menu. Select your operating system and CPU architecture. If you don't know your CPU architecture, it's probably x86-64
.
Now you can download that binary via browser into the Downloads folder or you can right click and copy the download link as well. If you have downloaded via browser, locate where the compressed file is downloaded using the terminal.
It is mostly in the Downloads
directory, so we will move it into /opt
directory. But before that, lets decompress the tarball first:
tar -xvf <your_downloaded_node>.tar.gz
sudo mv <your_downloaded_node_dir> /opt/
Now we will symlink all the node and npm binaries to /usr/local/bin
:
sudo ln -s /opt/node-v**/bin/* /usr/local/bin/
Replace the * with your actual version of node.
If you are using wget
to copy the download link, navigate to the /opt
directory and type wget
and then paste the copied link. Wget will download the node binary tarball for you. After it has finished downloading, decompress it with the tar
command above and also symlink the binaries to /usr/local/bin
as above.
You can download multiple versions of nodejs and store it in the /opt
directory. The only downside is that you have to manually add or remove the desired version of nodejs you want to use (since you have to symlink eveything manually).
Besides that, it is a very lightweight and convienient way to use nodejs.
Conclusion
So, how do you install nodejs in your system? Is there a better and easier approach you can share? Let me know. Also if you find this helpful, don't forget to share..
Thank you for reading..