Building a Python environment with WLS2 + Anaconda + PyCharm

In Previous article, I built a Python environment using WLS + VS Code. However, as I proceeded with Udemy's lecture, I wanted to create a better environment with ** WLS2 + Anaconda + PyCharm **, so I will note it.

First, I will describe the Udemy lectures that I referred to and their environments.

OS Ubuntu 20.04 LTS
Virtual environment Pythonvenvcommand
editor Visual Studio Code
OS Windows10
Virtual environment Anacondacreatecommand
editor PyCharm

The former environment has the advantage of being able to use the Linux OS using WSL, and the latter has convenient functions such as debugging with PyCharm and version control with Git, while easily building a virtual environment with Anaconda. It is a merit to be there.

So, ** I want to do these two good things! I came up with the idea of ​​**. By the way, WSL seems to be able to use a new version called WSL2, so I will update it as well. As a result, the environment ** WLS2 + Anaconda + PyCharm ** was created.

Prior to the update, I uninstalled the originally installed Ubuntu 20.04 LTS and PyCharm.

Update from WSL to WSL2

The Windows Subsystem for Linux (WSL) is designed to enable Linux to be used on Windows. I started using the lecture, but later learned that there was a new version called WSL2. That's because downloading a Linux distribution (such as Ubuntu) to Windows doesn't automatically enable WSL2, and you'll need to do some configuration before installation.

There is no problem with WSL as it is, but there is a merit to change to WSL2. One of them is to use a mechanism called ** Virtual Machine (VM) **. This makes it more compatible with Linux than ever before, and you can use the popular Docker [^ 1].

[^ 1]: "WSL 2" is officially released! -What are the advantages of "WSL 1"? Also pay attention to "Windows Terminal"

1. Check if Windows 10 is up to date

Now, when the motivation for updating is established, we move on to the WSL2 settings. Basically, it just follows Microsoft's documentation [^ 2] and there is nothing particularly difficult.

To use WSL2, you need to have Windows version 1903 or higher and build 18362 or higher. You can check the current version and build by opening Run with Windows key + r, typing winver and pressing Enter. If the requirements are not met, update with Windows Update Assistant.

This time it met the requirements, so proceed to the next.

2. Enable Windows features

To use WSL2, enable the features ** Windows Subsystem for Linux ** and ** Virtual Machine **. If you are using WSL originally, Windows Subsystem for Linux is already enabled, so you can omit it, but I will describe the method for the time being. You can also enable it from the control panel, but this time we will use PowerShell.

Start PowerShell as an administrator, execute each of the following commands, and restart the PC to complete the activation.

PowerShell


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

3. Kernel component update

You can get the latest kernel components by accessing the links below. Follow the instructions to update.

https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

4. Set WSL2 to the default version

Open PowerShell again and execute the following command. Now the next time you install a Linux distribution, you'll be using WSL2.

PowerShell


wsl --set-default-version 2

5. Install the Linux distribution

Now that you're ready, install your Linux distribution from the Microsoft Store.

[Previous article] I installed Ubuntu 20.04 LTS as well as (https://qiita.com/sota_morita/items/48cb64fe220aa3e1e2a0), set username and password, and translated it into Japanese.

6. Version check

Finally, check if the WSL2 settings are set. If you open a command prompt and run wsl --list --verbose, you'll see that it's definitely version 2.

>wsl --list --verbose
  NAME            STATE           VERSION
* Ubuntu-20.04    Running         2

Install Anaconda on Ubuntu

Originally, the Windows version of Anaconda was installed, but this time I will install Anaconda on Ubuntu. In other words, you will be installing the Linux version of Anaconda. This makes it easier to build a virtual environment that is confused by commands that you are not familiar with in the previous article (https://qiita.com/sota_morita/items/48cb64fe220aa3e1e2a0).

Since it is a Linux version installation, searching and downloading normally from Chrome does not work. Therefore, take the method of downloading the installer directly from the Ubuntu terminal [^ 3].

[^ 3]: Procedure to install Anaconda on Ubuntu and run Python and Jupyter Notebook

Find * Indivisual Edition * from the Anaconda home page and copy the address of the Linux * 64-Bit (x86) Installer * link. Write that address after the wget command.

$ wget https://repo.anaconda.com/archive/Anaconda3-2020.11-Linux-x86_64.sh

After the download is complete, use the bash command to execute the address" Anaconda3 ~ "or less.

$ bash Anaconda3-2020.11-Linux-x86_64.sh

Then, the terms of use and confirmation of the storage location will appear one after another, so if you proceed quickly with yes or Enter, the installation will be completed.

After the installation is complete, reflect the changes to .bashrc (you can also restart Ubuntu).

$ source ~/.bashrc

If you check the version in this state, you can confirm the installation safely.

$ conda -V
conda 4.9.2

After reloading .bashrc, the word(base)is displayed at the beginning of the command line, which is annoying. It remains displayed even after restarting Ubuntu. When I looked it up, it seems that auto_activate_base is True [^ 4]. Execute the following command and reload it with $ source ~/.bashrc, or restart Ubuntu to solve the problem.

$ conda config --set auto_activate_base False

After that, you can still build a virtual environment from the Ubuntu terminal with conda create -n virtual environment name python = version name. See below for building a virtual environment with Anconda.

[For beginners] Create a virtual environment with Anaconda

Use WSL with PyCherm

The installation of Anaconda for Linux is complete, and I'm almost done with what I want to achieve. In Previous article, I set WSL in Visual Studio Code, so I can use this, but I want to use more sophisticated PyCharm anyway.

According to PyCharm's guidance [^ 5], the use of remote development environments such as WSL seems to be limited to the Professional version. I was disappointed for a while, but it seems that JetBrains services such as PyCharm can be used ** free ** for those who belong to educational institutions including students [^ 6]. Fortunately, I'm a student, so I'll take advantage of it right away.

[^ 5]: Configure an interpreter using WSL

[^ 6]: How to apply for JetBrains student discount

From the JetBrains page (https://www.jetbrains.com/ja-jp/community/education/#students), select ** "Apply Now **" and register for an account. Register the university email address ending with .ac.jp in this email address. Then, an email will be sent to the registered address, so click the link provided to complete the authentication.

PyCharm free licence

Download the Windows version of the PyCherm installer (not Linux) and start the installation. At the beginning, you will be asked for your email address and registered password, so log in with the account you registered for the free license. You can now start using the Professional version.

After that, set according to PyCherm's guide [^ 5].

Open PyCharm and select ** File ** → ** Setting ** → ** Project ** → ** Python Interpreter **.

PyCharm Python Interpreter

The Python Interpreter is <No interpreter>. Select ** Add ... ** from the settings mark on the right side → Select ** WSL ** from the left pane → ** Ubuntu 20.04 ** for Linux Distribution → \\ wsl for Python interpreter path Select the environment that suits your purpose, such as $ \ Ubuntu-20.04 \ home \ user name \ anaconda3 \ envs \ virtual environment name.

Until now, I used both Windows and Ubuntu, so I had to use different environments depending on the lecture, but now I can easily switch between them with PyCherm alone. I would like to update it if there is an environment that looks good in the future.

PyCharm Python interpreter path

I didn't apply for Udemy's lecture because I wanted to learn how to build an environment, but it was very helpful because I happened to be using a different environment. I'll link it again just in case.

Recommended Posts

Building a Python environment with WLS2 + Anaconda + PyCharm
Building a virtual environment with Python 3
[Pyenv] Building a python environment with ubuntu 16.04
[Python] Building an environment with Anaconda [Mac]
[Python] Create a virtual environment with Anaconda
Building a Python 3.6 environment with Windows + PowerShell
Build a Python environment on your Mac with Anaconda and PyCharm
Building a python environment with virtualenv and direnv
Building an Anaconda environment for Python with pyenv
Python + Anaconda + Pycharm environment construction
Install Python environment with Anaconda
Building a Python virtual environment
Building a Python virtual environment
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Recommendation of building a portable Python environment with conda
conda memorandum: Building a Python environment with supercomputer ITO
Building a kubernetes environment with ansible 2
Building a Python environment on Ubuntu
Create a virtual environment with Python!
Building a kubernetes environment with ansible 1
Building a pyhon environment without using Anaconda (with easy startup)
Change Python 64bit environment to 32bit environment with Anaconda
[Mac] Building a virtual environment for Python
Build Python environment with Anaconda on Mac
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Building a Python development environment for AI development
Building a Windows 7 environment for getting started with machine learning with Python
Build a python environment with ansible on centos6
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
Use Python 3.8 with Anaconda
[Memo] Build a virtual environment with Pyenv + anaconda
[Python] Web development preparation (building a virtual environment)
A memo when creating a python environment with miniconda
Think about building a Python 3 environment in a Mac environment
Python environment with docker-compose
Commands for creating a python3 environment with virtualenv
Work in a virtual environment with Python virtualenv.
Flow of creating a virtual environment with Anaconda
Use Python in Anaconda environment with VS Code
Build a Python environment with OSX El capitan
Create a Python environment
Quickly build a Python Django environment with IntelliJ
Building a Python environment on a Sakura VPS server
From Python environment construction to virtual environment construction with anaconda
Virtual environment with Python 3.6
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
Get a quick Python development environment with Poetry
Write about building a Python environment for writing Qiita Qiita
Building a Docker working environment for R and Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Create a virtual environment with Anaconda installed via Pyenv
Build a python environment for each directory with pyenv-virtualenv
Create a comfortable Python 3 (Anaconda) development environment on windows
Create a python development environment with vagrant + ansible + fabric
I made a Python3 environment on Ubuntu with direnv.
Building an environment for natural language processing with Python
Build a machine learning application development environment with Python