DEV Community

Cover image for Add Manjaro into WSL 2
Damian Cyrus
Damian Cyrus

Posted on

Add Manjaro into WSL 2

I like Manjaro Linux. Too bad it is my second system, as I still need Windows software every day, which might or not work on a Linux distribution.

For this I wanted to use it in WSL, and found this nice YouTube video: Arch Linux on Windows Subsystem for Linux (WSL) - YouTube.

Based on the steps from the video I made my decision to use it as a base. The only preparation for this was to find out which docker image to use. Everything else is the same. I guess other distributions will work similarly if there is a docker image.

Requirements

  • Windows 10/11 with WSL 2 support
  • docker cli
    • Docker Desktop or
    • Rancher Desktop with Container Engine: dockerd (moby) within the settings
  • Windows Terminal
  • Windows PowerShell Core (recommended)
  • Ubuntu (or any other available distribution from the Windows Store) on WSL installed

Step by step

This guide will not go much into details, as it should bring you fast to an up and running system.

At the end there will be a condensed version, too. Use it when you understand the topics well.

Preparations

Make sure about the requirements, and your Linux distribution can run docker within.

Login into WSL

Open Windows Terminal, go into your WSL distribution and run following steps:

  • Pull image from docker hub: docker pull manjarolinux/base
  • Create a Manjaro Linux container: docker create -i manjarolinux/base bash
  • Copy the first eight or ten characters from the output. Example: 83398c22...
  • Run the container: docker container start 83398c22
  • Run interactive shell with it: docker exec -it 83398c22 /bin/bash

Within the Linux distribution

You have run the bash shell and it should now show information by running: whoami. It will show your logged in user is root.

Initialize pacman keys

There might be an optional step, but I recommend it for Arch Linux distributions. This one is for Manjaro:

pacman-key --refresh-keys
pacman-key --populate archlinux manjaro
Enter fullscreen mode Exit fullscreen mode

This can take time.

For more information see:

Add user and admin rights

Add a user within the wheel group and change its password:

useradd -m -G wheel cyrdam
passwd crydam
Enter fullscreen mode Exit fullscreen mode

Why wheel group? The wheel group will get sudo privileges in the next steps.

Next, the packages need to be up to date, and applications installed for user accounts:

pacman -Syu
pacman -Syu sudo vim
Enter fullscreen mode Exit fullscreen mode

This will install sudo and vim. You can choose any other shell editor if you like, this will go with the following.

Now we can add sudo privileges to the wheel group by running:

EDITOR=vim visudo
Enter fullscreen mode Exit fullscreen mode

And remove the hash character on the following line:

# %wheel ALL=(ALL) ALL

should look like

%wheel ALL=(ALL) ALL

Save and close vim by typing: :wq

Exit the docker container with the command:

exit
Enter fullscreen mode Exit fullscreen mode

Export the docker image

Create a directory on your hard drive from within the Linux distribution and go there. Then create a sub-folder for the .vhdx image file. You can choose whatever folder you like, but I recommend not to use a directory within the user directory.

Example:

cd /mnt/c/dev/wsl/
mkdir manjarolinux
Enter fullscreen mode Exit fullscreen mode

The directory manjarolinux is there to store the .vhdx file and will be empty at this moment. Stay in the current folder and run this docker command:

docker export 83398c22 > /mnt/c/dev/wsl/manjarolinux.tar
Enter fullscreen mode Exit fullscreen mode

It will take seconds to write the container into the file given by the path.

The container ID needs to be the same as at the beginning. You can get it by looking into the container list with: docker container ls.

Now exit your Linux distribution:

exit
Enter fullscreen mode Exit fullscreen mode

Import the image into WSL

From the Windows Terminal you go to the created directory and check if everything looks fine:

cd C:\dev\wsl\
ls
Enter fullscreen mode Exit fullscreen mode

There you see the .tar file and the empty directory manjarolinux.

It is time to import the image into WSL:

wsl --import Manjaro ./manjarolinux manjarolinux.tar
Enter fullscreen mode Exit fullscreen mode

Explanation of the parts:

  • wsl --import: command to import a distribution
  • Manjaro: Name of the distribution in WSL (and Windows Terminal drop-down menu)
  • ./manjarolinux: where to store the .vhdx image file
  • manjarolinux.tar: file to import

Restart Windows Terminal.

Update Windows Terminal settings

In Windows Terminal open the drop-down menu and run Manjaro from there. It will run the user as root. Nobody wants that. Add a little start command to change it each time we run the Terminal Window.

Go to the Windows Terminal settings and open the file version of the settings by clicking on the gear icon on the bottom left side of the window. Your editor for the file should open. Search for Manjaro. Add commandline into the settings:

{
    ...
    "name": "Manjaro",
    ...
    "commandline": "wsl.exe -u cyrdam -d Manjaro"
}
Enter fullscreen mode Exit fullscreen mode

You could also do it over GUI for the Manjaro profile.

Close the editor for the file, restart Windows Terminal and open Manjaro again. Check the user with whoami. It should show you your defined user (in my case cyrdam).

Optional step (aka signature)

To have a pleasant view and proof of running Manjaro within WLS you can install archey3 and present the output of your system, showing you also the kernel version from Microsoft. Run following commands:

sudo pacman -Syu archey3
archey3
Enter fullscreen mode Exit fullscreen mode

See running Manjaro Linux in WSL on Windows.

My output:

$ archey3

               +                OS: Arch Linux x86_64
               #                Hostname: name of the machine
              ###               Kernel Release: 5.15.79.1-microsoft-standard-WSL2
             #####              Uptime: 1:13
             ######             WM: None
            ; #####;            DE: None
           +##.#####            Packages: 151
          +##########           RAM: 1436 MB / 15856 MB
         #############;         Processor Type: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
        ###############+        $EDITOR: None
       #######   #######        Root: 798M / 1007G (0%) (ext4)
     .######;     ;###;`".
    .#######;     ;#####.
    #########.   .########`
   ######'           '######
  ;####                 ####;
  ##'                     '##
 #'                         `#
Enter fullscreen mode Exit fullscreen mode

Improvement thoughts

To make this less time taking it would be possible to automate this entire process by a script with input capabilities. That means we would also run commands within the terminal and containers directly.

I do not know how this could work, but here is a summary of all steps.

Within Linux distribution

Run Windows Terminal with your Linux distribution, then start with the commands.

# Download image and create container
docker pull manjarolinux/base
docker create -i manjarolinux/base bash
# Copy the first eight characters of the output (example: 83398c22)
# Run the container
docker container start 83398c22
# Login into the container with the bash shell
docker exec -it 83398c22 /bin/bash
Enter fullscreen mode Exit fullscreen mode

Within docker container

# Update pacman keys (can take a while)
pacman-key --refresh-keys
pacman-key --populate archlinux manjaro
# Add user
useradd -m -G wheel cyrdam
passwd crydam
# Update packages, install sudo and editor
pacman -Syu
pacman -Syu sudo vim
# Add user group to sudo user
EDITOR=vim visudo
# Edit the line: '# %wheel ALL=(ALL) ALL' > '%wheel ALL=(ALL) ALL'
# Exit vim
:wq
# Exit docker container
exit
Enter fullscreen mode Exit fullscreen mode

Back in Linux distribution

# Create the folder first if not available then go to
cd /mnt/c/dev/wsl/
mkdir manjarolinux
# Use the ID from before or search for it with: `docker container ls`
docker export 83398c22 > /mnt/c/dev/wsl/manjarolinux.tar
# Exit the Linux distribution
exit
Enter fullscreen mode Exit fullscreen mode

Within Windows PowerShell

cd C:\dev\wsl\
# Check directory content for .tar file and empty folder
ls
# Import image
wsl --import Manjaro ./manjarolinux manjarolinux.tar
Enter fullscreen mode Exit fullscreen mode

Within Windows Terminal settings

# Update settings for 'Manjaro' profile by adding following line
"commandline": "wsl.exe -u cyrdam -d Manjaro"
# Restart terminal
# Open 'Manjaro' and check user is not root
whoami
Enter fullscreen mode Exit fullscreen mode

Within Manjaro

sudo pacman -Syu archey3
archey3
Enter fullscreen mode Exit fullscreen mode

Summary

If someone knows how to make this automated:

  • adding the image name (dynamic)
  • adding the distribution name for WSL (dynamic)
  • running all commands without issues or destroying anything

then please share your knowledge. Is there a better solution?

Currently, I can export my Manjaro Linux and share it on other machines if necessary:

wsl --export <distributionsname> <TAR-file>
wsl --import <distributionsname> <directory path to vhdx file> <TAR-file>
Enter fullscreen mode Exit fullscreen mode

But be careful: you will also copy every file within the distribution, that also means project files that might not be for sharing because of legal reasons.

Top comments (0)