Monday, October 1, 2018

JavaScript - Install nodejs and npm in Ubuntu 18.04


On trying to install npm in Ubuntu 18.04, I received the following error:
$ sudo apt install npm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 npm : Depends: node-gyp (>= 0.10.9) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

Hence, I removed the nodejs referring to the following URL:
https://askubuntu.com/questions/626383/node-package-manager-got-corrupted-in-some-way-now-it-cannot-be-installed


To setup nodejs and npm using nvm in ubuntu 18.04:


Step 1: To download the nvm installation script from the project's GitHub page, you can use curl. Note that the version number may differ from what is highlighted here:

$ curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh -o install_nvm.sh

Step 2: Check the contents installation script with nano:

vi install_nvm.sh

Step 3:

$ bash install_nvm.sh

Step 4: It will install the software into a subdirectory of your home directory at ~/.nvm. It will also add the necessary lines to your ~/.profile file to use the file.

To gain access to the nvm functionality, you'll need to either log out and log back in again or source the ~/.profile file so that your current session knows about the changes:

$ source ~/.profile

Step 5: With nvm installed, you can install isolated Node.js versions. For information about the versions of Node.js that are available, type:

$ nvm ls-remote

Step 6: As you can see, the current LTS version at the time of this writing is v8.11.1. You can install that by typing:

$ nvm install 8.11.1

When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:

$ node -v
v8.11.1


$ npm -v
5.6.0



Reference:

2 comments: