Using Salesforce CLI with Windows Subsystem for Linux in 10 Easy Steps

Update: As of Summer ’18 the CLI is no longer officially support within the Windows Subsystem for Linux. As you can see from the slightly tongue and cheek title, there is a lot of modification to the standard installation of Ubuntu within WSL to enable the browser to communicate with WSL. The following information applies to Ubuntu using Chrome as the browser. Various hardware systems, specifically graphics cards, may prevent this approach from working and is therefore not supportable.

The Salesforce CLI works great out of the box for Windows, Mac and a standalone Linux system. This means that it should work equally well in the Windows Subsystem for Linux (WSL) — and in fact, it does. But what makes it a bit tricky is that WSL runs headless. That means that there is no GUI for WSL out of the box. Without the GUI, it is difficult to initially authenticate to a hub org using the sfdx force:auth:web:login command.

WSL does allow the launch of other installed Windows applications, but for the communication back to the CLI after successfully logging in via the browser, the native Windows browsers are not useful.

To be able to both login via the browser and easily open orgs from WSL you need to install a few extra bits. A native browser implementation needs to be installed into the WSL distro, an XWindows server needs to be installed on the Windows 10 host and few changes need to be made to some of the software installed by default for WSL distro.

The steps below get you up and running with WSL and Salesforce DX. This can take anywhere from 30 minutes to an hour depending on your internet connection speeds.

Step 1

Enable WSL in Windows 10. This is done by using the Windows Features Control Panel applet. The easiest way to get to this screen is to enter “Turn Windows features on or off” in the search bar in your Cortana search. A Windows restart is required after enabling WSL.

Step 2

Download and install the Ubuntu distribution from the Microsoft Store. Once downloaded and installed, type Ubuntu in the Windows 10 search box and launch Ubuntu. This will install a fresh Ubuntu image. The installation takes a few minutes and after it finished you will be prompted for a new Linux username and password.

Step 3

Install Node Version Manager (NVM) for Linux and Node.js 10.0.0. If you haven’t used NVM before, it’s a tool that enables you for example to use different versions of node.js simultaneously on a machine as well as installing the Salesforce CLI without needing sudo.

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
nvm install 10.0.0

You can verify the install of npm and node.js by using the commands below.

npm -v
node --version

Step 4

Install and verify the Salesforce CLI. The first command will install the CLI as global npm module.

npm install -g sfdx-cli
sfdx --version

Step 5

WSL includes a version of OpenSSH that is insufficient for use with XWindows, so we need to remove the installed OpenSSH version and install the full OpenSSH version.

sudo apt-get update
sudo apt-get remove openssh-server
sudo apt-get install openssh-server

Step 6

Edit the OpenSSH configuration file as follows:

sudo nano /etc/ssh/sshd_config

Things to do in sshd_config:

  1. Add the following and replace yourusername with your Linux username: AllowUsers yourusername
  2. Change the default port from Port 22 to Port 2200
  3. Uncomment ListenAddress 0.0.0.0
  4. Change UsePrivilegeSeparation yes to UsePrivilegeSeparation no
  5. Change PermitRootLogin prohibit-password to PermitRootLogin no
  6. Change PasswordAuthentication no to PasswordAuthentication yes

Step 7

Install an XWindows Server, where Xming or VcXsrv are two good choices. I’ve had good luck with VcXsrv (and it’s free) so that is what we will use here. You can download a Windows installer from here. Launch the installer and accept all the defaults for installation.

Step 8

Install Google Chrome into the Ubuntu distribution. This would likely work with other browsers, though I’ve only tested with Google Chrome. The key is that it is natively installed into Ubuntu.

# Get the signing key
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

# set the repository
echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list

# install the package (these two steps can take a total of 10 minutes
sudo apt-get install google-chrome-stable

Step 9

Set the display in .bashrc by editing the file with this command:

nano .bashrc

Add the following line at the end of the file:

export DISPLAY=:0

Save the file and reload it for the current bash session:

source .bashrc

Step 10

Launch VcXrv with multiple windows and accept all defaults and verify that everything is working. Authenticate with your Dev Hub and create then a new scratch org based on the Dreamhouse sample app.

# Login to hub org and set as default dev hub user, be patient, browser will launch
sfdx force:auth:web:login -a HubOrg -d

# create scratch org and then open in browser
git clone https://github.com/dreamhouseapp/dreamhouse-sfdx
cd dreamhouse-sfdx
sfdx force:org:create -s -a myscratch -c config/project-scratch-def.json
sfdx force:org:open

At this point you can now use the Salesforce CLI like a champ from WSL in Windows 10.

For more on using the Salesforce CLI, check out the App Development with Salesforce DX module on Trailhead.

Published
May 15, 2018

Leave your comments...

Using Salesforce CLI with Windows Subsystem for Linux in 10 Easy Steps