How can I raise the limit for open files in Ubuntu 20.04 on WSL2?
Asked Answered
R

3

21

My setup looks as follows: Windows 10, Release 1909 (Build 18363.1082), using WSL2 with an Ubuntu 20.04 environment. Everything works nicely most of the time, but there are some issues I cannot manage to solve.

During development using parcel (React bundler), I run into the problem that the bundler apparently opens lots of files at the same time, and at a certain point, I run into the following problem:

EMFILE: too many open files, open '/home/myusername/Projects/some-project-path/node_modules/@material-ui/icons/esm/RoundedCornerRounded.js'

As parcel seemingly does not easily support using something like graceful-fs, I have tried to increase the limit for open files inside the Ubuntu environment. What I have tried so far:

  • A simple ulimit -n 4096 (which is the highest possible by default), but it's apparently (by far?) not enough
  • I tried increasing fs.files-max to something really high in /etc/sysctl.conf, but it doesn't seem to have an effect (neither after sysctl -p nor after a restart of wsl)
  • I also tried increasing fs.inotify.max_user_watches, but that did not seem to have an effect either
  • Also setting soft and hard limits in /etc/security/limits.conf did not seem to have an effect
  • I also found information that changing DefaultLimitNOFILE in /etc/systemd/system.conf can have an effect (so I did that as well)

Has anybody manage to solve a similar system on Ubuntu 20.04 on WSL2? This left me pretty stumped, and it prevents me from using parcel inside this environment. That's a real pity, as really everything else is working really fine.


UPDATE

So I have found out that my changes in various places (probably the one in /etc/security/limits.conf) has had some kind of effect. Just not when logging in directly. This illustrates this:

donmartin@SOMEMACHINE:~$ ulimit -Hn
4096
donmartin@SOMEMACHINE:~$ su donmartin
Password:
donmartin@SOMEMACHINE:~$ ulimit -Hn
65536
donmartin@SOMEMACHINE:~$

Which means: If I su to my own user, the ulimit has indeed been raised. But if I log in just as normal using Windows Terminal, this limit is not in effect. Even more puzzled now - BUT - I have a workaround for my problem. Having set my values to 65536, the parcel build now works, running as my own user. Go figure! I still don't quite know which setting was changing the behaviour now - perhaps somebody has more thorough information on how this works and/or how I can make this also the default without having to do a su to get the updated limits.

Ranice answered 18/9, 2020 at 18:15 Comment(0)
R
8

I had to add the following line to /etc/systemd/user.conf:

DefaultLimitNOFILE=65535

As written in the answer here:

https://superuser.com/questions/1200539/cannot-increase-open-file-limit-past-4096-ubuntu/1200818#1200818?s=1b927bb17396480da98a94cbacf8da62

Also you may need to run this (if working with applications that monitors changes in many files/folders):

sudo sh -c 'sysctl fs.inotify.max_user_watches=524288 && sysctl -p'

Rapport answered 28/4, 2021 at 13:21 Comment(4)
Can you confirm that you are on WSL? The original question is about Windows Subsystem for Linux, which does not support systemd, so it would be surprising if editing a systemd configuration file would have any effect for this question. Perhaps I'm missing something; or perhaps you are?Tetrafluoroethylene
This was on WSL2. I had exactly the same problem with number of open files using WSL2 with Ubuntu 20.04. First I tried limits.conf and what I'm used to from other Linux distros, and then I stumbled upon the linked answer which solved it for me.Rapport
Cool - As I said, I'm surprised, but thanks for confirming!Tetrafluoroethylene
This is the only thing that actually worked for me on Ubuntu 22.04Carnet
V
1

Temporarily increase the open files hard limit for the session

Run this 3 commands (the first one is optinal), to check current open files limit, switch to admin user, and increase the value.

$ ulimit -n
1024

$ su <user name>
<Enter password>

$ ulimit -n 65535

Check the new limit:

$ ulimit -n
65535

To check all values, run this:

$ ulimit -a
Vermiculation answered 12/9, 2022 at 19:33 Comment(0)
M
0

Try this:

$ visudo
ADD: user ALL=(ALL) NOPASSWD:ALL
$ vi ~/.profile
ADD: user ALL=(ALL) NOPASSWD:ALL
$ vi /etc/security/limits.conf
ADD: user soft nproc 10000
user hard nproc 10000
user soft nofile 10000
user hard nofile 10000
Mozellemozes answered 2/11, 2021 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.