Using NPM without SUDO command2 min read


How do i setup npm not to use sudo credentials on Linux (Ubuntu)?

There are several ways to do this but the easiest way is to use NVM.

What is NVM?

NVM stands for “Node.js Version Manager” and is used to install multiple, self-contained versions of Node.js.
That means you can have multiple Node.js environments which are easy to access and switch between.

Why should i use NVM?

Well, you might have an app which is dependent on older version of Node.js and simultaneously you might want to develop and test new features on a new version of Node.js.
This way is easier to switch between environments.
Plus, all npm installations are used without SUDO command.

HowTo?

Dead simple! Just follow these two steps:

Step 1:

Update software packages from Ubuntu repositories by doing:

 
• sudo apt-get update • sudo apt-get install build-essential libssl-dev

Step 2:

Install via curl or wget using this script (version number might vary):

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash  OR 
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

This will install software into a subdirectory of your home directory at ~/.nvm and will add necessary lines to your ~/.profile file to use the file.
With these few lines you will have your NVM installed.
Next is to list all node versions, install the ones you will use and switch between them.

Using NVM:

To list all available Node.js versions ready for installation use:

• nvm ls-remote

To install latest Node.js version use:

• nvm install

To install specific Node.js version use:

• nvm install 7.0.0

To use specific Node.js version use:

• nvm use 7.0.0

To list all installed Node.js versions use:

• nvm ls

To use specific Node.js version as default one, use:

• nvm alias default 7.0.0

Each Node.js version has it’s own packages and own npm to handle it.

If you wish to install globally some package which will then be available for all Node.js versions, simply use parameter ‘-g‘ when installing it.

For more stuff  you can always ask for a help:

• nvm help

Conclusion:

As you can see, using NVM gives you enormous flexibility and as a side effect you don’t have to use SUDO command to install anything regarding Node.js.

So this is definitely the easiest way to use  npm without sudo!

Have fun coding!

Leave a Reply

Your email address will not be published. Required fields are marked *