Some notes on using Windows Subsystem for Linux

Created: December 30, 2019   Last Modified: November 15, 2024   Category: windows   Print this pageBack to Home

File permissions in WSL

Here, we describe how to handle file permission in Windows Subsystem for Linux. It was originally from here.

Windows Subsystem for Linux (WSL) usually mounts Windows drives under /mnt. However, the Linux file permission seems to be awful. To fix this issue, simply add to /etc/wsl.conf (if the file does not exist, simply create it):

[automount]
enabled = true
options = "metadata,umask=22,fmask=11"

In short, every files now have permission 0644 and every directories have permission 0755.

Also, add the following to ~/.profile to fix the permission of newly created files and directories.

if [[ "$(umask)" = "0000" ]]; then
	umask 0022
fi

Export and import WSL Distros

Copy contents of a file to clipboard

clip.exe < file.txt # do not miss the .exe part

SSH

I use keychain to avoid typing SSH passphrases multiple times. After installing keychain in my Arch WSL, I simply put the following to .bashrc

/usr/bin/keychain --nogui $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOST-sh

In this way, I have to type in the passphrase for the first time I open a Arch WSL terminal. As long as the distribution is running (which can be veerified by typing wsl -l --running in a cmd windows), I don’t have to type it again when using ssh.

Another way is to use wsl-ssh-agent. If you are using WSL1, the steps are simple.

If you are using WSL2, you need some workaround.

Install SageMath 10.4 in Ubuntu WSL

Enable WSL2

Follow this official instruction.

Install Ubuntu (version 24.04) as a WSL

Follow this official instruction or install from the Microsoft Store. After finishing installation, run the following in a cmd or powershell (if you run wsl --set-default-version 2, you don’t need to do this):

wsl --set-version Ubuntu-24.04 2

Adjust file permissions in Ubuntu if necessary, following the above instruction. Additionally, create a new user in Ubuntu terminal by running the following command as root:

adduser <username>
usermod -a G adm,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev <username>

Run the command ubuntu2404.exe config --default-user <username> to set the default user for the Ubuntu WSL.

Installation

Follow this instruction and this instruction. I recorded the steps here. Open a Ubuntu terminal and run:

sudo apt update
sudo apt upgrade
sudo apt install autoconf automake gh git gpgconf libtool openssh-client pkg-config \
  automake bc binutils bzip2 ca-certificates cliquer cmake curl ecl \
  eclib-tools fflas-ffpack flintqs g++ gengetopt gfan gfortran git glpk-utils gmp-ecm lcalc \
  libatomic-ops-dev libboost-dev libbraiding-dev libbz2-dev libcdd-dev libcdd-tools \
  libcliquer-dev libcurl4-openssl-dev libec-dev libecm-dev libffi-dev libflint-dev \
  libfreetype-dev libgc-dev libgd-dev libgf2x-dev libgiac-dev libgivaro-dev libglpk-dev \
  libgmp-dev libgsl-dev libhomfly-dev libiml-dev liblfunction-dev liblrcalc-dev liblzma-dev \
  libm4rie-dev libmpc-dev libmpfi-dev libmpfr-dev libncurses-dev libntl-dev libopenblas-dev \
  libpari-dev libpcre3-dev libplanarity-dev libppl-dev libprimesieve-dev libpython3-dev \
  libqhull-dev libreadline-dev librw-dev libsingular4-dev libsqlite3-dev libssl-dev \
  libsuitesparse-dev libsymmetrica2-dev zlib1g-dev libzmq3-dev libzn-poly-dev m4 make nauty \
  openssl palp pari-doc pari-elldata pari-galdata pari-galpol pari-gp2c pari-seadata patch perl \
  pkg-config planarity ppl-dev python3-setuptools python3-venv r-base-dev r-cran-lattice singular \
  sqlite3 sympow tachyon tar tox xcas xz-utils \
  texlive-full latexmk pandoc dvipng     
wget https://github.com/sagemath/sage/releases/download/10.4/sage-10.4.tar.gz
echo "dcecdbdc2091798b6f85d1a5300f5f8f sage-10.4.tar.gz" | md5sum -c
tar -xvf sage-10.4.tar.gz -C $HOME
cd $HOME/sage-10.4
rm -rf configure
make configure
./configure
MAKE="make -j8" make

Note:

Some extra packages I installed with sage:

sage -i plantri sage_sws2rst rst2ipynb notebook

Aliases in $HOME/.bashrc

Add the following to $HOME/.bashrc:

alias sage="~/sage-10.4/sage"
alias sage-notebook="cd <your-sagemath-notebook-dir> && ~/sage-10.4/sage -n jupyter"
alias sage-clear="echo yes | ~/sage-10.4/sage -ipython history clear"

Terminal Colors

See this page for more details. First, run sage -ipython profile create to create the default profile. Then, in Ubuntu terminal, run:

export IPYTHON_CONFIG=$(sage -ipython locate)
echo "c.TerminalInteractiveShell.colors = 'Linux'" >> $IPYTHON_CONFIG/profile_default/ipython_config.py

Open SageMath Jupyter notebook in Google Chrome

In Ubuntu Terminal. run:

cd $HOME/sage-10.4
sage-notebook --generate-config

Edit $HOME/.sage/jupyter-4.1/jupyter_notebook_config.py by adding the following content to the end:

c.SeverApp.use_redirect_file = False
import webbrowser
webbrowser.register('chrome', None, webbrowser.GenericBrowser(u'/mnt/c/Program Files/Google/Chrome/Application/chrome.exe'))
c.ServerApp.browser = 'chrome'

On the other hand, you can also add the following to $HOME/.bashrc:

export BROWSER="/mnt/c/Program Files/Google/Chrome/Application/chrome.exe"

Note that C:\Program Files\Google\Chrome\Application\chrome.exe is the location of Google Chrome installed in my computer.

If you want to open the notebook in a specific folder, say C:\Users\<your-username>\SageMath, run the following command in Ubuntu Terminal:

sage -n jupyter --notebook-dir="/mnt/c/Users/<your-username>/SageMath"

Create shortcuts in Windows desktop

Add/Remove Right Click/Shift + Right Click “Open SageMath Notebook here” context menu

Create a Add_Open_SageMath_Notebook_here_context_menu.reg file with the following contents (I modified the downloaded registry file from this guide. Remeber to change the path to sagemath.ico appropriately):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\SageMathWSL]
@="Open SageMath Jupyter Notebook here"
"Extended"=-
"Icon"="C:\\Users\\<your-username>\\Pictures\\Icons\\sagemath.ico"
"NoWorkingDirectory"=""
"ProgrammaticAccessOnly"=-

[HKEY_CLASSES_ROOT\Directory\Background\shell\SageMathWSL\command]
@="wsl.exe --cd \"%V\" --distribution Ubuntu-24.04 --exec /bin/bash -c \"$HOME/sage-10.4/sage --notebook jupyter\""



[HKEY_CLASSES_ROOT\Directory\shell\SageMathWSL]
@="Open SageMath Jupyter Notebook here"
"Extended"=-
"Icon"="C:\\Users\\<your-username>\\Pictures\\Icons\\sagemath.ico"
"NoWorkingDirectory"=""
"ProgrammaticAccessOnly"=-

[HKEY_CLASSES_ROOT\Directory\shell\SageMathWSL\command]
@="wsl.exe --cd \"%V\" --distribution Ubuntu-24.04 --exec /bin/bash -c \"$HOME/sage-10.4/sage --notebook jupyter\""



[HKEY_CLASSES_ROOT\Drive\shell\SageMathWSL]
@="Open SageMath Jupyter Notebook here"
"Extended"=-
"Icon"="C:\\Users\\<your-username>\\Pictures\\Icons\\sagemath.ico"
"NoWorkingDirectory"=""
"ProgrammaticAccessOnly"=-

[HKEY_CLASSES_ROOT\Drive\shell\SageMathWSL\command]
@="wsl.exe --cd \"%V\" --distribution Ubuntu-24.04 --exec /bin/bash -c \"$HOME/sage-10.4/sage --notebook jupyter\""

To remove these items from the context menu, create a Remove_Open_SageMath_Notebook_here_context_menu.reg file with:

Windows Registry Editor Version 5.00

[-HKEY_CLASSES_ROOT\Directory\Background\shell\SageMathWSL]

[-HKEY_CLASSES_ROOT\Directory\shell\SageMathWSL]

[-HKEY_CLASSES_ROOT\Drive\shell\SageMathWSL]