I have been through all of the ways to do this, and as of 2022, I can tell you that the best way is to use NVM. You can get the free opensource version managment tool freely by visiting the repository at the link above (click on the title), or you can simply use the following command, which auto-updates your $HOME/.bashrc
configuration. Because your .bashrc
is updated, there is no need to do anything, beyond the curl command below, to install NVM.
NVM via Curl:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Alternatively, if you prefer wget
, use the following...
NVM via Wget
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
At this point you can preform the following commands.
If you don't have NPM you can start with this command
~$> nvm install-latest-npm
The above will upgrade to the latest NPM version for the current node version.
If you have NPM, or already installed NPM as shown above, get the latest node version with the command below.
~/$> nvm install node
the above installs the newest version of node.
If you want the long term support version instead, preform this command
~/$> install --lts
you can switch between the two by using the following:
~/$> use node
-or- ~/$> nvm use --lts
you can also use...
~/$> install 10.1.2
To install a different version, just replace 10.1.2
with the version you want
There is one hiccup that occurs when choosing to use NVM (Node Ver Manager) as your method for installing Node.js, however; it is easily dealt with.
On an Important Side Note:
Many developers struggle with the "hiccup" that I am referencing. Its not a difficult thing to fix, but it is important to know how to fix it so your Linux Operating System & Commands work as you intend for them to (and as you desire for them to).
THE NVM HICCUP
Step #1
When NVM updates & installs Node.js & NPM, the versions that you install, each get placed into the directory @ "$HOME/.nvm/versions/node"
.
The problem with this is even though you use NVM to upgrade Node.js, when you use the command node -v
the console will print the old version, because your binaries haven't been updated with a symbolic link to the new node version.
SOLUTION, Adding upgraded Symbolic Link for Node.js
I suggest that you look at that directory if you have already used NVM to install Node.js &/or NPM, using the common cmd...
~/$> ls ~/.nvm
~/$> ls ~/.nvm/versions
~/$> ls ~/.nvm/versions/node
~/$> ls ~/.nvm/versions/npm
(use this only if you have NPM versions)
Listing the directories will familiarize you with your Linux OS, with NVM, & with Node.js.
Inside the directory, you should have found each version you installed of Node.js & NPM.
Step #2
Currently the node version is 17.5.0 at the time of writing this.
...so, at the time of writing this, when I install the latest Node Version I will perform the next step like this.
~/$> cd ~/.nvm/versions/node/17.5.0
You will want to replace 17.5.0
with what ever version your working with.
You should now have your consoles cmdLine pointed at the current Node.js version's directory that you installed using NVM.
use ~/$> ls .
to veiw the contents.
You should see a bin
dir listed, go ahead and use:
~/$> cd bin
from within the nvm-node directory...
Alternatively you can use the following from whatever directory your in
~/$> cd ~/.nvm/versions/node/17.5.0/bin
Once inside your nodes bin
directory, use ~/$> ls -1 .
\
You should see a couple symbolic links, and a node executable. In my terminal (which uses the standard ANSI Linux color scheme) the node
executable is highlighted green.
It will simply be called node
, but it won't be a directory.
preform this command
~/$> sudo cp ./node /usr/bin/node
That copies the executable, which cannot be linked using the ln -s ...
command into your binaries.
Now if you use node -v
you will have the latest version.