iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📌

[WSL] [Bioinformatics] Setting up a WSL environment on Windows 11 (1)

に公開
2024/05/14

April 2023
I had an opportunity to set up a new WSL environment, so I am recording it as a memo.

The computer I used is a mini PC purchased in December 2022 for 115,980 JPY. I used one with 64GB of RAM.
Minisforum EliteMini B550 AMD Ryzen™ 7 5700G / 64GB RAM + 512GB SSD
https://store.minisforum.jp/products/minisforum-elitemini-b550


Specs of the PC used

The C drive came with a 512 GB M.2 SSD, but I replaced it with a 2 TB Kioxia EXCERIA G2 NVMe™ SSD.

[ WSL Environment Setup ]

1. Obtaining Ubuntu

Download Ubuntu from the Microsoft Store.
If you want a specific version of Ubuntu, you should download the one labeled LTS. (Occasionally, certain features are available or unavailable depending on the version.)

2. Enabling Windows Subsystem for Linux

If you see the following message when you launch Ubuntu, the Windows-side settings are not enabled.

The Windows Subsystem for Linux optional component is not enabled. Please enable it and try again.

  1. Open Turn Windows features on or off from the taskbar search or similar.
  2. Check Windows Subsystem for Linux and Hyper-V, then click OK.
  3. You will be prompted to restart your PC, so please restart it.

3. Updating the Linux Kernel

Relaunch Ubuntu.

If the following message appears, an additional Linux kernel update program is required.

Error: 0x800701bc WSL 2 ???????????? ??????????????????????? https://aka.ms/wsl2kernel ?????????

Navigate to the link in the error message and download the WSL2 Linux kernel update package for x64 machines from the link, then run it.
There aren't really any choices to make, so just follow the on-screen instructions.

4. Setting Username and Password

When you relaunch Ubuntu, you will be prompted to set a username and password.
Nothing is displayed on the screen when typing the password, but it is actually being reflected.

It is successful if the prompt (the username@computername:~$ part) is displayed and it is in a state ready to accept commands.

5. update/upgrade

First, perform an update and upgrade.
sudo explicitly indicates that the command should be executed with administrative privileges. An error will occur without it, so make sure to include it. You may see messages prompting you to use sudo for things other than update/upgrade as well. You can simply execute the command after adding sudo at the beginning.

sudo apt update
sudo apt upgrade

You will be asked yes or no, so press Y.

In the future, if you encounter an error when installing something, it is good to perform these two steps first to bring everything up to the latest state and then try again.


[ Simple Command Examples ]

Checking WSL Home Directory

pwd command allows you to check the home directory of the Linux environment.
/home/username is set as the home directory.

Checking the parent working directory
pwd

/home/rchiji


File List

You can check the list of files in the current directory with the ls command.

ls -la


-a is an option to show hidden files, and -l displays them in a list format. Writing ls -al is the same as writing ls -a -l.

You should see files like .profile and .bashrc; these are important files for operating WSL, so it is a good idea to look them up.


Changing Directories

Use the cd command. A directory is what we call a folder in Windows. Moving between directories is like opening folders in Windows Explorer.

# Create test folder
mkdir test
# Move to test folder
cd test
# Check the file list in the test folder
ls -la

As the blue part of the prompt changes when you move directories, the part before the $ indicates the current location (current directory).
It shows ~ from the start, and ~ means the home directory (in my case, /home/rchiji).
. means the current directory, and .. means the directory one level above the current one, so it is useful to use these as shortcuts.
You should Google and learn about absolute paths/relative paths regarding this.


[ Windows Folder ⇆ Linux Folder ]

Accessing Windows Folders/Files from Ubuntu

WSL also uses the Windows system drives, but you cannot access them as easily as your home directory.
For example, to access the C drive, you need /mnt/c/, and for the D drive, /mnt/d/, following the pattern /mnt/drive_letter at the beginning.

# Move to the Pictures folder on the C drive
cd /mnt/c/Users/rchiji/Pictures/
# Check the contents of the Pictures folder
ls -la

You can return to your home directory by using just cd ~ or cd.


Accessing Linux Folders from Windows Explorer

You can access folders and files in the Linux environment from Explorer. If you can't find the "Linux" section on the left side of Explorer, you can reach it by typing "Linux" directly into the address bar at the top of Explorer.

Let's take a look at the actual contents of the home directory. You can confirm that files like .profile and .bashrc are there.

If nothing is displayed, it is a good idea to change your Explorer settings to show hidden files.


File Paths

To access WSL files from R or Python on the Windows side, you will need the file path.

For example, suppose there is a file named "text.txt" in the WSL home directory. If you copy the path in Explorer, you will get a path like this:
"\wsl.localhost\Ubuntu\home\rchiji\test.txt"

If you are careful with the handling of backslashes, you can read WSL files from the Windows versions of R and Python.

R

read.csv("\\\\\\\\wsl.localhost\\\\Ubuntu\\\\home\\\\rchiji\\\\test.txt")
read.csv("//wsl.localhost//Ubuntu//home//rchiji//test.txt")

Python

pd.read_csv(r"\\\\wsl.localhost\\Ubuntu\\home\\rchiji\\test.txt")
pd.read_csv("\\\\\\\\wsl.localhost\\\\Ubuntu\\\\home\\\\rchiji\\\\test.txt")
pd.read_csv("//wsl.localhost/Ubuntu/home/rchiji/test.txt")


[ .profile / .bashrc ]

Let's open the .profile file in the home directory. Since it is a simple text file, you can open it with Notepad. Note: The following image shows it being displayed in a free software called Notepad++.

.profile contents

.profile is a file executed when Ubuntu starts. In the image above, the following execution contents are written:

  1. Execute .bashrc in the home directory
  2. Add the bin folder in the home directory to the path
  3. Add the .local/bin folder in the home directory to the path

In practice, you customize .profile by adding content you want executed at startup.
Examples include initial settings for Ubuntu, aliases, and paths you want to set.

Let's try changing the prompt color and the displayed information.
Add the following to .profile. *Note: Line ending characters differ between Windows and Linux. Be careful, as line breaks made in Windows Notepad can become characters that are unreadable in Linux.

PS1="\[\e[36m\][\u@:\w \D{%F %T}]\[\e[0m\]\n\\$ "

Relaunch Ubuntu.

The style of the prompt has changed. (I won't explain the meaning of the code above. You can find it by searching for something like "bash prompt color change." Feel free to customize it in your own style.)

.bashrc has even more variety of processes written in it.

.bashrc contents

Let's remove the addition to .profile and try writing the following to .bashrc instead.

PS1="\[\e[36m\][bioinfo@:\w]\[\e[0m\]\n\\$ "

When you relaunch Ubuntu, the prompt style change is applied.

In this way, you can write initial settings in either .profile or .bashrc.

I will proceed with this prompt style from now on.

Note that if a .bash_profile file already exists, it will be prioritized over .profile.
Generally, creating a .bash_profile is more frequently recommended than editing .profile.
Also, since the .bashrc file is executed within .profile, you can also write startup processes inside .bashrc. The choice between them is a matter of personal preference.


[ GUI Apps ]

WSL2 has made it easy to use Linux GUI apps. Let's try using GUI apps like xeyes and gedit.
First, install them:

sudo apt install x11-apps
sudo apt install gedit

Try running the xeyes command, but an error occurs saying that the display cannot be opened.

Updating WSL with Windows PowerShell

wsl --update


(It might be the same if you install Windows Subsystem for Linux from the Microsoft Store.)

Relaunch Ubuntu and try the xeyes command. If a window with moving eyes opens, it's a success.

Linux apps that are like Notepad can now be used as well.





Continued ↓↓
https://zenn.dev/rchiji/articles/83b8b4e53393ab

Discussion