For WSL2, the issue was resolved by setting up a .wslconfig file & assigning specific amount of memory to WSL2. I allocated 8 GB & it worked. I also set the MAX_JOBS environment variable to a low value.