Skip to content

Instantly share code, notes, and snippets.

@ckunte
Last active May 1, 2023 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckunte/eef5df56dc94941655e168575bda247b to your computer and use it in GitHub Desktop.
Save ckunte/eef5df56dc94941655e168575bda247b to your computer and use it in GitHub Desktop.
Access linux environment from within Windows (via Windows Subsystem for Linux)

Access linux environment from within Windows

via Windows Subsystem for Linux (WSL)

Ubuntu (a debian linux distribution) in Windows Terminal powered by WSL.

Ubuntu (a debian linux distribution) in Windows Terminal powered by WSL.

Purpose

The purpose of this brief is to enable technical and non-technical users interested in accessing a linux environment for the purposes of automation, data-science, engineering, productivity, etc.

How to access linux from within Windows

With Windows subsystem for linux (WSL) — a virtualisation technology developed and bundled with Windows operating system by Microsoft — this is now possible. It enables Windows users to have native, unmodified access to a linux distribution via the command-line shell.

It does not require leaving the comfort of Windows, the need for a disk partition and dual-boot, the complications of setting-up a linux-specific computer (server, blade, or instance), or the challenges of exchanging data between Windows and non-Windows computers, even within sanitised corporate environments.

With bilateral access to disk drives and files, and with speeds as fast as running linux bare metal, WSL offers seamless inter-operability between Windows and Linux — on Windows. Files created in Windows for instance can be generated, read, edited, and processed in WSL and vice versa, see WSL documentation by Microsoft and Canonical for more information.

Install

WSL and Ubuntu

WSL is an optional feature that can be enabled on demand from within Windows. It requires either Windows 10 version 2004 and higher (Build 19041 and higher), or Windows 11. The process to get a linux environment via WSL up and running on a Windows computer is as follows.

  1. Install WSL from either Microsoft Store or the Software Center.1 Microsoft Update Windows Subsystem for Linux 2.0. Version 2.0 is recommended, as this is fast, runs linux utils like a native, and without requiring either emulation, translation, or pre-compiled binaries.
  2. Check for availability in Windows PowerShell with wsl --help.
  3. Get IT Support to run as admin., if appropriate2: wsl --install -d Ubuntu
  4. Set username and password when prompted at the end of step 4.3
  5. Restart Windows.

python, numpy, matplotlib, and pandas

To install these, the terminal needs access to open source repositories on the internet.

# Check and update the system
sudo apt update -y && sudo apt full-upgrade -y
# Install pandas and matplotlib (this also installs numpy)
python3 -m pip install --upgrade pandas matplotlib

For access to internet via proxy, advanced terminal users can set proxy servers by adding the following to ~/.bashrc file. (In the snippet below, replace the placeholder url:port with the actual url and port information.)

# Set Proxy
function setproxy() {
    export {http,https,ftp}_proxy="url:port"
}
# Unset Proxy
function unsetproxy() {
    unset {http,https,ftp}_proxy
}
# Activate proxy
setproxy

Optional tools

For a pleasant linux command-line experience, one can optionally install the following:

  • Windows Terminal via Microsoft Store
  • Visual Studio Code via user or portable install
  • Remote Development Extension Pack in Visual Studio Code

Disk access

WSL Ubuntu home drive, for example, is virtually mounted at the following address, which is accessible from Windows Explorer:

# WSL path from Windows Explorer
\\wsl$\Ubuntu\home\<wsl_username>

Windows file-system is mounted in WSL as follows, which is accessible from Bourne-Again SHell (bash):

# Windows path from WSL
/mnt/c/Users/<windows_username>

where, <wsl_username> is the username set during WSL installation (step 5), while <windows_username> is the username in Windows system. They need not be the same.

This makes it trivial to then map files and folders, e.g., using the symbolic linking tool, ln. To map Windows Documents folder in WSL, this can be set once like so, which creates a shortcut (i.e., soft-link using the -s switch) to Windows Documents folder from WSL home folder. Documents folder can then be accessed easily with cd command like so:

# Create a symlink to Windows Documents folder (only once)
ln -s /mnt/c/Users/<windows_username>/Documents ~/Documents
# Change directory to Windows Documents folder from WSL
cd ~/Documents

Next

New to command-line experience? Don't sweat. Take MIT's The Missing Semester of Your CS Education.

Footnotes

  1. Managed (corporate) software service, if appropriate.

  2. This will begin installing the default linux distribution Ubuntu. (Important Note: Do not click within the window or interrupt the process until complete.) From here-on, there is no further need for any Windows Admin intervention.

  3. This user account will be set as the linux administrator account, which can use sudo for elevating permissions when required (see documentation). Note that this does not conflict with Windows Administrator account(s), and it has no admin access on Windows side of things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment