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 and lxde as desktop.
Contents
- Enable the Windows Subsystem for Linux
- Install the Linux distribution
- Configure WSL
- Install Emacs
- Run Emacs in terminal
- Run Emacs in graphical display
- Remarks
- FAQ
Enable the Windows Subsystem for Linux
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
You can install the Linux distribution:
- from the Microsoft store
- from the command line (used in this guide)
- manually (download)
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 in WSL can be configured inside the file “/etc/wsl.conf”. The default is as follows:
# Enable extra metadata options by default.
[automount]
enabled = true
mountFsTab = true
root = /mnt/
options = ""
# Enable DNS even though these are turned on by default, we’ll specify here just to be explicit.
[network]
generateHosts = true
generateResolvConf = true
[interop]
enabled = true
appendWindowsPath = true
[automount]
- enabled
- true causes fixed drives (i.e C:/ or D:/) to be automatically mounted with DrvFs under /mnt. False means drives won’t be mounted automatically, but you could still mount them manually or via fstab.
- mountFsTab
- true sets /etc/fstab to be processed on WSL start. /etc/fstab is a file where you can declare other filesystems, like an SMB share. Thus, you can mount these filesystems automatically in WSL on start up.
- root
- Sets the directory where fixed drives will be automatically mounted. For example, if you have a directory in WSL at windir and you specify that as the root, you would expect to see your fixed drives mounted at /windir/c
- options
- This value is appended to the default DrvFs mount options string. Only DrvFs-specific options can be specified. Options that the mount binary would normally parse into a flag are not supported. If you want to explicitly specify those options, you must include every drive for which you want to do so in /etc/fstab.
[network]
- generateHosts
- true sets WSL to generate /etc/hosts. The hosts file contains a static map of hostnames corresponding IP address.
- generateResolvConf
- true set WSL to generate /etc/resolv.conf. The resolv.conf contains a DNS list that are capable of resolving a given hostname to its IP address.
[interop]
- enabled
- Setting this key will determine whether WSL will support launching Windows processes.
- appendWindowsPath
- Setting this key will determine whether WSL will add Windows path elements to the $PATH environment variable.
User
Instead of using root user it’s better to add an user.
Add user
sudo adduser <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.
Make it a sudo user
Make that user be a sudo user:
# change to root
sudo su -
sudo usermod -a -G sudo <username>
exit
Ssh key
Generate a new ED25519 SSH key pair:
ssh-keygen -t ed25519 -C "[email protected]"
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
sudo apt install -y build-essential texinfo libx11-dev libxpm-dev \
libjpeg-dev libpng-dev libgif-dev libtiff-dev libgtk2.0-dev \
libncurses-dev gnutls-dev libgtk-3-dev libgnutls28-dev
# some more from a stackoverflow, eww was not working before
sudo apt install -y autoconf automake libtool xorg-dev libncurses5-dev \
libdbus-1-dev libm17n-dev librsvg2-dev libotf-dev libxml2-dev \
libmagickwand-dev libc6-dev libtiff5-dev xaw3dg-dev \
zlib1g-dev libice-dev libsm-dev libxext-dev libxi-dev libxmu-dev \
libxmuu-dev libxrandr-dev libxt-dev libxtst-dev libxv-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 Linux desktop and 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).
Using open source VcXsrv:
- Download it from https://sourceforge.net/projects/vcxsrv/
- Install it by executing the exe
Install the Linux desktop
Using lxde (lightweight one):
sudo apt install lxde
Run Emacs
Run the xserver
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
Make a Shortcut
Make a shortcut to vcxsrv.exe and then change its target to:
"C:\Program Files\VcXsrv\vcxsrv.exe" :0 -multiwindow -clipboard -wgl
You can put this link into the startup folder to start it when booting. And stick it to the task bar to launch it from there.
Taken from https://superuser.com/a/1372940.
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
.
Start distribution & run Emacs in there
You can of course also run the distribution (after running the xserver) and then run Emacs in there. To start the disttribution (in this case lxde):
export DISPLAY=:0
export LIBGL_ALWAYS_INDIRECT=1
# OPTIONAL Set the keyboard layout to US
setxkbmap -layout us
startlxde
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:
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:
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.