Building and Running VSCode on a RaspberryPi 3

Your mileage may vary


As part of a DIY robotics project I am tinkering with, I recently discovered that it is possible to run Visual Studio Code on a Raspberry Pi 3.  There were a couple of posts about how to write code using .Net Core that can control GPIO pins.  Which is the sort of thing I will need to work with a motor controller.  One approach for that is to use a Windows 10 computer to write and deploy code with VS Code to the Raspberry Pi over Putty or by some other means.  After hearing that VS Code can actually be built on the Raspberry Pi, my thought was, ok, well, why not write the code directly on the device?

This turned out to be more difficult than I had hoped.  My mileage did vary from a few existing how-to posts on the internet.  This post hopes to provide a little more detail.

Sources

Thanks to Gunjan Datta for the excellent post in December of 2016 on how to do this (here).

Thanks to Scott Hanselman for the inspirational post in March of 2016 on how to do this (here).

There is also a neat little gist with some useful resources (here).

Requirements

First up, hardware and software versions.  There are countless articles about how to provide keyboard, mouse, an HDMI monitor, and power to the Raspberry Pi and most of this post assumes a basic understanding of how to get started with the Raspberry Pi 3.  But if you want a resource for getting started, that is located (here).

I used a Raspberry Pi Model 3 B. Which has a Quad Core 1.2GHz CPU, 1GB RAM, 40 GPIO Pins, 4x USB Ports, 4 Pole Stereo Output, HDMI Port, Ethernet, WiFi, Bluetooth Low Energy (BLE), and a Micro SD Card Slot.

Using the NOOBS operating system preinstalled on a 16GB MicroSD card, I chose to install the Raspbian OS and I specified English (US) as the language and US as the keyboard layout.  The installation wizard is available on first boot up by holding down your shift key.

raspbianos.png

Once the Raspbian OS is installed, it makes good sense to update and upgrade it to the latest available version.  Make sure you have a good WiFi connection or are connected to the internet via the ethernet.  The how to guide for that is located (here).

Here are the two main steps outlined on the link above. Open up a terminal and run the following two commands:

sudo apt-get update
sudo apt-get dist-upgrade

The upgrade process takes a while.  After that is done, Raspbian is all up to date and it's time to get the prerequisites.

Dependencies

NodeJS is required for the VSCode build.  There is a current how-to set of commands to do that (here).

Here are the commands to install NodeJS (for the version that is current at the time of this post):

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs

I also chose to install the optional build tools:

sudo apt-get install -y build-essential

Git VSCode

Next up, use git to clone the VSCode repo.  You can see all the source code for VSCode up at https://github.com/microsoft/vscode

git clone https://github.com/microsoft/vscode

This creates a directory at the current location of the terminal.  The new directory is "vscode" and in order to begin running a build, change directory to it.

cd vscode

Now it's time to build VSCode on the Raspberry Pi 3.  Use the following command at the vscode directory:

./scripts/npm.sh install --arch=armhf

Mistakes were made

This where the fun begins.  Your mileage may vary.  My experience definitely required a few more steps than the original posts talked about.  Which makes sense because it has been about a year since they were written.

There were several missing packages and the build failed at each one.

The first error, "yarn: command not found".

Solution, use npm to install yarn globally: 

sudo npm install -g yarn

The second error, "Package libsecret-1 was not found in the pkg-config search path."

Solution:

sudo apt-get install libsecret-1-dev

The third error: "Package xkbfile was not found in the pkg-config search path."

Solution:

sudo apt-get install libxkbfile-dev

 While the following command was included on the blog posts listed earlier, it didn't seem to be required in the steps I outlined above.

sudo apt-get install libx11-dev

Start up VSCode

Ok, time to run VSCode!  In the vscode directory, run the following command:

./scripts/code.sh

 And VOILA!!  After a few minutes, VSCode is up and running on a RaspberryPi 3.

vscodeonraspbianos.png

Extra Credit

Here are some nice enhancements to help start VSCode more easily the next time:

 - From the Raspian Start Menu, choose Preferences/Main Menu Editor

 - Click on Programming

 - Click on New Item

 - Set the Name field to "VSCode"

 - Set the Command field by browsing to: "/home/pi/vscode/scripts/code.sh" (or using the path where the VSCode repo was cloned).

 - Set the application icon using an ico file located in the /vscode/resources directory.

 - Update the /vscode/product.json file to include the extensionsGallery settings:

"extensionsGallery":{ 
  "serviceUrl": "https://marketplace.visualstudio.com/_apis/public/gallery", 
  "cacheUrl": "https://vscode.blob.core.windows.net/gallery/index", 
  "itemUrl": "https://marketplace.visualstudio.com/items" }