extremelly slow npm install on Ubuntu 20.04 running on WSL2

I'm using npm version 8.1.2 inside Ubuntu 20.04 in windows subsystem for linux 2 (WSL2).

I've been trying to install 5 dependencies for my project, yet the installation hangs.

npm install solc web3 mocha ganache-cli @truffle/hdwallet-provider --verbose

Yet, installation is taking forever (over 10 hours).

I already checked the following:

  1. npm version is up to date, I am using npm version 8.1.2
  2. I already updated the registry to use http instead of https using:
npm config set registry http://registry.npmjs.org/
  1. Checked internet connection which is running fine at 144 Mbps
  2. Tried the same installation in a different linux machine, which ran at the expected speed

Has anyone encountered this issue before? What has been the solution?

PS. There are indeed many topics regarding speed of npm install in stackoverflow, yet, I have already read a lot of them, which led me to testing the 4 topics mentioned before.


Assuming you are not working on files mounted in /mnt then the following worked for me. As a quick test I did wget -O - https://registry.npmjs.org and it was pretty obvious that name resolution was somehow the culprit.

I had the same issue and solved it by creating the /etc/wsl.conf file adding the following:

[network]
generateResolvConf = false

Then I replaced the existing /etc/resolv.conf symlink so you will have to remove it and then recreate it using your favourite text editor adding the following:

nameserver 8.8.8.8

Replace 8.8.8.8 with whatever IP address your DNS server of choice has, if you don't want to use Googles.

When done restart your WSL instance by running wsl --shutdown in an elevated Windows command prompt, then you can start your WSL instance like you normally would.

I don't know why the name resolution is so slow using the stock resolv.conf configuration, but this workaround makes it normal again.