How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)

Overview

I will summarize the settings made on Ubuntu of Windows Subsystem for Linux (WSL) as a memorandum.

Premise

Windows 10 version 2004 and above

Confirmation method

  1. Start "Run" with Win + R image.png
  2. Type winver and press ʻEnter`

winver.png

Enable WSL

1. Enable the "Windows Subsystem for Linux" option

Run PowerShell as an administrator and execute the following command

PowerShell[Administrator]


dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

2. Enable the "Virtual Machine Platform" option

Run PowerShell as an administrator and execute the following command

PowerShell[Administrator]


dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

3. Restart your PC

4. Download the Linux kernel update package

Please download and install from here.

5. Set WSL2 as default

PowerShell[Administrator]


wsl --set-default-version 2

6. Install Ubuntu from Microsoft Store

Please obtain and install from the following. https://www.microsoft.com/ja-jp/p/ubuntu/9nblggh4msv6?

7. Start Ubuntu

Press the Windows key to launch Ubuntu from the list of apps. First, set the user name. This time, it is user in the image, but it is OK if you decide as you like within the range of half-width alphanumeric characters. Then enter the password twice. It is not displayed even if I enter it, but it is entered properly. (Because I will use it many times after that, please do not forget it) image.png

Reference https://docs.microsoft.com/ja-jp/windows/wsl/install-win10

VcXsrv Windows X Server installation

Install the X-Window server to run GUI programs on Linux.

1. Download the installer

Download the installer from here.

2. Launch the installer

Launch the downloaded installer and install according to the instructions.

3. Launch XL launch

Search for XLaunch in the start menu and launch Click Next and write -ac in the -Additional parameters for VcX srv field on the third screen. image.png

If you save the configuration file in shell: startup with save configuration, VcXsrv will start automatically when Windows starts. image.png

WSL settings

I'm using Windows Terminal. It is quite easy to use, so if you want to use it, please refer to the installation / setting method described in [below](installation setting of windows-terminal from # microsoft-store is optional).

1. Start Ubuntu

2. Package installation

Execute the following commands in order. I think it will take a few hours lightly.

bash


sudo apt update -y
sudo apt upgrade -y
sudo apt install -y language-pack-ja manpages-ja manpages-ja-dev nkf build-essential x11-apps emacs emacs-mozc fcitx-mozc dbus-x11 gtypist typespeed texlive-full evince gnome-terminal sox nodejs libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-net-dev libsdl2-ttf-dev libsdl2-gfx-dev libopencv-dev freeglut3-dev fonts-freefont-ttf fonts-ubuntu gdb python3-pip libreadline-dev libssl-dev

I will explain what you are doing

1st line: Update the package list (Be sure to execute it when adding / deleting repositories) 2nd line: Update package 3rd line: Installation of basic packages and deb files

3. Various settings

Execute the following command to set the display.

bash


echo "export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0" >> ~/.profile
echo 'export LIBGL_ALWAYS_INDIRECT=1' >> ~/.profile

Execute the following command to set the location.

bash


sudo update-locale LANG=ja_JP.UTF-8

Next, as a setting for entering Japanese words, add the following contents to `~ / .profile``.

~/.profile


export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx
export DefaultIMModule=fcitx
if xset -r 49; then
fcitx-autostart >/dev/null 2>&1 &
else
echo "Start XLaunch and then Restart Ubuntu"
fi

Continue to set the time zone.

bash


sudo dpkg-reconfigure tzdata

And select ʻAsia Tokyo`.

4. Text editor settings

If you have VS Code installed and Remort --WSL installed, you can open VS Code with the code command, but you can't edit it with sudo (please tell me how to do it ...) Let's keep it.

VSCode Obtain and install from the following. https://code.visualstudio.com/download

Then install the extension from ctrl + shift + x or the □ mark on the far left. Remort - WSL Japanese Language Pack for Visual Studio Code Is mandatory. (* If you don't want to speak Japanese, just Remort --WSL is OK.)

After installing the extension, reload it and press ctrl + shift + p to bring up the command palette. Enter & select Configure Display Language and change it to ja to make it Japanese.

In addition, please install the extension function if you like.

Besides I Extensions that introduce Vim keybindings Color scheme Monokai Pro extension I have installed extensions such as, but I will omit it because my tastes are different.

You can open the setting from ctrl +, or the lower left gear → Settings. I think it is easy to understand because it is a GUI. (Editable with json)

Reference https://rfs.jp/sb/vsc/vsc-setting.html https://qiita.com/hi85/items/eaede5ebb509f21f27f5

If you use LaTeX, I wrote about it before, so Please refer to here) to set it.

Vim Write the settings in ~ / .vimrc. Also, the color scheme is not good at the beginning, so I have introduced the color scheme molokai.

Setting example

Download molokai

mkdir ~/.vim cd ~/.vim mkdir colors git clone https://github.com/tomasr/molokai mv molokai/colors/molokai.vim ~/.vim/colors/ vi ~/.vimrc


 > ~ / .vimrc contents

>```vim:~/.vimrc
set fenc=utf-8 "UTF-Save in 8 formats
set title "Show the name of the file being edited
set showmatch "Display the corresponding parenthesis when entering parentheses
set number "Show line number
syntax on "Turn on code color coding
set smartindent "Set smart indent
set tabstop=2 "Set indent to 2 spaces
syntax on "Code color coding
set showcmd "Display the command you are typing
set virtualedit=onemore "Allows you to move the cursor to the end of a line
set clipboard=unnamedplus "Put it in the clipboard when yank
set ignorecase "If the search string is lowercase, the search is case insensitive.
set smartcase "If the search string contains uppercase letters, search separately
set incsearch "Hit the target character string in sequence when entering the search character string
set wrapscan "If you go to the end when searching, go back to the beginning
set hlsearch "Highlight search terms
nmap <Esc><Esc> :nohlsearch<CR><Esc> "ESC repeated hits to remove highlights
colorscheme molokai "Make the color scheme molokai
set t_Co=256 "Make vim compatible with 256 colors

emacs Describe it in ~ / .emacs.d / init.el. Initially I don't think there is a directory ~ / .emacs.d

bash


mkdir ~/.emacs.d

Create it with.

Setting example

;;Japanese input settings (require 'mozc) (set-language-environment "Japanese") (setq default-input-method "japanese-mozc") ;;Turn off the welcome screen on the initial screen (setq initial-scratch-message t) (setq initial-scrarch-message nil) ;;Show how many rows and columns you are in at the bottom bar of the screen (line-number-mode t) (column-number-mode t) ;;Make yes and no hit with y and n (defalias 'yes-or-no-p 'y-or-n-p) ;;Line number display (global-linum-mode) (setq linum-format "%4d ") ;;UTF text encoding-Set to 8 (set-default-coding-systems 'utf-8) (set-keyboard-coding-system 'utf-8) (set-terminal-coding-system 'utf-8) (set-buffer-file-coding-system 'utf-8) (prefer-coding-system 'utf-8)


## 5. Font settings
 Makes Windows fonts available on WSL.
 First, open `/etc/fonts/local.conf` with sudo.
 vi can be emacs (code is not possible)


#### **`bash`**
```bash

sudo vi /etc/fonts/local.conf

Then paste the following content.

/etc/fonts/local.conf


<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
    <dir>/mnt/c/Windows/Fonts</dir>
</fontconfig>

After this,

bash


fc-cache -fv

Then you can use Windows fonts in WSL.

Installation of development tools

1.Python The default for Ubuntu 20.04 is 3.8.6, which is a little old, and it is convenient to be able to manage multiple versions.

1. Download pyenv

bash


git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

2. Download python with pyenv

bash


pyenv install --list

You can see the list that can be installed with.

The latest version at the time of posting is 3.9.0, so I will install it. If you want to install another version, please read as appropriate.

bash


pyenv install 3.9.0
pyenv global 3.9.0

3. pip package installation

bash


pip3 install pandas numpy scipy xlrd matplotlib japanize-matplotlib Pillow opencv-python folium plotly requests beautifulsoup4 lxml Flask Flask-Bootstrap4 Django PyMySQL

2.Ruby on Rails

1. Install rbenv

bash


git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
~/.rbenv/bin/rbenv init
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

2. Install ruby-build

bash


git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

3. Install Ruby

bash


rbenv install -list

If you do

bash


2.5.8
2.6.6
2.7.2
jruby-9.2.13.0
maglev-1.0.0
mruby-2.1.2
rbx-5.0
truffleruby-20.2.0
truffleruby+graalvm-20.2.0

It comes out like. This time I will install 2.7.2. If you want to install another version, please read as appropriate.

bash


rbenv install 2.7.2
rbenv rehash
rbenv global 2.7.2

4. Rails installation

bash


gem install rails

5. Install SQL / bundler / yarn

bash


sudo apt install libsqlite3-dev
gem install sqlite3
gem install bundler
curl -o- -L https://yarnpkg.com/install.sh | bash

6. Run Rails

bash


rails new app
cd app
rails server

Go to 127.0.0.1:3000 in your browser and image.png It's OK if something like this appears.

Alias setting

An alias is a command alias. Write it in the form of addition to ~ / .bashrc.

Below are my settings, but I think there is no loss in adding them because all but the bottom two are basic.

Setting example

#(Abbreviation) #Please add alias ..='cd ..' alias ..2='cd ../..' alias ..3='cd ../../..' alias agi='sudo apt install' alias agr='sudo apt remove' alias agu='sudo apt update && sudo apt upgrade' alias agi='sudo apt install' alias ags='apt search' alias cp='cp -i' alias mv='mv -i' alias rm='rm -ri' alias g='git' alias ga='git add' alias gd='git diff' alias gs='git status' alias gp='git push' alias gb='git branch' alias gst='git status' alias gco='git checkout' alias gf='git fetch' alias gc='git commit' #Note that these two are a little radical... alias m='mkdir' alias p='python3


 > Reference
https://qiita.com/reireias/items/d906ab086c3bc4c22147


# Symbolic link
 Symbolic links are like shortcuts in Windows.

 In WSL, the C drive of Windows is / mnt / c, so the Downloads folder is / mnt / c / User / (Windows user name) / Downloads.

 Let's put a symbolic link here.


#### **`bash`**
```bash

ln -s /mnt/c/User/(Windows username)/Downloads

image.png The symbolic link is pasted like this. Using symbolic links in this way makes it easier to link WSL with Windows on the host.

For the time being, I'm pasting this much. image.png

Install and configure Windows Terminal from Microsoft Store (optional)

Please obtain and install from the following. https://www.microsoft.com/ja-jp/p/windows-terminal/9n0dx20hk701

After installation, start it and select the setting from ∨ or open the setting with Ctrl +,.

Let's specify the ubuntu profile guid in profiles as the globals defaultProfile. Rearrange the order of the list to your liking. I have ʻUbuntu, Powershell, cmd` in that order.

Then set the startingDirectory in your Ubuntu profile to // wsl $ / Ubuntu / home / (username). The username here is ʻUbuntu username`.

For reference, in my case it is as follows.

settings.json


{
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "defaultProfile": "{2c4de342-38b7-51cf-b940-2309a097f518}",
    "profiles": {
        "defaults": {
        },
        "list": [
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": false,
                "name": "Ubuntu",
                "startingDirectory": "//wsl$/Ubuntu/home/user",
                "source": "Windows.Terminal.Wsl"
            },
            {
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "powershell.exe",
                "hidden": false
            },
            {
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "command prompt",
                "commandline": "cmd.exe",
                "hidden": false
            },
            //(Abbreviation)
        ]
    },
    //(The following is omitted)
}

I will omit the terminal color scheme and key binding settings. (Because the taste seems to be different)

Reference https://docs.microsoft.com/ja-jp/windows/terminal/get-started

Recommended Posts

How to set up Ubuntu for Windows Subsystem for Linux 2 (WSL2)
How to install Windows Subsystem For Linux
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
WSL2 (Windows Subsystem for Linux) installation procedure
When Windows Subsystem for Linux (WSL) cannot be started
How to install MBDyn (Linux Ubuntu)
[PowerShell] How to search for rows like Linux grep (Windows)
pykintone on Windows Subsystem for Linux
How to set up and use OMC Log Analytics --Linux version -
Until Windows Subsystem for Linux (WSL) is installed in Windows and fish is installed
How to set up the development environment of ev3dev [Windows version]
Windows Subsystem for Linux is not displayed
How to set up SVM using Optuna
How to install php7.4 on Linux (Ubuntu)
On Ubuntu Linux, set Tab to q
How to set up Random forest using Optuna
How to set up Random forest using Optuna
How to create a shortcut command for LINUX
How to set up a local development server
How to build Java environment on Ubuntu (Linux)
How to set CPU affinity for process threads
Useful for changing permissions on Linux! How to count up to 31 with one hand.
[Linux] How to read .bashrc of general user as root user on WSL2 Ubuntu20.04
Linux (WSL) on Windows
When I tried to use Python on WSL (windows subsystem for linux), it got stuck in Jupyter (solved)
How to set up a Python environment using pyenv
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
How to create a local repository for Linux OS
Introducing WSL (Ubuntu 18.04) to WIndows10 ~ Proxy environment second part ~
How to set up and compile your Cython environment
Set up Python 3.4 on Ubuntu
[Windows] RDP to Windows via Linux
How to install Python [Windows]
How to import Python library set up in EFS to Lambda
Set Up for Mac (Python)
[For beginners] How to use for statements on Linux (variables, etc.)
How to implement 100 data science knocks for data science beginners (for windows10 Home)
Set up AWS (Ubuntu 14.04) for Deep Learning (install CUDA, cuDNN)
How to implement Python EXE for Windows in Docker container
Set up Polyglot on Windows
Comfortable LaTeX with Windows Subsystem for Linux and VS Code
Use Linux on Windows 10 (WSL2)
How to set cron for regular Python scraping on Sakura server.
How to set the output resolution for each keyframe in Blender
How to get the printer driver for Oki Mac into Linux
Set up Ubuntu as a Linux cheat sheet and https server
[Unity] How to run ML-Agents Release 8 even for transcendental beginners [Windows]
[For non-programmers] How to walk Kaggle
How to use Dataiku on Windows
WSL2 ~ Linux on Windows ~ (Part 1: Introduction)
How to install wkhtmltopdf (Amazon Linux2)
How to install VMware-Tools on Linux
How to install pycrypto on Windows
How to deploy django-compressor on Windows
How to launch Explorer from WSL
How to speed up Python calculations
Bring files in Windows to WSL
How to check Linux OS version
[Windows] Transfer event logs to Linux
How to install Go on Ubuntu
How to install music 21 on windows