LoginSignup
12
12

More than 5 years have passed since last update.

WSL(Ubuntu18.04)によるPython, Julia環境構築

Posted at

WSL(Windows Subsystem for Linux)

WSLが大分使える感じになってきた気がするので、移行できそうな環境を移行させたときのメモ。

コンソールの配色

WSLは配色の問題で視認性が悪いので、とにかく配色を改善する。
MS謹製のColorToolを使用する。
カラーテーマは以下の通り。

  • cmd-legacy
  • campbell
  • campbell-legacy
  • deuteranopia
  • OneHalfDark
  • OneHalfLight
  • solarized_dark
  • solarized_light

例えば、campbellにするなら以下の様にする。

> ColorTool.exe -d campbell

Neovim

最初からvimが導入されているはずなので、不要なら飛ばす。

nvimのドキュメントに従ってインストール。

$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:neovim-ppa/stable
$ sudo apt-get update
$ sudo apt-get install neovim

プラグインはdeinで適当に。

$ curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh > installer.sh
$ sh ./installer.sh ~/.cache/dein

installer.shの出力を参考に~/.config/neovim/init.vimを作成。

  • init.vim作成例
init.vim
" dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif
"
" Required:
set runtimepath+=/home/username/.cache/dein/repos/github.com/Shougo/dein.vim

" Required:
if dein#load_state('/home/username/.cache/dein')
  call dein#begin('/home/username/.cache/dein')

  " Let dein manage dein
  " Required:
  call dein#add('/home/username/.cache/dein/repos/github.com/Shougo/dein.vim')

  " Add or remove your plugins here like this:
  call dein#add('Shougo/neosnippet.vim')
  call dein#add('Shougo/neosnippet-snippets')
  call dein#add('crusoexia/vim-monokai')

  " Required:
  call dein#end()
  call dein#save_state()
endif

" Required:
filetype plugin indent on
syntax enable

" If you want to install not installed plugins on startup.
if dein#check_install()
  call dein#install()
endif

set background=dark
colorscheme monokai

"End dein Scripts-------------------------

Shell

fishxonsh等、好みのシェルを導入する。

$ sudo apt install fish

chshでデフォルトシェルを切り替えても良いが、環境変数などの関係上.bashrcの最終行に起動コマンドを記述した方が良い。

.bashrc
# 最終行に記述
exec /usr/bin/fish

Python

pyenv

pyenvのドキュメントに従ってインストール。
pyenv-virtualenvも同時にインストールする。
公式ドキュメントでは.bash_profileに記述しているが、ここでは.bashrcに記述。

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\n  eval "$(pyenv virtualenv-init -)"\nfi' >> ~/.bashrc

pyenv-virtualenv以外のプラグインも導入。

$ git clone git://github.com/pyenv/pyenv-update.git $(pyenv root)/plugins/pyenv-update
$ git clone https://github.com/pyenv/pyenv-pip-rehash $(pyenv root)/plugins/pyenv-pip-rehash
$ git clone https://github.com/massongit/pyenv-pip-update $(pyenv root)/plugins/pyenv-pip-update

Anaconda

以下のコマンドで使用可能な環境の一覧が表示される。

$ pyenv install --list

所望の環境をインストール。
CONFIGURE_OPTS="--enable-shared"オプションはJulia等からPythonを使用するために必要となる。
以下はpyenvの例。virtualenvを使用する場合はvirtualenvのコマンドにする。

$ CONFIGURE_OPTS="--enable-shared" pyenv install anaconda3-x.x.x

以下のコマンドで使用可能な仮想環境の一覧が表示される。

$ pyenv versions

所望の環境をアクティベートする。

$ pyenv global anaconda3-x.x.x
$ pyenv rehash  # pyenv-rehashを導入していれば不要

プロンプトの仮想環境名を非表示にしたい場合は以下の環境変数を追加すればよい。

$ echo 'export PYENV_VIRTUALENV_DISABLE_PROMPT=1' >> ~/.bashrc

X Server

このままだとmatplotlib等が表示されないため、X Serverを導入する。
VcXsrv Windows X Serverをインストールする。設定はデフォルトで良い。
.bashrcに環境変数を追加すればGUIが表示されるようになる。

$ echo 'export DISPLAY=localhost:0.0' >> ~/.bashrc

Windows起動時にVcXsrvを自動起動させたい場合は、スタートメニューのVcXsrv->XLaunchから設定を進めsave configuration*.xlaunchを保存し、それをスタートアップに置く。
スタートアップフォルダの場所は「ファイル名を指定して実行」(ショートカット: Win+R)にshell:startupを入力する。

Julia

Julia言語を使わないなら不要。

JuliaのダウンロードページからLinuxバイナリをダウンロードして展開。

$ curl https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.x.x-linux-x86_64.tar.gz
$ tar zxvf julia-1.x.x-linux-x86_64.tar.gz

で展開されたディレクトリを任意の場所(例えば/opt/julia)に移動してパスを通す。

$ echo 'export PATH="$PATH:/opt/julia/bin"' >> ~/.bashrc

Jupyter

Anacondaを導入していればPythonのkernelは入っているはずなので、Juliaのkernelを導入する。

$ julia
# Julia起動
julia> ]
# パッケージモードに移行
(v1.x) pkg> add IJulia
(v1.x) pkg> 
# BackSpaceで通常モードに移行
julia> using IJulia

以下のコマンドでJupyterのカーネルにJuliaが追加されていることが確認できる。

$ jupyter kernelspec list

VSCode

他に好みのエディタがあるならばそれを使用する。

Git

wslgitを導入することでVSCodeから利用することができる。
※2018/11現在、GitLensに対応していないため、WSLではなくGit for Windowsの利用を推奨

  • VSCodeの設定例。(C:\bin\以下にwslgit.exeを置いたとする)
setting.json
{
  "git.path": "C:/bin/wslgit.exe",
}
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12