/emacs-wsl

Install and run emacs with the Windows Subsystem for Linux (WSL) in Windows 10.

Primary LanguageShell

Emacs-wsl

This guide shows you how to run Emacs with the Windows Subsystem for Linux WSL in Windows 10. Emacs can either be run with a graphical display or directly in the terminal.

This guide is using Ubuntu 18.04 LTS as Linux distribution.

./img/emacs-wsl.png

Contents

Enable the Windows Subsystem for Linux

https://docs.microsoft.com/en-us/windows/wsl/install-win10

Open PowerShell as Administrator and run the following to enable the feature:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Restart your computer (you should be prompted to do so).

This can also be done with:

  • Open Control Panel
  • Programs and Features
  • Turn Windows Feature on or off
  • Check Windows Subsystem for Linux

Install the Linux distribution

install-your-linux-distribution-of-choice

You can install the Linux distribution:

Installing from command line

Installing Ubuntu 18.04 from command line with PowerShell.

curl.exe -L -o ubuntu-1804.appx https://aka.ms/wsl-ubuntu-1804
Add-AppxPackage .\ubuntu-1804.appx

Configure WSL

Start the WSL and configure it.

Update

Make sure everything is up to date:

sudo apt update
sudo apt upgrade

wsl.conf

With Windows 10 Build 17093 and later certain functionality (automount options and network configuration) in WSL can be configured inside the file /etc/wsl.conf.

No need to touch that file, just leave the defaults.

User

Instead of using root user it’s better to add an user and use that as default user.

Add user

sudo adduser <username>

Make it a sudo user

Make that user be a sudo user:

sudo usermod -a -G sudo <username>

Change default user

Change the default user which is used when starting the WSL.

Open cmd.exe and run:

ubuntu config --default-user <username>

Restart WSL.

You can change the default back to root by using root as username. To change to root inside wsl temporarily use sudo su -.

Ssh key

Generate a new ED25519 SSH key pair:

ssh-keygen -t ed25519 -C "email@example.com"

A dialog will ask you to:

  • input a file path: use the suggested path by pressing Enter
  • enter a password: enter your password

To copy the generated ssh key into the clipboard use:

clip.exe < ~/.ssh/id_ed25519.pub

Language

This might be needed:

sudo update-locale LANG=en_US.UTF8

Mount drives

Temporarily

You can mount network drives temporarily with:

# mount a mapped drive
sudo mkdir /mnt/g
sudo mount -t drvfs G: /mnt/g
# unmout
sudo umount /mnt/g
# mount network location
sudo mount -t drvfs '\\server\share' /mnt/share

All mounted drives are found under /mnt:

cd /mnt
ls

Automatically

To do so the fstab file needs to be configured.

For instance to mount H: add this to “/etc/fstab” (the directory has to exist to make this work, so in this case sudo mkdir /mnt/h is needed beforehand):

H: /mnt/h drvfs defaults 0 0

Zsh

If you want to use zsh and oh-my-zsh:

sudo apt install zsh
chsh -s $(which zsh)
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

Restart WSL.

Install Emacs

This installs all dependencies for Emacs and then installs Emacs 26.3:

## install dependencies (got those from all over the net)

sudo apt install -y autoconf automake autotools-dev bsd-mailx build-essential \
    diffstat gnutls-dev imagemagick libasound2-dev libc6-dev libdatrie-dev \
    libdbus-1-dev libgconf2-dev libgif-dev libgnutls28-dev libgpm-dev libgtk2.0-dev \
    libgtk-3-dev libice-dev libjpeg-dev liblockfile-dev liblqr-1-0 libm17n-dev \
    libmagickwand-dev libncurses5-dev libncurses-dev libotf-dev libpng-dev \
    librsvg2-dev libsm-dev libthai-dev libtiff5-dev libtiff-dev libtinfo-dev libtool \
    libx11-dev libxext-dev libxi-dev libxml2-dev libxmu-dev libxmuu-dev libxpm-dev \
    libxrandr-dev libxt-dev libxtst-dev libxv-dev quilt sharutils texinfo xaw3dg \
    xaw3dg-dev xorg-dev xutils-dev zlib1g-dev libjansson-dev libxaw7-dev \
    libselinux1-dev libmagick++-dev libacl1-dev

## download and install

cd ~
wget https://ftp.gnu.org/pub/gnu/emacs/emacs-26.3.tar.gz
tar -xzvf emacs-26.3.tar.gz
cd emacs-26.3
./configure
make
sudo make install

cd ~
rm -rf ~/emacs-26.3
rm ~/emacs-26.3.tar.gz

See emacs-27.sh to install Emacs 27 instead.

Run Emacs in terminal

Run Emacs with emacs -nw to see if it is working. You can also see what path it is using as home with C-h v user-emacs-directory. That’s where you can place your init.el etc.

Run Emacs in graphical display

To be able to run Emacs with a graphical display you need to install a Windows X server.

Install Windows X-server

An X-server lets you access a Linux application or desktop environment’s graphic user interface (GUI).

You can use VcXsrv or Cygwin/X. Both are free and based on xorg.

Install VcXsrv

Download VcXsrv from https://sourceforge.net/projects/vcxsrv/ and install it.

Install Cygwin/X

You have to install Cygwin and install additional packages:

  • Download Cygwin from https://cygwin.com/install.html.
  • Run the setup to install Cygwin. When you come to the select packages step you have to add (change from skip to the newest version) xorg-server and xinit. If you want to use the wizard (GUI) for launching the X-Server add xlaunch as well.
  • If you missed that step you can easily add those packages later on by running the setup again.

Run Emacs

Run the X-server

With XLaunch (GUI)

Start XLaunch and use the defaults:

  • Multiple Windows, Display number -1 (or 0 if not working), Next
  • Start no client, Next
  • Leave checkboxes, Next
  • Finish

With a Shortcut

Make a shortcut (right click on your desktop > New > Shortcut) and use the following as target.

  • With VcXsrv:
    # Change the path if installed somewhere else.
    "C:\Program Files\VcXsrv\vcxsrv.exe" :0 -multiwindow -clipboard -wgl
        
  • With Cygwin/X:
    # Change the path if installed somewhere else.
    "C:\cygwin64\bin\run.exe" --quote /usr/bin/bash.exe -l -c "XWin :0 -listen tcp -multiwindow -clipboard -wgl"
        

You can put the shortcut into the startup folder to start it when booting. Or stick it to the task bar to launch it from there.

From command line

You can use the command from the Shortcut also from the command line.

Run Emacs from WSL

Open WSL and run (this also changes the keyboard layout used to US, remove if yout don’t want this. Then it should use your default keyboard layout):

export DISPLAY=:0
export LIBGL_ALWAYS_INDIRECT=1
# OPTIONAL Set the keyboard layout to US
setxkbmap -layout us
setsid emacs
exit

This will open Emacs in a new window. By using setsid this is done in a new session and therefore the WSL can be closed after with exit. You can just change it to emacs and remove exit if you want.

To not have to type this over and over make an alias in ~/.bashrc or if you installed zsh in ~/.zshrc:

alias eme='
export DISPLAY=:0.0
export LIBGL_ALWAYS_INDIRECT=1
setxkbmap -layout us
setsid emacs
exit
'

Now you can fire wsl up and run eme.

Remarks

Accessing Linux files from Windows

Don’t touch your Linux files from Windows. Creating and changing Linux files from Windows can result in losing files or corrupting data.

This also means that if you want to for instance copy a file into your subsystem this has to be done from inside the WSL.

Looks like this is getting better if one has Windows 10 Version 1903 or newer:

whats-new-for-wsl-in-windows-10-version-1903

FAQ

Where is the root folder located?

It’s in %LOCALAPPDATA%\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs See https://superuser.com/a/1280916.

How start WSL from Windows Explorer in the current folder?

To start WSL from Windows Explorer just type wsl into the location input box:

./img/wsl-from-windows-explorer.png

The drive has to be mounted else it will not work.

What ways are there to run WSL?

See https://docs.microsoft.com/en-us/windows/wsl/wsl-config#ways-to-run-wsl.