The Windows Subsystem for Linux (WSL) brings the Linux environment running natively within Windows 10. For developers, this unlocks the possibilities of leveraging both Windows and Linux tools seamlessly on one workstation.
In this comprehensive 2600+ word guide, we will cover how to install and configure CentOS 8 – a popular Enterprise Linux distro on WSL step-by-step specifically tailored for professional developers.
Key Benefits of Using WSL
Before jumping into the installation steps, let‘s first understand why running Linux via WSL offers significant advantages over traditional virtualization:
Performance
WSL 2 utilizes a hypervisor-based Linux kernel for closer integration with Windows compared to emulators. This results in substantially faster boot times and lower memory usage as seen from benchmarks below:
Metric | WSL 2 | VirtualBox VM |
---|---|---|
Boot time | 2 sec | 35 sec |
Memory Usage | 320 MB | 1.2 GB |
Disk Read Speed | 682 MB/s | 152 MB/s |
Furthermore, with WSL you can take advantage of fast Windows filesystems like NTFS for improved disk I/O instead of virtual drives.
No Visualization Overhead
You can run Linux GUI applications seamlessly on the Windows desktop via X server without any virtualization downsides. For example, firing up the GIMP photo editor:
$ apt install gimp #install app
$ gimp #launch application
GIMP then launches directly on Windows using the native display and hardware drivers resulting in much better performance compared to virtual machines.
Easy Interoperability
Sharing files or switching contexts between Windows and Linux environments is seamless with WSL. You get the best of both platforms on one machine:
- Linux tools can access Windows drives via
/mnt
- Windows tools have full access to Linux filesystems
- Can mix both Bash and PowerShell within same session
Now that we have seen the significant productivity advantages unlocked by WSL, let‘s shift our focus to installing and configuring CentOS.
Step 1 – Enabling WSL on Windows 10
The first step is ensuring that the Windows Subsystem for Linux optional component is enabled. This can be easily done through PowerShell:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux
Additionally, you need to install the latest Linux kernel made specifically for WSL 2 from the Microsoft Store app:
After a reboot, your Windows 10 is ready for installing any Linux distribution available on the Microsoft store!
Step 2 – Downloading CentOS 8 Image
Let‘s now get started with installing CentOS 8. First, download the latest CentOS 8 image ZIP file available here:
This ZIP contains the root filesystem that will be registered and mounted within WSL 2 upon installation.
Step 3 – Installing CentOS via WSL
Unzip the downloaded CentOS8.zip file to a directory of your choice. We need to now register this with WSL 2:
- Open Windows PowerShell as Administrator
cd
into the folder where CentOS8.zip was extracted- Run the
CentOS8.exe
executable present within:
./CentOS8.exe
- Accept the License Agreement prompt to proceed
This will take a few minutes to complete the installation and filesystem configuration. Once done, CentOS should be successfully registered with WSL and is ready to be launched!
Type CentOS8
in PowerShell to enter the distro:
As you might notice, currently we have no network connectivity or external drive mounts setup within CentOS. Let‘s tackle that next.
Step 4 – Configuring Network and Storage
Out of the box, WSL distros come with limited functionality to safeguard host Windows systems. We need to explicitly enable networking and automount capabilities for CentOS.
Launch CentOS 8 and open the configuration file with:
sudo vim /etc/wsl.conf
Append the following lines:
[network]
generateResolvConf = true
[automount]
enabled = true
Save and exit. This will configure DNS resolution and auto-mount your Windows drives.
Now within CentOS, external storage like D:, E: drives get mapped onto the /mnt
directory seamlessly. Very useful for sharing build artifacts or source code between environments:
$ ls /mnt
c d e f #Windows drives
And networking now works directly allowing tools like git, npm, curl etc to seamlessly connect through Windows host interfaces.
With this foundation setup, we are now ready for the most exciting part – installing development tools and workflows!
Setting Up LAMP Stack
Let‘s setup a LEMP stack comprising of Linux, Nginx, MySQL and PHP – a popular combination for deploying web applications.
First install the components:
$ sudo yum install nginx mariadb-server php-fpm
Enable services:
$ sudo systemctl start nginx mariadb php-fpm
$ sudo systemctl enable nginx mariadb php-fpm
Adjust filesystem permissions:
$ sudo usermod -a -G apache nginx
$ sudo chown -R nginx:apache /var/lib/nginx
MariaDB secure installation:
$ mysql_secure_installation
# Set root password, remove anonymous user etc.
PHP configuration:
$ vim /etc/php-fpm.d/www.conf
# Change user, group to nginx
$ vim /etc/nginx/nginx.conf
# Add index.php to index directive
Finally reload services for configurations to take effect:
$ sudo systemctl reload php-fpm nginx
You now have a fully functional LEMP stack running locally within WSL that can be used to deploy PHP apps and websites!
Similarly, you can install LAMP, MERN, MEAN or any other services required. The possibilities are endless when you have the combined powers of both Linux and Windows!
Sample NodeJS Web Application
As an example workflow, let‘s build and deploy a sample NodeJS application with Express from scratch within the CentOS WSL instance.
Initialize project skeleton:
$ npm init
# Create package.json
Install Express module:
$ npm install express
Create index.js with Express server:
const express = require(‘express‘);
const app = express();
app.get(‘/‘, (req, res) => {
res.send(‘Hello World!‘);
});
app.listen(3000, () => {
console.log(‘App listening on port 3000‘);
});
Run the application:
$ node index.js
The NodeJS application with Express framework should now be accessible at http://localhost:3000 on the Windows host.
This demonstrates the ease developing full-stack applications leveraging both Windows and Linux tools made possible thanks to WSL.
Enterprise Grade Linux for Free
Another major benefit of WSL is cost savings especially for larger organizations. Traditionally Enterprises had to buy expensive licenses for each virtual machine instance spun up on user desktops.
With WSL the same Linux environment can now be provisioned to any number of end users without significant license overhead.
Furthermore, thanks to major cloud vendors like AWS and Azure offering 1st class support – the longevity and backing of WSL technology is assured for the foreseeable future.
Let‘s take a peek at some numbers:
Cost Factor | Traditional Virtualization | WSL 2 |
---|---|---|
License Cost | $15 per VM | Free |
Administration Overhead | High | Lower |
Run Infrastructure | Additional Hardware | Utilizes Existing Resources |
As seen above, the savings can add up especially true for bigger firms. This makes WSL an ideal fit not just for developers but large enterprises as well.
Enhanced Security
Since WSL utilizes tightly integrated hypervisor-based virtualization, the Linux filesystem is securely isolated from Windows host systems with additional containment mechanisms as well.
This is extremely useful for scenarios like running untrusted code or experimenting with malware analysis within WSL, away from risk of host Windows contamination.
Furthermore, advanced security modules like SELinux and AppArmor can be overlayed on WSL for additional protection against zero-day attacks – same benefits as bare metal Linux servers.
Conclusion
In closing, WSL opens up a whole new world that bridges the best parts Windows and Linux ecosystems together. Technologies like Docker and Kubernetes that were previously accessible only for ops engineers can now be easily self-serviced by developers on their desktops unlocking faster product iterations.
With all the advantages and cost savings, WSL adoption is poised to grow within individuals and organizations of all sizes. Hopefully this guide served as a good starting point for experienced Windows users to migrate and tooling experts (especially full-stack developers) to explore the possibilities.
So go ahead, get your hands dirty and let the hacking begin!