Running sudo in Jupyter notebook launched from

Is there a way to run sudo commands from a Python Jupyter notebook launched from the Project Jupyter | Try Jupyter public site (which I assume is hosted by mybinder)?

I need to install some additional packages and !pip install isn’t enough. The specific command I need is !sudo apt-get install python3-lxml but this returns an error as follows.

sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the ‘nosuid’ option set or an NFS file system without root privileges?

Yes, those examples are all being served via

Did you just try running %pip install lxml in your notebook? You sure that doesn’t get you want you need? (I’ll continue on with other options, assuming pip alone doesn’t cover your needs here. However, if it did, you could just use a simple requirements.txt configuration file to specify that for future launches of the environment you need, see here, with an example here. This tends to be the simplest way to specify an environment in conjuction with MyBinder launches.)
See below if you aren’t sure that apt-get is the only route to install what is needed. Often conda (actually mamba in mybinder building images for the containers) is faster and easier. See here and here. Plus using %conda install you can often do from within a session that is already running, see the section below for more as the rest of this section, I’ll answer your question about how to use apt-get in conjunction with MyBinder.

You cannot do what you are trying to do from within a launched session. However, you can configure an environment to launch as you specify. MyBinder works with many things besides Github and so you aren’t restricted to that even. You can go to the link above and click on the drop-down on the left below ‘GitHub repository name or URL’ to see the possibilities.

In your repository you’ll make a text file names apt.txt and put ‘python3-lxml’ on a line as the content there. This configuration file will direct the MyBinder service to build the image that will launch the served environment using apt-get to install that dependency as described here.

Resources about this:

That is if you are sure that using apt-get is the only way to install it. Use of %conda install lxml, based on here and here in a running session isn’t working?

In discussing conda/mamba above, I quickly pointed out above why if that installs what you want, that use of environment.yml to configure the environment may be better for your. environment.yml is another type of configuration file that you can use to direct MyBinder to build the appropriate environment for launches, see here.


Thanks for the help. I got it working by creating a Github repository with an apt.txt to get the required packages installed.

1 Like