EC2 Ubuntu Instances failed to run npm install
Let’s fix it …
Today I set up Jenkins instance in a EC2 Ubuntu instance (t2 micro) to run a build for a react project. This project uses npm
as the build tool and following are two simple build steps used.
stage('Build') {
sh 'npm install'
sh 'npm run build'
}
No magic !!! Two simple steps. But it failed :) Jenkins log says that npm install
failed to download the npm packages. Always it failed in the middle of the download. I ran the build several times just to make sure this was not happening due to a random network failure. No, it is the same result. Always failed near the half way mark. A quick google search suggested this could occur because the server might have run out memory.
I did not wait long. Suddenly logged into the EC2 instance and hit the command dmesg
to see the proof. Yes, it is the case.
[68643.046227] Out of memory: Kill process 17133 (npm) score 418 or sacrifice child
[68643.049960] Killed process 17133 (npm) total-vm:1675016kB, anon-rss:431044kB, file-rss:3324k
Next few Google and Stack Exchange searches confirmed that this is a common issue which can be occurred in servers with low memory foot prints specially when long running commands are executed. Is n’t this a series issue? My server memory is not enough to run this process. Don’t I need to increase the memory of this server? Since this is an EC2 instance, I can upgrade my server to the next tier. To be honest, those are the first few thoughts came to my mind when I was reading that Stack Exchange post. But that thought last only till I read the next comment in that thread. @yaplik mentions in the same thread, a simple swap space addition would solve this issue very easily.
How to add a swap space on EC2 Ubuntu Instance?
Following Digital Ocean tutorial explains how to add a swap space on EC2 Ubuntu instance in a very detailed manner covering all the concepts and commands. Thanks Justin Ellingwood !!! you saved my day …