{"id":12545,"date":"2017-02-08T12:00:43","date_gmt":"2017-02-08T19:00:43","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=12545"},"modified":"2020-01-08T18:50:06","modified_gmt":"2020-01-08T18:50:06","slug":"targeting-windows-subsystem-for-linux-from-visual-studio","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/targeting-windows-subsystem-for-linux-from-visual-studio\/","title":{"rendered":"Targeting the Windows Subsystem for Linux from Visual Studio"},"content":{"rendered":"<p>Update Jan. 8, 2020: Visual Studio 2019 version 16.1 and later has <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/c-with-visual-studio-2019-and-windows-subsystem-for-linux-wsl\/\">native support for WSL<\/a>. This eliminates the need to establish an SSH connection and is the recommended workflow for building and debugging on WSL.<\/p>\n<p><span style=\"font-family: Calibri;\"><span style=\"color: #000000;\">The <\/span><a href=\"https:\/\/msdn.microsoft.com\/en-us\/commandline\/wsl\/about\"><span style=\"color: #0563c1;\">Windows Subsystem for Linux (WSL)<\/span><\/a><\/span><span style=\"color: #000000; font-family: Calibri;\"> was first introduced at Build in 2016 and was delivered as an early beta in Windows 10 Anniversary Update. Since then, the WSL team has been hard at work, dramatically improving WSL\u2019s abilty to run an ever increasing number of native Linux command-line binaries and tools, including most mainstream developer tools, platforms and languages, and many daemons\/services* including MySQL, Apache, and SSH.<\/span><\/p>\n<p><span style=\"font-family: Calibri;\"><span style=\"color: #000000;\">With the Linux development with C++ workload in Visual Studio 2017 you can use the full power of Visual Studio for your C\/C++ Linux development. Because WSL is just another Linux system, you can target it from Visual Studio by following <\/span><a href=\"http:\/\/aka.ms\/vslinux\"><span style=\"color: #0563c1;\">our guide on using the Linux workload<\/span><\/a><\/span><span style=\"color: #000000; font-family: Calibri;\">. \u00a0This gives you a lot of flexibility to keep your entire development cycle locally on your development machine without needing the complexity of a separate VM or machine. It is, however, worth covering how to configure SSH on Bash\/WSL in a bit more detail.<\/span><\/p>\n<h2><span style=\"color: #2f5496; font-family: Calibri Light; font-size: x-large;\">Install WSL<\/span><\/h2>\n<p><span style=\"font-family: Calibri;\"><span style=\"color: #000000;\">If you\u2019ve not already done so, you\u2019ll first need to <\/span><a href=\"https:\/\/msdn.microsoft.com\/en-us\/commandline\/wsl\/install_guide\"><span style=\"color: #0563c1;\">enable developer mode and install WSL itself<\/span><\/a><\/span><span style=\"color: #000000; font-family: Calibri;\">. This only takes a few seconds, but does require a reboot. You need to be on a current Insider build or wait for the release of\u00a0Creators Update to\u00a0use WSL with the Linux workload in VS.<\/span><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Calibri;\">When you run Bash for the first time, you\u2019ll need to follow the on-screen instructions to accept Canonical\u2019s license, download the Ubuntu image, and install it on your machine. You\u2019ll then need to choose a UNIX username and password. This needn\u2019t be the same as your Windows login username and password if you prefer. You\u2019ll only need to enter the UNIX username and password in the future when you use <\/span><span style=\"font-family: Consolas;\">sudo<\/span><span style=\"font-family: Calibri;\"> to elevate a command, or to login \u201cremotely\u201d (see below).<\/span><\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bash-install.png\"><img decoding=\"async\" class=\"aligncenter size-mediumlarge wp-image-12565\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bash-install-500x282.png\" alt=\"Setting up WSL\" width=\"500\" height=\"282\" \/><\/a><\/p>\n<p><span style=\"color: #000000; font-family: Calibri;\">Now you\u2019ll have a vanilla Ubuntu instance on your machine within which you can run any ELF-64 Linux binary, including those that you download using apt-get!<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Calibri;\">Before we continue, let\u2019s install the build-essential package so you have some key developer tools including the GNU C++ compiler, linker, etc.:<\/span><\/p>\n<p><code><span style=\"color: #000000;\">$ sudo apt install -y build-essential<\/span><\/code><\/p>\n<p><span style=\"color: #000000; font-family: Calibri;\">We&#8217;ll also need to install gdbserver, a program that allows you to debug with a remote GDB debugger.<\/span><\/p>\n<p><code><span style=\"color: #000000;\">$ sudo apt install -y gdbserver<\/span><\/code><\/p>\n<h2><span style=\"color: #2f5496; font-family: Calibri Light; font-size: x-large;\">Install &amp; configure SSH<\/span><\/h2>\n<p><span style=\"color: #000000; font-family: Calibri;\">Let\u2019s use the \u2018apt\u2019 package manager to download and install SSH on Bash\/WSL:<\/span><\/p>\n<p><code><span style=\"color: #000000;\">$ sudo apt install -y openssh-server<\/span><\/code><\/p>\n<p><span style=\"color: #000000; font-family: Calibri;\">Before we start SSH, you will need to configure SSH, but you only need to do this once. Run the following commands to edit the sshd config file:<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Consolas;\">$ sudo nano \/etc\/ssh\/sshd_config<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Calibri;\">Scroll down the \u201cPasswordAuthentication\u201d setting and make sure it\u2019s set to \u201cyes\u201d:<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bash-nanosshd.png\"><img decoding=\"async\" class=\"aligncenter size-mediumlarge wp-image-12585\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bash-nanosshd-454x350.png\" alt=\"Editing sshd_config in nano\" width=\"454\" height=\"350\" \/><\/a><\/p>\n<p>Hit CTRL + X to exit, then Y to save.<\/p>\n<p>Now generate SSH keys for the SSH instance:<\/p>\n<p>$ sudo ssh-keygen -A<\/p>\n<p>Start SSH before connecting from Visual Studio:<\/p>\n<p>$ sudo service ssh start<\/p>\n<p><strong>*Note: You will need to do this every time you start your first Bash console. As a precaution, WSL currently tears-down all Linux processes when you close your last Bash console!<\/strong>.<\/p>\n<h2><span style=\"color: #2f5496; font-family: Calibri Light; font-size: x-large;\">Install &amp; configure Visual Studio<\/span><\/h2>\n<p><span style=\"font-family: Calibri;\"><span style=\"color: #000000;\">For the best experience, we recommend installing <\/span><a href=\"https:\/\/www.visualstudio.com\/vs\/visual-studio-2017-rc\/\"><span style=\"color: #0563c1;\">Visual Studio 2017 RC<\/span><\/a><\/span><span style=\"color: #000000; font-family: Calibri;\"> (or later) to use Visual C++ for Linux. Be sure to select the Visual C++ for Linux workload during the installation process.<\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bashinstalllinux.png\"><img decoding=\"async\" class=\"aligncenter size-mediumlarge wp-image-12575\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bashinstalllinux-500x293.png\" alt=\"Visual Studio installer with Linux C++ workload\" width=\"500\" height=\"293\" \/><\/a><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Calibri;\">Now you can connect to the Windows Subsystem for Linux from Visual Studio by going to Tools\u00a0&gt;<\/span><span style=\"font-family: Calibri;\"> Options\u00a0&gt;<\/span><span style=\"font-family: Calibri;\"> Cross Platform &gt; <\/span><span style=\"font-family: Calibri;\">Connection Manager. Click add and enter \u201clocalhost\u201d for the hostname and your WSL user\/password.<\/span><\/span><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bashconnectionmanager.png\"><img decoding=\"async\" class=\"aligncenter size-mediumlarge wp-image-12555\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/bashconnectionmanager-500x290.png\" alt=\"VS Connection Manager with WSL\" width=\"500\" height=\"290\" \/><\/a><\/p>\n<p><span style=\"color: #000000;\"><span style=\"font-family: Calibri;\">Now you can use this connection with any of your existing C++ Linux projects or create a new Linux project under File\u00a0&gt;<\/span><span style=\"font-family: Calibri;\"> New Project\u00a0&gt;<\/span><span style=\"font-family: Calibri;\"> Visual C++\u00a0&gt;<\/span><span style=\"font-family: Calibri;\"> Cross Platform\u00a0&gt;<\/span><span style=\"font-family: Calibri;\"> Linux.<\/span><\/span><\/p>\n<p><span style=\"color: #000000; font-family: Calibri;\">In the future, we\u2019ll publish a more detailed post showing the advantages of working with WSL, particularly leveraging the compatibility of binaries built using the Linux workload to deploy on remote Linux systems.<\/span><\/p>\n<p><span style=\"color: #000000; font-family: Calibri;\">For now, now that, starting with Windows 10 Creators Update, Bash on the Windows Subsystem for Linux (Bash\/WSL) is a real Linux system from the perspective of Visual Studio. <\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update Jan. 8, 2020: Visual Studio 2019 version 16.1 and later has native support for WSL. This eliminates the need to establish an SSH connection and is the recommended workflow for building and debugging on WSL. The Windows Subsystem for Linux (WSL) was first introduced at Build in 2016 and was delivered as an early [&hellip;]<\/p>\n","protected":false},"author":677,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[216,279],"tags":[],"class_list":["post-12545","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-documentation","category-linux"],"acf":[],"blog_post_summary":"<p>Update Jan. 8, 2020: Visual Studio 2019 version 16.1 and later has native support for WSL. This eliminates the need to establish an SSH connection and is the recommended workflow for building and debugging on WSL. The Windows Subsystem for Linux (WSL) was first introduced at Build in 2016 and was delivered as an early [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/12545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/677"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=12545"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/12545\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=12545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=12545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=12545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}