Running Snaps on WSL2 (Insiders only for now)

I’ve followed the latest instructions and even used the precompiled kernal. When I run snap version and snap debug confinement I get the expected results. But when I run sudo snap install hello-world I am getting: error: access denied (see 'snap help login'). I created an Ubuntu One account and tried to login, but after putting in my password I get the same exact message. The output of snap version is:

➜  dev-files snap version
snap    2.42.1+18.04
snapd   2.42.1+18.04
series  16
ubuntu  18.04
kernel  4.19.72-microsoft-standard+

I am zsh via oh my zsh!. I doubt that is the issue, though…

I figured out something (weird to me, but probably not others) that if I run /usr/bin/env bash after wsl boots, it puts me into the right context to now use something as simple as sudo snap list. Maybe this does have something to do with ZSH?

Hi Guys,
Do we have version of this scripts for running in ZSH ?

I have noticed something that messes up the scripts when on 20.04. The fix is to have the “ps -ef” commands be “ps -efw”. Without that “-w” the output is truncated, one of the calls will fail to find in start, the other will succeed and will not spawn anything, which causes my gnome-terminal sessions to instantly close after the first one.

Adding the -w to both ps commands fixed it for me.

1 Like

Additionally, I have managed to fix a few issues that happened on my system.

First, in start-systemd-namespace I have also added $(pwd) and in my copy I said something like export SAVED_PWD="$(pwd)". Of course, you probably would do something like export PRE_NAMESPACE_PWD="$(pwd)". At the end of the script I also did a cd to that variable.

Second, in enter-systemd-namespace I have had serious issues with the environment due to an interesting issue – your literal “$HOME” in the /bin/login case actually evaluates to /root on my system (or /home/root if root’s homedir is moved there) rather than the correct one. I have replaced $HOME with $(eval echo ~$SUDO_USER). Now it’s reliable enough.

Finally, for some reason that -w issue from before stopped affecting me after a reinstall? Not sure. (EDIT: scratch this one, my terminal was simply wider)

EDIT 2: Also unset the variables once they’re used. This way if I just do ctrl-shift-t in a gnome-terminal they are not overridden.

1 Like

Unfortunately, I cannot edit the forum post above anymore, because it’s too old so the forum has locked it. I’m helping to maintain the code at https://github.com/damionGans/ubuntu-wsl2-systemd-script now

Edit: I’m marking this comment as “The Solution” so that passers-by hopefully see the link…

2 Likes

Alright, I will send pull requests addressing the issues. Thanks for notifying.

1 Like

Double check that they’re required, as I recently made a substantial change to improve things like the home directory…

Sure, I’ve already sent some other minor improvements which I do believe are still applicable. I’ll

check if the home directory handling still needs fixing before sending anything related to that.

Thanks.

Mail](https://go.microsoft.com/fwlink/?LinkId=550986) for Windows 10

A755AF79EA1F4C6691B4E7BDC582DE56.png

1 Like

Hi there, thanks for creating and sharing a great script!
I’ve sent three pull requests for minor improvements to https://github.com/DamionGans/ubuntu-wsl2-systemd-script.

I run LXD and docker on systemd successfully.
I confirmed that the default kernel would do for running docker (The custom kernel is not needed).

I wrote a blog post to set up LXD and docker at


(in Japanese though).
No special steps are needed.
Just as usual installation steps for LXD or docker once systemd is running.
Wonderful!
So thanks again for a great script!

I’ve added an automatic installation and updater script for the custom kernel (if you want to use it) at https://github.com/diddlesnaps/WSL2-Linux-Kernel/wiki/Installation-and-Automatic-updater

2 Likes

I followed all the steps in your post, including installing prerequisites (daemonize, etc.)

Now when I try to run Ubuntu I get:

“/usr/sbin/enter-systemd-namespace: line 10: /usr/sbin/daemonize: No such file or directory”

I’m new to WSL2 but not Linux. I don’t know how to edit Ubuntu’s configuration files without starting it first, but it won’t start. Any ideas? Where is the filesystem? I’ve searched Windows and files are not found so I guess it must be an image file. Hopefully in a standard format I can mount some alternative way (from VirtualBox or something, which I haven’t yet installed on this new Windows 10 PC as I’ve only been using WSL2) and not some Microsoft specific image format. Ideas?

Also, are there default values for the two environment variables set? I didn’t check before I set them. I’ve removed them and the error persists so I’m guessing the value for BASH_ENV is the default, as /etc/bash.bashrc already existed and I verified what was added to it before I exited and tried to restart Ubuntu.

I just installed WSL2 within the past month or so as soon as it became available in Windows Update.

Hi,

You can “break in” to your distro to find out what’s wrong, or revert the changes, by running the following in powershell or cmd.exe:

wsl.exe -e bash --norc

# or

wsl.exe -e sh

If your distro is not the default in WSL then you can specify the correct one with the -d switch:

wsl.exe -d Ubuntu-20.04 -e bash --norc
1 Like

As to the environment variables, they are not present by default until the script added them.

I’ve also worked up a different method that you might want to try instead, but I’ve not created an install script for it yet:

https://snapcraft.ninja/2020/08/06/starting-systemd-in-wsl-when-you-login-to-windows-youll-be-astounded-by-the-speed-improvement/

1 Like

Thanks. Got in. Commented out the new line in bash.bashrc for now. Have to go through the scripts and see what’s not working.

Hi! :grinning:

I am trying to activate systemd and I almost did it.

I followed the instructions from here: https://github.com/DamionGans/ubuntu-wsl2-systemd-script.

After installing this script and running wsl for the first time, the command: “snap version” gives this result:

> snap version                                                                                                 
snap 2.45.1+20.04.2
snapd unavailable
series -

The systemctl command produces this result:

> systemctl                                                                                                     
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

But if I execute a command after that in the current session:

> neofetch

or

> wslfetch

then the message “Sleeping for 1 second to let systemd settle” will appear for a short time (~ 1 or 2 seconds).

Then a welcome message will be displayed:

Sleeping for 1 second to let systemd settle
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.121-microsoftstandard x86_64)
 * Documentation: https://help.ubuntu.com
 * Management: https://landscape.canonical.com
 * Support: https://ubuntu.com/advantage
  System information as of Wed Aug 26 20:12:21 MSK 2020
  System load: 0.0 Processes: 89
  Usage of /: 6.7% of 250.98GB Users logged in: 0
  Memory usage: 5% IPv4 address for eth0: 172.26.42.54
  Swap usage: 0%
 * Are you ready for Kubernetes 1.19? It's nearly here! Try RC3 with
   sudo snap install microk8s --channel=1.19/candidate --classic
   https://microk8s.io/ has docs and details.
3 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Last login: Wed Aug 26 20:10:40 MSK 2020 on pts/0
And after that the "systemctl" command will display a list of available units, their status and states (I'm just giving the end of a large list here): 
LOAD = Reflects if the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
183 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
lines 150-191/191 (END)

The “snap version” command shows that snap and snapd are available:

> snap version                                                                                                  
snap 2.45.1+20.04.2
snapd 2.45.3.1
series 16
ubuntu 20.04
kernel 4.19.121-microsoftstandard

The list of snap packages will be available as well as the packages themselves - I can run the snap packages available to me (GUI applications using VcXsrv or just console applications). But snap packages will only be available to me in the current session of the terminal: if I open a new session of the terminal - snap packets will not be available again.

But when I execute commands again in the new session:

> neofetch

or

> wslfetch

— then again the message “Sleeping for 1 second to let systemd settle” will appear for a short time (~ 1 or 2 seconds).

Then a welcome message will be displayed:

Sleeping for 1 second to let systemd settle
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 4.19.121-microsoftstandard x86_64)
 * Documentation: https://help.ubuntu.com
 * Management: https://landscape.canonical.com
 * Support: https://ubuntu.com/advantage
  System information as of Wed Aug 26 20:12:21 MSK 2020
  System load: 0.0 Processes: 89
  Usage of /: 6.7% of 250.98GB Users logged in: 0
  Memory usage: 5% IPv4 address for eth0: 172.26.42.54
  Swap usage: 0%
 * Are you ready for Kubernetes 1.19? It's nearly here! Try RC3 with
   sudo snap install microk8s --channel=1.19/candidate --classic
   https://microk8s.io/ has docs and details.
3 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable
Last login: Wed Aug 26 20:10:40 MSK 2020 on pts/0

And I can use snap packages again.

Another strange thing is that when I get access to snap packages, or in other words when systemd is activated, it somehow modifies the $PATH environment variable, and then I cannot call windows applications from current wsl session (but in new session I can). For example: “code” to run vs code or “exlorer.exe .” to run the windows explorer:

> exlorer.exe .
zsh: command not found: explorer.exe

But in the new session, I lose the availability of snap packages (or systemd) and can run windows applications from wsl again.

And if print the value of the environment variable $PATH in the session where snap packages are available (or in other words, systemd is activated) will be something:

> echo $PATH
/home/haosmos/.nvm/versions/node/v14.8.0/bin:/home/haosmos/haosmos/.local/bin:/home/haosmos/bin:/usr/local/bin:/home/haosmos/. local/share/umake/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

If output the environment variable $PATH - in a session where snap packages are NOT available (or in other words, systemd is NOT activated) there will be something:

> echo $PATH
/home/haosmos/.nvm/versions/node/v14.8.0/bin:/home/haosmos/haosmos/.local/bin:/home/haosmos/bin:/usr/local/bin:/home/haosmos/.local/share/umake/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/mnt/c/Program Files (x86)/NetSarang/Xlpd 6/:/mnt/c/Program Files (x86)/NetSarang/Xftp 6/:/mnt/c/Program Files (x86)/NetSarang/Xshell 6/:/mnt/c/Program Files (x86)/NetSarang/Xmanager 6/:/mnt/c/tools/dart-sass:/mnt/c/Users/Haosmos/AppData/Local/hyper/app-3.0.2/resources/bin:/mnt/c/Windows/System32:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/dotnet/:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/Dart/dart-sdk/bin:/mnt/c/WINDOWS/System32/wsl:/mnt/c/Users/Haosmos/AppData/Roaming/nvm:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/PowerShell/7/:/mnt/c/Program Files (x86)/Common Files/Acronis/VirtualFile/:/mnt/c/Program Files (x86)/Common Files/Acronis/VirtualFile64/:/mnt/c/Program Files (x86)/Common Files/Acronis/FileProtector/:/mnt/c/Program Files (x86)/Common Files/Acronis/FileProtector64/:/mnt/c/Program Files (x86)/Common Files/Acronis/SnapAPI/:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/:/mnt/c/WINDOWS/System32/OpenSSH/:/mnt/c/Program Files/Git/cmd:/mnt/c/Users/Haosmos/AppData/Local/Microsoft/WindowsApps:/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/Users/Haosmos/AppData/Local/GitHubDesktop/bin:/mnt/c/Users/Haosmos/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Program Files (x86)/Python38-32:/mnt/c/Program Files (x86)/Python38-32/Scripts:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/tools/dart-sdk/bin:/mnt/c/Users/Haosmos/AppData/Roaming/Pub/Cache/bin:/mnt/c/Program Files/JetBrains/WebStorm 2020.2/bin:/mnt/c/Program Files/JetBrains/PhpStorm 2020.1.2/bin:/mnt/c/Users/Haosmos/AppData/Local/Microsoft/WindowsApps:/mnt/c/Program Files/JetBrains/WebStorm 2020.1/bin:/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe:/mnt/c/Program Files/Firefox Developer Edition/firefox.exe:/mnt/c/Program Files/Internet Explorer/iexplore.exe

I recorded a short video with a demonstration of what I have described above — https://yadi.sk/i/gRPIMDwaDqt6uw

Please tell me how to make snap packages (or more generally systemd) and commands to run windows applications always available in wsl 2?

My environment: Windows 10 OS version: 2004 OS build: 20197.1000 (insider preview “dev”) WSL: Ubuntu-20.04.1 Linux kernel: 4.19.121

Try executing those commands in bash with the bash command

i followed these steps and am now getting an “access denied” error trying to access WSL. has anyone else experienced this? tried to reinstall and got this error:

Installing, this may take a few minutes…
WslRegisterDistribution failed with error: 0x80070005
Error: 0x80070005 Access is denied.

nevermind - i’m dumb. just had to edit ~/.wslconfig to remove the kernel. i’m guessing it’s a permissions error to that folder

To the author

When I tried it according to the article, when I started UBUNTU-20.04, at the beginning,
/ bin / cat: /root/.systemd-env: No such file or directory
Will come out.
It may not be a problem, but what does this mean?
Is there a way to avoid the appearance of this wording?