Running npm install on a Server with 1GB Memory using Swap
Hello and welcome back to “Continuous Improvement,” the podcast where we dive into the intricacies of optimizing performance, whether it’s in life, work, or tech. I’m your host, Victor Leung, and today, we’re tackling a common challenge for those working with limited server resources: running npm install
on a server with just 1GB of memory. Yes, it can be done smoothly, and swap space is our savior here.
So, what exactly is swap space, and how can it help? Think of swap space as an overflow area for your RAM. When your server’s physical memory gets filled up, the system can move some of the inactive data into this swap space on your hard disk, freeing up RAM for more critical tasks. It’s slower than RAM, but it can prevent those dreaded out-of-memory errors that can crash your operations.
Let’s walk through how to set up and optimize swap space on your server.
First, you’ll want to see if swap space is already configured. You can do this with the command:
sudo swapon --show
This command will display any active swap areas. If there’s none, or if it’s too small, you’ll want to create or resize your swap space.
Next, ensure you have enough disk space to create a swap file. The command df -h
gives you a human-readable output of your disk usage. Ideally, you want to have at least 1GB of free space.
Assuming you have the space, let’s create a swap file. You can allocate a 1GB swap file with:
sudo fallocate -l 1G /swapfile
If fallocate
isn’t available, you can use dd
as an alternative method to create a swap file.
To secure your swap file, change its permissions to prevent access from unauthorized users:
sudo chmod 600 /swapfile
Then, format it as swap space:
sudo mkswap /swapfile
And enable it:
sudo swapon /swapfile
Your server now has additional virtual memory to use, but we’re not done yet.
To make sure your server uses the swap file even after a reboot, add it to your /etc/fstab
file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
For a balanced system, you’ll want to adjust how often the system uses swap space. This is controlled by the swappiness
value. Check the current setting with:
cat /proc/sys/vm/swappiness
Setting it to 15 is a good starting point:
sudo sysctl vm.swappiness=15
To make this change permanent, add it to /etc/sysctl.conf
:
echo 'vm.swappiness=15' | sudo tee -a /etc/sysctl.conf
Similarly, for vfs_cache_pressure
, which controls how aggressively the system reclaims memory used for caching, a setting of 60 can be beneficial:
sudo sysctl vm.vfs_cache_pressure=60
And again, make this permanent:
echo 'vm.vfs_cache_pressure=60' | sudo tee -a /etc/sysctl.conf
By now, your server should be better equipped to handle memory-intensive operations like npm install
. Remember, swap is a temporary workaround for insufficient RAM. If you find yourself needing it often, consider upgrading your server’s physical memory.
Thank you for tuning in to this episode of “Continuous Improvement.” I hope these tips help you optimize your server’s performance. If you enjoyed this episode, don’t forget to subscribe and leave a review. I’m Victor Leung, and until next time, keep improving!