Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VS Code under WSL

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Please indicate the source of the reprint: grape City official website, Grape City provides developers with professional development tools, solutions and services, empowering developers.

It was translated and sorted out by the grape city technical team.

Since Microsoft began to embrace open source, I think the two best features released by Microsoft are Visual Studio Code (VS Code) and Linux (WSL). With the combination of these two, it opens up a new way for software developers to write code.

WSL enables developers to run the Linux environment on Windows 10 without having to pay more overhead when using virtual machines. With WSL, we can install most official versions of Linux from the Windows App Store, and we can even run Bash shell scripts and Linux applications on its GUI.

This article provides detailed step-by-step instructions for WSL configuration so that we can run VS Code on Linux through WSL. Although the title of this article is "using VS Code under WSL", it also applies to other GUI applications.

The software used in this article includes:

Windows 10 1809

Ubuntu 18.04

Visual Studio Code 1.30.2

MebaXTerm 11.1

The contents of this article:

Enable WSL

Install Linux

Download and install MobaXterm

Start MobaXterm and open X Server

Install X Client on the installed Linux

Start VS Code

In addition to Windows 10 and VS Code, we also need X Server and X Client for VS Code and WSL to work together.

In the world of Linux, X Window System acts as the basic framework necessary to build a GUI environment. Using the client-server model, X Window System has been able to connect to remote computers in a graphical interface. Therefore, to connect to a remote Linux computer using our local computer, such as a laptop, we need:

Run the X Client application on a remote Linux machine

Run the X Server application on the local computer

The remote X client establishes a connection with the local X server and provides a graphical interface for the application. So with this technique, you can run VS Code using WSL.

X Server provides us with the graphical environment we want to run, and since the Bash of WSL does not support X Server, I chose the simpler MobaXterm here through selection.

Turn on the WSL function

Before we install the Linux distribution, we need to enable WSL. To do this, first enter "turn Windows on or off" on the search bar.

Then check "WSL" in the open window.

After clicking the OK button, we may need to restart the machine.

Now, we have enabled WSL on Windows 10.

(for more details on WSL, visit https://docs.microsoft.com/en-us/windows/wsl/install-win10)

Install Linux (Ubuntu)

If the WSL feature is enabled, then we can install Linux on Windows 10.

Search the Windows App Store for Ubuntu.

Start after the installation is complete

After starting Ubuntu, follow the instructions to create a user account.

After the Ubuntu is fully configured, we continue to install MobaXterm

Install and set up MobaXterm

The first choice is to download MobaXterm from the official website, which provides both the green version and the installation version. Which one to choose depends on personal preferences, there is little difference in actual use, I use the green version here. After the decompression is completed, start the program, and click to start X Server (figure below)

Then X Server starts.

Configure X Client

As mentioned at the beginning, X Client is the application we want to access remotely. In this article, the application is VS Code. To do this, follow these steps:

Start the command prompt

At the command prompt, type bash to enter Ubuntu bash.

Enter the command $export DISPLAY=localhost:0.0. We can also add this command to ~ / .bashrc so that we don't have to repeat this every time we log in, in.

Export DISPLAY=localhost:0.0 tells X Client the specific IP information of the application. Because we execute the command locally, we use localhost. If you are using a remote computer, please change the localhost section

Install VS Code

Now we can download VS Code and install it on Ubuntu. Here we use VS Code under Firefox, which can reduce a lot of trouble later.

The command is as follows:

$sudo apt update

$sudo apt install firefox

Start Firefox to download VS Code

The downloaded VS Code Debian package should be located in ~ / Download. Within the directory

$cd ~ / Download

$sudo dpkg-I code_1.30.2-1546901646_amd64.deb

Note: if the installation fails due to some dependency issues, install the dependent libraries first, as follows:

$sudo dpkg-I code_1.30.2-1546901646_amd64.debSelecting previously unselected package code. (Reading database... 42604 files and directories currently installed.) Preparing to unpack code_1.30.2-1546901646_amd64.deb. Unpacking code (1.30.2-1546901646). Dpkg: dependency problems prevent configuration of code: code depends on libnotify4; however: Package libnotify4 is not installed. Code depends on libnss3 (> = 2 Package libnss3 is not installed 3.26), however: Code depends on libxkbfile1; however: Package libxkbfile1 is not installed. Code depends on libgconf-2-4; however: Package libgconf-2-4 is not installed. Code depends on libsecret-1-0; however: Package libsecret-1-0 is not installed. Code depends on libxss1; however: Package libxss1 is not installed. Dpkg: error processing package code (--install): dependency problems-leaving unconfiguredProcessing triggers for mime-support (3.60ubuntu1)... Errors were encountered while processing: code

Install dependent libraries

Sudo apt install libnotify4 libnss3 libxkbfile1 libgconf-2-4 libsecret-1-0 libgtk-3-0 libxss1

If the following error occurs, try the sudo apt-fix-broken install command to reinstall

Reading package lists... DoneBuilding dependency treeReading state information... Donelibgtk-3-0 is already the newest version (3.22.30-1ubuntu1). Libgtk-3-0 set to manually installed.You might want to run 'apt-- fix-broken install' to correct these.The following packages have unmet dependencies: libgconf-2-4: Depends: gconf2-common (= 3.2.6-4ubuntu1) but it is not going to be installed Recommends: gconf-service but it is not going to be installed libnss3: Depends: libnspr4 (> = 21ubuntu1 4.12) but it is Not going to be installed libsecret-1-0: Depends: libsecret-common but it is not going to be installedE: Unmet dependencies. Try 'apt-fix-broken install' with no packages (or specify a solution).

After the installation is complete, start VS Code.

$code

Note: once we start VS Code, we may not be able to move or adjust the VS Code window. This is because custom tiles and menu bars on Linux have been enabled by default since VS Code 1.30. To avoid this problem, we need to change the setting to native.

First, turn on the VS Code settings. [file-> preferences-> Settings]

Second, type title bar on the search bar, and then select native.

Restart VS Code, and we should now be able to move and resize the VS Code window.

Conclusion

With WSL and X Server, we can install Linux on Windows 10 and run applications on Linux using a graphical interface. However, at this time, some people may ask, why are we doing this? Why not use virtual machines such as VirtualBox or VMware or other Linux machines directly on Windows? Of course, these methods are fine in most cases, but in some individual cases, they do not apply. For example:

Without a powerful workstation, running a virtual machine causes the machine to be slow.

There is no budget to buy another machine to run Linux.

Running applications with graphical interfaces on the cloud can be slow due to network delays.

WSL provides a simple and inexpensive solution for running Linux and Linux applications in our more familiar Windows 10 environment.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report