Working with Visual Studio Code on Ubuntu on WSL2

Key Value
Summary Working with Visual Studio Code on Ubuntu on WSL2
Categories wsl2
Difficulty 2
Author oliver.smith@canonical.com

Note: This doc is not being maintained anymore. Please see instead https://canonical-ubuntu-wsl.readthedocs-hosted.com/ .

Overview

Duration: 2:30

Windows Subsystem for Linux (WSL) allows you to leverage the benefits of Linux package management and command line tools to streamline your development workflow. This is particularly useful for web developers and data scientists.

The easiest way to access your Ubuntu development environment in WSL is using Visual Studio Code via the built in Remote extension.

In this tutorial, you will learn:

  • How to set up Visual Studio Code for remote development on Ubuntu on WSL
  • How to start a basic Node.js webserver on Ubuntu using Visual Studio Code

What you will need:

  • A PC with Windows 10 or 11
  • (Optional) This tutorial uses Windows Terminal Preview, which you can get from the Windows app store

Install Ubuntu on WSL2

Duration: 5:00

This tutorial assumes that you already have WSL2 installed with the latest LTS release of Ubuntu.

If not, check out our getting started tutorials for Windows 10 and Windows 11:

Once you have completed the relevant tutorial, the following steps will work on either Windows 10 or 11.

Install Visual Studio Code on Windows

One of the advantages of WSL is that it can interact with the native Windows version of Visual Studio Code using its remote development extension.

To install Visual Studio Code visit the Microsoft Store and search for Visual Studio Code.

Then click Install.

Alternatively, you can install Visual Studio Code from the web link here.

During installation, under the Additional Tasks step, ensure the Add to PATH option is checked.

Once the installation is complete, open Visual Studio Code.

Install the Remote Development Extension

Duration: 1:00

Navigate to the Extensions menu in the sidebar and search for Remote Development.

This is an extension pack that allows you to open any folder in a container, remote machine, or in WSL. Alternatively, you can just install Remote - WSL.

Once installed we can test it out by creating an example local web server with Node.js

Install Node.js and create a new project

Duration: 3:00

Open your WSL Ubuntu terminal and ensure everything is up to date by typing:

sudo apt update

And then

sudo apt upgrade

Entering your password and pressing Y when prompted.

Next, install Node.js and npm:

sudo apt-get install nodejs

sudo apt install npm

Press Y when prompted.

Now, create a new folder for our server.

mkdir serverexample/

Then navigate into it:

cd serverexample/

Now, open up your folder in Visual Studio Code, you can do this by typing:

code .

The first time you do this, it will trigger a download for the necessary dependencies:

Once complete, your native version of Visual Studio Code will open the folder.

Creating a basic web server

Duration: 3:00

In Visual Studio Code, create a new file called package.json and add the following text (original example)

{

"name": "Demo",

"version": "1.0.0",

"description": "demo project.",

"scripts": {

"lite": "lite-server --port 10001",

"start": "npm run lite"

},

"author": "",

"license": "ISC",

"devDependencies": {

"lite-server": "^1.3.1"

}

}

Save the file and then, in the same folder, create a new one called index.html

Add the following text, and then save and close:

<h1>Hello World<h1>

Now return to your Ubuntu terminal (or use the Visual Studio Code terminal window) and type the following to install a server defined by the above specifications detailed in package.json:

npm install

Finally, type the following to launch the web server:

npm start

You can now navigate to localhost:10001 in your native Windows web browser by using CTRL+Left Click on the terminal links.

That’s it!

By using Ubuntu on WSL you’re able to take advantage of the latest Node.js packages available on Linux as well as the more streamlined command line tools.

Enjoy Ubuntu on WSL!

Duration: 1:00

That’s all folks! In this tutorial, we’ve shown you how to connect the Windows version of Visual Studio Code to your Ubuntu on WSL filesystem and launch a basic Node.js webserver.

We hope you enjoy using Ubuntu inside WSL. Don’t forget to check out our other tutorials for tips on how to optimise your WSL setup for Data Science.

Further Reading

1 Like

Maybe, try another demo. lite-server is having issues. https://github.com/advisories/GHSA-89w7-5q45-r53w

Thanks

Love the demo :slight_smile: I am still learning. Is this a fix for the vulnerability I came across?

  1. Uninstall lite-server by running the command npm uninstall lite-server.
  2. Install http-server by running the command npm install --save-dev http-server.
  3. In your project’s package.json file, find the scripts section and replace the scripts section. You can update the scripts section of your package.json file as follows:

“scripts”: {
“start”: “http-server -p 10001”
}

  1. You can then start the local server by running the command npm start.

  2. Hit “CTRL-C” to stop the server

  3. Run command $ npm audit

image