How to Set Up Ubuntu on Windows with WSL for Development
Many developers find themselves needing a robust Linux environment for tasks like AI, machine learning, web development, or system administration, even when their primary workstation runs Windows. Dual-booting or setting up traditional virtual machines can be cumbersome and resource-intensive. The Windows Subsystem for Linux (WSL) offers an elegant, lightweight solution, allowing you to run a full Linux distribution, such as Ubuntu, directly within Windows. This tutorial will guide you through the process of setting up Ubuntu on WSL, enabling you to leverage the power and flexibility of Linux without leaving your familiar Windows desktop.
Step 1: Enable Windows Subsystem for Linux and Virtual Machine Platform
Before you can install a Linux distribution, you need to enable the core WSL features on your Windows machine. This involves activating two optional Windows features: the Windows Subsystem for Linux and the Virtual Machine Platform. The latter is crucial as WSL2, the latest and recommended version, utilizes virtualization technology to provide a full Linux kernel experience.
To enable these features, open PowerShell as an administrator. You can do this by searching for "PowerShell" in the Windows Start menu, right-clicking on "Windows PowerShell," and selecting "Run as administrator."
Once PowerShell is open, execute the following command. This command is a streamlined way to enable both necessary features and install the default Ubuntu distribution on newer versions of Windows:
wsl --installIf you are using an older version of Windows or prefer to enable features manually, you can use these commands:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestartdism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestartAfter running these commands, you will likely be prompted to restart your computer. A restart is essential for the changes to take effect. If you used the wsl --install command, it might automatically restart or prompt you to do so.
Verification: After your computer restarts, you can verify that the features are enabled by opening PowerShell again (no need for administrator privileges this time) and running:
wsl --statusThis command should output information about your WSL installation, confirming that WSL is running and indicating the default version (preferably WSL 2). If it shows an error, ensure your Windows version is up to date and try the commands again.
Step 2: Install Your Linux Distribution (Ubuntu)
With WSL enabled, the next step is to install a specific Linux distribution. Ubuntu is a popular choice due to its extensive community support and rich package repositories. The easiest way to install Ubuntu is through the Microsoft Store.
Open the Microsoft Store application on your Windows machine. In the search bar, type "Ubuntu." You will see several versions of Ubuntu available, such as "Ubuntu," "Ubuntu 20.04 LTS," or "Ubuntu 22.04 LTS." LTS stands for Long Term Support, meaning these versions receive security updates and bug fixes for several years, making them ideal for stable development environments.
Select the Ubuntu version you prefer (e.g., "Ubuntu 22.04 LTS") and click the "Get" or "Install" button. The Microsoft Store will download and install the distribution onto your system. This process might take a few minutes depending on your internet connection.
The Microsoft Store provides a convenient way to manage your WSL distributions. It handles the download, installation, and updates, integrating seamlessly with your Windows environment. Each distribution installed this way runs as an isolated instance, allowing you to have multiple Linux environments if needed.
Verification: Once the installation is complete, you should see an "Open" button in the Microsoft Store. Additionally, a new "Ubuntu" entry will appear in your Windows Start menu, ready to be launched.
Step 3: Initial Setup and System Updates
After installing Ubuntu, you need to perform some initial setup steps, including creating a user account and updating the package list to ensure you have the latest software.
Launch Ubuntu for the first time. You can do this by clicking the "Open" button in the Microsoft Store, or by finding "Ubuntu" in your Windows Start menu and clicking its icon. A console window will open, and Ubuntu will complete its initial setup process. This might take a moment.
You will then be prompted to create a new UNIX username and password. This account will be your default user within the Ubuntu environment and will have administrative (sudo) privileges. Choose a username (it doesn't have to match your Windows username) and a strong password. Remember this password, as you'll need it for administrative tasks within Ubuntu.
Once your user account is set up, the first critical step is to update the package lists and upgrade any installed packages to their latest versions. This ensures your system is secure and has access to the newest software. Execute the following commands in your Ubuntu terminal:
sudo apt updatesudo apt upgradeThe sudo apt update command fetches the latest package information from the Ubuntu repositories, letting your system know about new versions of software. The sudo apt upgrade command then installs those newer versions for all installed packages. You will be prompted to confirm the upgrade by typing Y and pressing Enter. This process can take some time, depending on the number of updates available.
Verification: A successful update will show messages indicating that packages are being downloaded and installed. After the upgrade completes, you should see a message similar to "Done" or "All packages are up to date." If there are any errors, check your internet connection or refer to Ubuntu's official documentation for troubleshooting.
Step 4: Working with the Ubuntu Terminal and File System
Now that Ubuntu is set up, you can begin interacting with it through the terminal. This is where you'll execute Linux commands, manage files, and run your development tools.
Open your Ubuntu terminal (from the Start menu or by typing ubuntu in the Windows Run dialog, accessible via Win + R). You'll be greeted by your command prompt, typically showing your username and hostname, followed by a dollar sign ($).
Here are some fundamental Linux commands to get you started:
ls: Lists the contents of the current directory.pwd: Prints the working directory (shows your current location in the file system).cd [directory_name]: Changes the current directory. For example,cd Documentsmoves into the Documents folder.mkdir [new_directory]: Creates a new directory.rm [file_name]: Removes a file. Userm -r [directory_name]to remove a directory and its contents (use with caution!).man [command]: Displays the manual page for a command (e.g.,man ls). Pressqto exit the manual page.
One of the powerful features of WSL is its seamless integration with the Windows file system. You can access your Windows drives directly from within Ubuntu. Windows drives are mounted under the /mnt directory. For example:
/mnt/c: Represents your Windows C: drive./mnt/d: Represents your Windows D: drive (if you have one).
To navigate to your Windows user profile from Ubuntu, you might use a command like:
cd /mnt/c/Users/YourWindowsUsername/DocumentsReplace YourWindowsUsername with your actual Windows username. This allows you to work on files stored on your Windows drive using Linux tools.
Conversely, you can access your Linux files from Windows. Open File Explorer in Windows and type \wsl$\[DistributionName] in the address bar (e.g., \wsl$
Ubuntu). This will show you the Linux file system, where you can browse, copy, and paste files just like any other network drive. You can also open your current WSL directory in Windows File Explorer by typing explorer.exe . (note the dot) in your Ubuntu terminal.
Step 5: Enhancing Your WSL Development Environment
With Ubuntu running smoothly on WSL, you can now customize it for your specific development needs. This often involves installing programming languages, frameworks, and other tools.
Installing Development Tools
The apt package manager is your primary tool for installing software in Ubuntu. Here are examples of how to install common development tools:
- Git: Essential for version control.
sudo apt install git - Node.js and npm: For JavaScript development. It's often recommended to use a version manager like NVM (Node Version Manager) for more flexibility, but you can install directly:
sudo apt install nodejs npm - Python and pip: Python is often pre-installed, but you might want specific versions or
pip(Python package installer):sudo apt install python3-pip - Docker: For containerization. Follow the official Docker documentation for WSL-specific installation steps, which often involve adding Docker's repository.
Always remember to run sudo apt update before installing new packages to ensure you're getting the latest available versions.
Configuring Your Shell and Editor
Many developers customize their terminal experience. You might consider installing a different shell (like Zsh with Oh My Zsh) or a command-line text editor (like Vim or Nano) if you prefer working entirely within the terminal. For GUI-based text editors or IDEs, you can install them on your Windows side and configure them to work with your WSL environment, leveraging tools like Visual Studio Code's Remote - WSL extension.
Running GUI Applications with WSLg
WSL 2, combined with Windows 10 version 21H2 or Windows 11, supports running Linux GUI applications directly on your Windows desktop. This feature, known as WSLg, allows you to install and launch graphical Linux applications (like GIMP, VS Code for Linux, or even full desktop environments) and have them appear seamlessly alongside your Windows applications. There's no complex setup required; if you have a compatible Windows version and WSL 2, it usually works out of the box after installing a GUI application within Ubuntu using apt.
By following these steps, you've successfully integrated a powerful Ubuntu environment directly into your Windows workflow, opening up new possibilities for development, data science, and more. For developers looking to build and host their projects, Yammbo Web offers an AI-powered website builder at https://web.yammbo.com.