如何在 VS Code 终端上使用 Windows 上的 Ubuntu (WSL) 上的 Bash?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44450218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:33:32  来源:igfitidea点击:

How do I use Bash on Ubuntu on Windows (WSL) for my VS Code terminal?

gitbashubuntuvisual-studio-codewindows-subsystem-for-linux

提问by taswyn

While other questions deal with how to use things like git-bash, giving the new WSL a spin as the terminal for VS Code is not the same: it gives you access to bash running on an actual Ubuntu Linux subsystem, instead of the git-bash terminal running on the Windows subsystem.

虽然其他问题涉及如何使用 git-bash 之类的东西,但让新的 WSL 旋转,因为 VS Code 的终端是不一样的:它使您可以访问在实际 Ubuntu Linux 子系统上运行的 bash,而不是 git-在 Windows 子系统上运行的 bash 终端。

So how do we get it working as the VS Code terminal, and particularly how do we get it working as a functional dev environment terminal?

那么我们如何让它作为 VS Code 终端工作,特别是我们如何让它作为一个功能性开发环境终端工作?

Unlike git-bash, this is unfortunately not as simple, because the Ubuntu Linux configuration in WSL can provide some gotchas, such as NPM trying (and failing) to run from your Windows Program Files directory due to the interaction between WSL and Windows itself in terms of pathing, and some packages such as Compass failing for what are not necessarily immediately obvious reasons to someone who is not used to developing on Linux. What's a simple way to have a reliable WSL terminal environment for VS Code where most frequently used tools will run when installed via apt-getor npm?

与 git-bash 不同,不幸的是,这并不那么简单,因为 WSL 中的 Ubuntu Linux 配置可以提供一些问题,例如由于 WSL 和 Windows 本身之间的交互,NPM 尝试(并失败)从您的 Windows Program Files 目录运行路径方面的术语,以及一些软件包(例如 Compass)失败的原因对于不习惯在 Linux 上开发的人来说不一定是显而易见的原因。有什么简单的方法可以为 VS Code 提供可靠的 WSL 终端环境,其中最常用的工具将在通过apt-get或安装时运行npm

回答by taswyn

This answer seeks to help others avoid spending 1-2 hours troubleshooting and slowly finding disparate solutions for common problems when using WSL for the terminal in VS Code. It does not cover installing specific packages, but rather common ones that may not properly install as dependencies when installing things that do rely on their presence, and on fixing related common settings.

此答案旨在帮助其他人在 VS Code 中将 WSL 用于终端时,避免花费 1-2 小时进行故障排除并慢慢找到针对常见问题的不同解决方案。它不包括安装特定的包,而是在安装依赖于它们的存在的东西时可能无法正确安装为依赖项的常见包,以及修复相关的通用设置。

Summary of steps

步骤总结

  • WSL installed
  • VS Code (or other IDE) configured for terminal
  • NPM installed & path fixin .profile (may help with other tools)
  • build-essential installed (helps with any tools that use make/gcc/etc)
  • VS Code Tasks using WSL
  • Extras
  • 安装了 WSL
  • 为终端配置的 VS Code(或其他 IDE)
  • .profile 中的NPM 安装和路径修复(可能有助于其他工具)
  • 已安装 build-essential(有助于使用 make/gcc/etc 的任何工具)
  • 使用 WSL 的 VS Code 任务
  • 附加功能

Getting Started & Requirements

入门和要求

  • You must have WSL installed. (Which means you must be running 64 bitWindows 10, with the appropriate updates) Follow the install guideif not already installed. This will require some reboots.
  • 您必须安装 WSL。(这意味着您必须运行64 位Windows 10,并带有适当的更新)如果尚未安装,按照安装指南进行操作。这将需要一些重新启动。

VS Code Terminal Configuration

VS Code 终端配置

Either the CTRL+,keyboard shortcut, or FilePreferencesSettings

无论是CTRL+,键盘快捷键,或File→交通Preferences→交通Settings

In the top right of the editing window, make sure you are working in the correct context for you: either User Settingsor Workspace Settings.

在编辑窗口的右上角,确保您在正确的上下文中工作:用户设置工作区设置

enter image description here

在此处输入图片说明

In the settings search bar, type terminal.integrated.shell.windows(or whatever gets you far enough long)

在设置搜索栏中,输入terminal.integrated.shell.windows(或任何让你足够长的东西)

Find the setting in the actual settings file, use Edit(mouse over the line, it will be on the left: on a touch screen without a mouse, you should be able to simply tap to the left of the line), and select Replace in Settings

在实际设置文件中找到设置,使用Edit(将鼠标悬停在该行上,它将在左侧:在没有鼠标的触摸屏上,您应该可以简单地点击该行的左侧),然后选择Replace in Settings

enter image description here

在此处输入图片说明

In the right pane, modify the entry created in the modified json file: replace the previous setting with

在右侧窗格中,修改在修改后的 json 文件中创建的条目:将之前的设置替换为

"C:\WINDOWS\Sysnative\bash.exe"

enter image description here

在此处输入图片说明

Other IDEs: IntelliJ

其他 IDE:IntelliJ

Open Settings/Tools/Terminal and set the "Shell path" field to "C:\Users\USERNAME\AppData\Local\Microsoft\WindowsApps\ubuntu.exe"

打开设置/工具/终端并将“外壳路径”字段设置为 "C:\Users\USERNAME\AppData\Local\Microsoft\WindowsApps\ubuntu.exe"

Making your WSL Ubuntu Bash Terminal functional for dev

使您的 WSL Ubuntu Bash 终端为开发人员工作

When you use CTRL+`to open the terminal, you should now have a bash terminal.

当您使用CTRL+`打开终端时,您现在应该有一个 bash 终端。

If this is the first time you have run bash.exe, you may be asked about installing Ubuntu. Do so. Once your installation is complete, choose your username and password to be used in WSL Ubuntu. These do not have to coincide with your current Windows account, and it's important to note that they will not change based on changes to your Windows account's password.

如果这是您第一次运行 bash.exe,可能会询问您是否安装 Ubuntu。这样做。安装完成后,选择要在 WSL Ubuntu 中使用的用户名和密码。这些不必与您当前的 Windows 帐户一致,重要的是要注意它们不会根据您的 Windows 帐户密码的更改而更改。

Once you are done, you will have a bash command prompt in your terminal.enter image description here

完成后,您的终端中将有一个 bash 命令提示符。在此处输入图片说明

Note that unlike git-bash on Windows, this is a separate environment. While it can be used to launchWindows software outside of itself, you will need appropriate Ubuntu packages to run them within the actual terminal.

请注意,与 Windows 上的 git-bash 不同,这是一个单独的环境。虽然它可用于在其自身之外启动Windows 软件,但您需要适当的 Ubuntu 软件包才能在实际终端中运行它们。

Currently, WSL does not come loaded with everything you might expect or be used to having, and some things can conflict with software you have loaded in Windows, based on default profile settings.

目前,WSL 并未加载您可能期望或习惯拥有的所有内容,并且根据默认配置文件设置,某些内容可能与您在 Windows 中加载的软件冲突。

Updates & git

更新 & git

Note:I'm going to document these as sudo for people who simply need single pieces of this, but one option at the start is to instead sudo suand simply running the following commands without sudo.

注意:我将把这些记录为 sudo,供那些只需要其中一部分的人使用,但一开始的一个选择是改为sudo su简单地运行以下命令而不使用 sudo。

Make sure your Ubuntu packagesare up to date:

确保您的 Ubuntu 软件包是最新的:

sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade
sudo apt autoremove

Install git:

安装git:

sudo apt-get install git

Node.js & NPM

Node.js 和 NPM

If you already have Node or NPM loaded in Windows, running them in Ubuntu can get problematic due to path issues. So, you need to install the Ubuntu native versions and ensure that they are used instead.

如果您已经在 Windows 中加载了 Node 或 NPM,在 Ubuntu 中运行它们可能会由于路径问题而出现问题。因此,您需要安装 Ubuntu 本机版本并确保使用它们。

First, install node.js with NPM. (alternate: install NVMand use it to install node.js)

首先,与NPM安装的node.js。(替代方法:安装 NVM并使用它来安装 node.js)

After installing, running npm commands will probably fail: for example, npm -vwill probably give you:

安装后,运行 npm 命令可能会失败:例如,npm -v可能会给你:

: not foundram Files/nodejs/npm: 3: /mnt/c/Program Files/nodejs/npm:
: not foundram Files/nodejs/npm: 5: /mnt/c/Program Files/nodejs/npm:
/mnt/c/Program Files/nodejs/npm: 6: /mnt/c/Program Files/nodejs/npm: Syntax error: word unexpected (expecting "in")

This is due to a pathing issue with a fairly straightforward solution. Using your favorite CLI editor (such as nano, vim, emacs, catand sed… etc), open your ~/.profile

这是由于具有相当简单的解决方案路径问题。使用您喜欢的CLI编辑器(如nanovimemacscatsed...等),打开你的~/.profile

nano ~/.profile

Note: do NOT attempt to edit Linux files using Windows tools.(Thanks to @david-c-rankin's commentfor the official link with the bold red text explaining this) If you don't want to use a CLI editor for this in the terminal, see the bottom of this post for a link on how to get a GUI one running.

注意:不要尝试使用 Windows 工具编辑 Linux 文件(感谢@david-c-rankin对官方链接的评论,并用粗体红色文本解释了这一点)如果您不想在终端中为此使用 CLI 编辑器,请参阅本文底部的链接,了解如何让一个 GUI 运行。

Currently, the default bash PATH variable in WSL is

目前,WSL 中的默认 bash PATH 变量是

PATH="$HOME/bin:$HOME/.local/bin:$PATH"

Which is injecting the windows path after the first two binary directories. Unfortunately, this doesn't result in /usr/bin being used before the windows installed npm, so add that before the final $PATH:

这是在前两个二进制目录之后注入 windows 路径。不幸的是,这不会导致在 Windows 安装 npm 之前使用 /usr/bin,因此在最终 $PATH 之前添加它:

PATH="$HOME/bin:$HOME/.local/bin:/usr/bin:$PATH"

Save out, and then either reload the terminal or just source the path file

保存,然后重新加载终端或仅获取路径文件

source ~/.profile

Build-essential

构建必备

If you are using anything which requires compiling or otherwise uses make, it's almost guaranteed that you will need these installed; so if you did not install them while installing node.js, do so. It's much easier to simply use the build-essential packagerather than try to install everything separately.

如果您正在使用任何需要编译或以其他方式使用 make 的东西,几乎可以保证您需要安装它们;因此,如果您在安装 node.js 时没有安装它们,请这样做。简单地使用build-essential 包比尝试单独安装所有东西要容易得多。

Note that packages such as Compass which rely on Ruby FFI will fail without these. If you are having trouble properly installing and running a tool, making sure you have gcc and make installed can be a good place to start.

请注意,依赖 Ruby FFI 的 Compass 等软件包将在没有这些的情况下失败。如果您在正确安装和运行工具时遇到问题,请确保安装了 gcc 和 make 是一个不错的起点。

sudo apt-get install -y build-essential

Running Tasks using Ubuntu

使用 Ubuntu 运行任务

Note that if you use VS Code's tasks.json to run build tasks, by default it will still run them using the Windows subsystem instead of the Ubuntu one. Sometimesthis may be what you want, but if you've just finished installing grunt-cli in Ubuntu and not Windows, it's probably not.

请注意,如果您使用 VS Code 的 tasks.json 来运行构建任务,默认情况下它仍会使用 Windows 子系统而不是 Ubuntu 子系统来运行它们。有时这可能是您想要的,但如果您刚刚在 Ubuntu 而非 Windows 中安装了 grunt-cli,则可能不是。

VS Code recently had the 2017 May update to how Tasks work that allows it to set the task runner as the terminal. This is by far the easiest way to migrate tasks over.

VS Code 最近在 2017 年 5 月更新了 Tasks 的工作方式,允许将任务运行器设置为终端。这是迄今为止迁移任务的最简单方法。

Simply set

简单设置

"runner": "terminal",

in your tasks.jsonand you're done (assuming you have all of the appropriate tools you are trying to run now installed in WSL Ubuntu).enter image description here

在你的tasks.json和你已经完成了(假设你有你正在尝试运行的所有适当的工具,现在安装在 WSL Ubuntu 中)。在此处输入图片说明

This is veryportable, ideally requiring no changes between systems which do or do not have WSL, or to other OSes, and is the method I would recommend.

这是非常便携的,理想情况下不需要在有或没有 WSL 的系统或其他操作系统之间进行更改,这是我推荐的方法。

As of the moment, this method spawns another TERMINALtab instance (accessed from the drop down). You can still set up appropriate watchers, but it does mean that it's no longer sitting on the OUTPUTtab.

目前,此方法生成另一个TERMINAL选项卡实例(从下拉列表中访问)。您仍然可以设置适当的观察者,但这确实意味着它不再位于OUTPUT选项卡上。

The old method is capable of invoking the WSL Ubunutu Bash shell and having it show up in OUTPUT, and involves either calling bash.exe with the -c argument or using a shell script. It unfortunately is not as semantic, since we are making bashour command and passing it what we want to run as an argument instead. This also means it is not as quickly portable to other systems.

旧方法能够调用 WSL Ubunutu Bash shell 并将其显示在 中OUTPUT,并且涉及使用 -c 参数调用 bash.exe 或使用 shell 脚本。不幸的是,它不是语义化的,因为我们正在创建bash我们的命令并将我们想要运行的作为参数传递给它。这也意味着它无法快速移植到其他系统。

You can use the same location you gave VS Code earlier for the terminal itself, C:\\WINDOWS\\Sysnative\\bash.exeas the value for commandenter image description here

您可以使用您之前为终端本身提供 VS Code 的相同位置,C:\\WINDOWS\\Sysnative\\bash.exe作为command在此处输入图片说明

Set the first element of the argsarray as -cand the second as the command you want to run (credit to the second half of this answer).

args数组的第一个元素设置-c为您要运行的命令,将第二个元素设置为要运行的命令(归功于本答案的后半部分)。

Alternatively, you can instead run a shell script as seen here.

或者,您可以改为运行 shell 脚本,如下所示

Further helpful bits

更多有用的位

Want to start VSCode in Windows from the WSL Bash command line?

从 WSL Bash 命令行在 Windows 中启动 VSCode吗?

Want to have a graphical interface for your WSL Ubuntu? (this will allow you to do things like use a Linux GUI editor for files withinthe Ubuntu system itself: do not edit them using Windows editing tools, see comments/note in section on npm)

想要为您的 WSL Ubuntu提供图形界面吗?(这将允许您在 Ubuntu 系统本身对文件使用 Linux GUI 编辑器:不要使用 Windows 编辑工具编辑它们,请参阅 npm 部分中的注释/注释)

Want to build (see the above portion on setting up VS Code Tasks properly for WSL) and debug entirely within WSL Ubuntu? (this shows how to do so using gdb, but the pipeTransportconcept could be used with other debuggers) (credit to this answer, but the one before it also provides a method using local loopback that could prove useful)

想要完全在 WSL Ubuntu 中构建(请参阅上面有关为 WSL 正确设置 VS Code 任务的部分)和调试?(这显示了如何使用 gdb 执行此操作,但该pipeTransport概念可以与其他调试器一起使用)(归功于此答案,但之前的那个也提供了一种使用本地环回的方法,该方法可能很有用)

回答by Hyeon ki Hong

if you want to use zsh, find path of ubuntu1804.exe or ubuntu1604.exe.

如果要使用 zsh,请找到 ubuntu1804.exe 或 ubuntu1604.exe 的路径。

in my case

就我而言

"terminal.external.windowsExec": "C:\Users\asdf\AppData\Local\Microsoft\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\ubuntu1804.exe",
"terminal.integrated.shell.windows": "C:\Users\asdf\AppData\Local\Microsoft\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\ubuntu1804.exe",

回答by Almenon

Now you can use Microsoft's WSL extension.

现在您可以使用Microsoft 的 WSL 扩展