ためすう
Anaconda をインストールする
2018-08-05目的
Anaconda をインストールします。
Anaconda は、データサイエンスに特化したプラットフォームを提供します。
導入
インストーラーをダウンロード
下記からインストーラーをダウンロードして、インストールしていきます。
インストール後、少し使ってみる
現在使用している環境は下記で見ることができます。
$ conda info -e
# conda environments:
#
base * /anaconda3
xgboost、plotly をインストールする
Kaggle というサイトで、チュートリアルコンペティションに参加してみました。
ここで、チュートリアルを進めて行くと、xgboost
plotly
をインストールする必要が出てきました。
xgboost をインストールします。
$ conda install -c conda-forge xgboost
plotly をインストールします。
$ conda install -c plotly plotly
参考
vagrant で共有フォルダのマウントに失敗するので、解決を試みる
2018-08-05目的
Vagrant で仮想マシンは起動できるのですが、共有フォルダのマウントに失敗しています。
発生しているエラー
default: /vagrant => [動かしている環境]
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:
mount -t vboxsf -o uid=500,gid=500 vagrant /vagrant
The error output from the command was:
/sbin/mount.vboxsf: mounting failed with the error: No such device
対応手順
$ vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Installed the plugin 'vagrant-vbguest (0.15.1)'!
$ vagrant vbguest --status
[default] GuestAdditions versions on your host (5.2.6) and guest (4.3.8) do not match.
guest が古いので、vbguest プラグインで更新します。
$ vagrant vbguest
[default] GuestAdditions versions on your host (5.2.6) and guest (4.3.8) do not match.
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: ftp.jaist.ac.jp
* extras: ftp.jaist.ac.jp
* remi-safe: mirrors.thzhost.com
* updates: ftp.jaist.ac.jp
No package kernel-devel-2.6.32-573.7.1.el6.x86_64 available.
Package gcc-4.4.7-18.el6.x86_64 already installed and latest version
Package binutils-2.20.51.0.2-5.47.el6_9.1.x86_64 already installed and latest version
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package 4:perl-5.10.1-144.el6.x86_64 already installed and latest version
Package bzip2-1.0.5-7.el6_0.x86_64 already installed and latest version
Nothing to do
Copy iso file /Applications/VirtualBox.app/Contents/MacOS/VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Mounting Virtualbox Guest Additions ISO to: /mnt
Installing Virtualbox Guest Additions 5.2.6 - guest version is 4.3.8
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.2.6 Guest Additions for Linux........
VirtualBox Guest Additions installer
Removing installed version 5.2.6 of VirtualBox Guest Additions...
You may need to restart your guest system to finish removing the guest drivers.
Copying additional installer modules ...
Installing additional modules ...
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
kernel-devel kernel-devel-2.6.32-573.7.1.el6.x86_64
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
kernel-devel kernel-devel-2.6.32-573.7.1.el6.x86_64
An error occurred during installation of VirtualBox Guest Additions 5.2.6. Some functionality may not work as intended.
In most cases it is OK that the "Window System drivers" installation failed.
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules.
This system is currently not set up to build kernel modules.
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
kernel-devel kernel-devel-2.6.32-573.7.1.el6.x86_64
groupadd: group 'vboxadd' already exists
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.
ゲスト側の /var/log/vboxadd-setup.log
は下記が残っていました。
Building the main Guest Additions module.
/tmp/vbox.0/Makefile.include.header:141: *** Error: unable to find the headers of the Linux kernel to build against. Specify KERN_VER=<version> (currently 2.6.32-573.7.1.el6.x86_64) and run Make again. Stop.
Creating user for the Guest Additions.
Creating udev rule for the Guest Additions kernel module.
modprobe vboxguest failed
ここでエラーを追うのはストップしようと思い Vagrant を再起動すると、共有フォルダがマウントされるようになりました。
$ vagrant halt
$ vagrant up
※注意: 根本解決はしていません
参考URL
less で zip、gzip を開く
2018-08-04目的
例えば、ログがローテートされて gzip 形式で保存されているファイルの確認をしたい時
ファイルを展開せずに確認する方法です。
実験
gzip のファイルを見る
$ vim test.txt
下記を入力します
test
test1
test222
test.txt.gz に圧縮します
$ gzip test.txt
方法1. less で見る
$ less test.txt.gz
方法2. gzcatで見る
$ gzcat test.txt.gz
zip のファイルを見る
$ vim ziptest.txt
下記を入力します
ziptest
ziptest1
ziptest222
zip ファイルを作ります
$ zip ziptest.txt.zip ziptest.txt
adding: ziptest.txt (deflated 32%)
方法1. less で見る
zip ファイルの中身を確認することができました。
$ less ziptest.txt.zip
Archive: ziptest.txt.zip
Zip file size: 191 bytes, number of entries: 1
-rw-rw-r-- 3.0 unx 28 tx defN 18-Mar-03 15:35 ziptest.txt
1 file, 28 bytes uncompressed, 19 bytes compressed: 32.1%
php でファイルパスを検索する
2018-08-04目的
アップロードされたファイルが見えないファイルかどうかを判別する時に
php の標準の関数を利用して、ファイルのパスで行うようにしました。
その時に使用する basename と dirname の挙動について調べてみます。
ちなみに
Windows では、スラッシュ(/) とバックスラッシュ () の両方がディレクトリ区切り文字として使われます。 その他の環境ではスラッシュ(/)になります。
やってみる
$ vim test.php
<?php
$files = [
'p_dir1/p_dir2',
'p_dir1/p_dir2/test.jpg',
'test2.jpg',
];
foreach ($files as $v) {
echo $v . "\n";
echo "dirname: " . dirname($v) . "\n";
echo "basename: " . basename($v) . "\n";
echo "\n";
}
実行結果
$ php test.php
p_dir1/p_dir2
dirname: p_dir1
basename: p_dir2
p_dir1/p_dir2/test.jpg
dirname: p_dir1/p_dir2
basename: test.jpg
test2.jpg
dirname: .
basename: test2.jpg
参考URL
php でファイル操作する方法
2018-07-29目的
大量のデータをバッチで処理する時に
最後まで処理した id をファイルに記録しておく必要がありました。
その時にファイル操作が必要だったので、挙動を確認します。
やってみる
position.txt に最後に処理した id を記憶します。
まずは、手動で作っておきます。
position.txt
100
php_file.php
<?php
// 最後に処理した id を取得します
$file_path = './position.txt';
$a = file($file_path);
var_dump($a);
$b = file_get_contents($file_path);
var_dump($b);
// id: 101 を処理
// id: 102 を処理
// ...
// id: 999 を処理
// 最後に処理した id を記録します
$c = file_put_contents($file_path, 999);
$c2 = file($file_path);
var_dump($c2);
実行結果
$ php php_file.php
array(1) {
[0]=>
string(4) "100
"
}
string(4) "100
"
array(1) {
[0]=>
string(3) "999"
}
参考URL
rbenv を使ってみる
2018-07-29目的
ruby のバージョンを任意のタイミングで切り替えることができるようにします。
今回、rbenv を利用します。
方法
利用する ruby を指定します
$ brew upgrade ruby-build
インストールできる ruby を確認します
$ rbenv install --list
今回 ruby の 2.2.7 をインストールしたいので下記のように指定します
$ rbenv install 2.2.7
バージョンを確認します
$ rbenv versions
参考
公開鍵認証で ssh 接続する
2018-07-28目的
レンタルサーバーを借りているのですが、
ssh 接続する時にパスワードを都度入力しています。
今回、 公開鍵認証でパスワードを入力せずに ssh 接続するできるようにします。
方法
鍵の生成
ローカル PC で秘密鍵、公開鍵を作成します。
$ cd ~/.ssh
$ ssh-keygen -t rsa -f hogehoge
ここで生成された hogehoge.pub が公開鍵です。
公開鍵をレンタルサーバーに転送
scp コマンドなどを駆使して先ほど作成した hogehoge.pub (公開鍵) を
レンタルサーバーに転送します。
公開鍵をレンタルサーバーに登録
$ touch ~/.ssh/authorized_keys
$ cat hogehoge.pub >> ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys
パーミッションは 600 にするのが良いようなので、今回指定します。
(なぜ 600 なのかは別の機会に…)
これで ssh 接続する時にパスワードが不要になります。
終わりに
ssh-keygen で鍵を作成する時に色々と強度があるようです。
参考
brew コマンドが動かなくなっていた件
2018-07-22目的
brew コマンドでインストールしようとしたところエラーが発生しました。
どうにかして使えるようにした時の記録です。
発生したエラー
tmux を利用しようと思い、インストールしてみたところ下記のエラーが発生しました。
$ brew install tmux
==> Installing dependencies for tmux: makedepend, libevent
==> Installing tmux dependency: makedepend
==> Downloading http://xorg.freedesktop.org/releases/individual/util/makedepend-1.0.5.tar.bz2
Already downloaded: /Library/Caches/Homebrew/makedepend-1.0.5.tar.bz2
==> Downloading http://xorg.freedesktop.org/releases/individual/proto/xproto-7.0.25.tar.bz2
Already downloaded: /Library/Caches/Homebrew/makedepend--xproto-7.0.25.tar.bz2
==> ./configure --disable-silent-rules --prefix=/private/tmp/makedepend20180701-31856-1cid5cf/makedepend-1.0.5/xproto
checking for suffix of executables...
checking whether we are cross compiling... configure: error: in `/private/tmp/makedepend--xproto20180701-31856-1md6eg2/xproto-7.0.25':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
READ THIS: https://git.io/brew-troubleshooting
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:921:in `connect': Failed to connect to: https://api.github.com/search/issues?q=makedepend+repo:Homebrew/homebrew+in:title+state:open&per_page=100 (GitHub::Error)
SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: tlsv1 alert protocol version
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:921:in `block in connect'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:921:in `connect'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:851:in `start'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:313:in `open_http'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:709:in `buffer_open'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:210:in `block in open_loop'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:208:in `catch'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:208:in `open_loop'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:149:in `open_uri'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:689:in `open'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:30:in `open'
from /usr/local/Library/Homebrew/utils.rb:354:in `open'
from /usr/local/Library/Homebrew/utils.rb:384:in `issues_matching'
from /usr/local/Library/Homebrew/utils.rb:412:in `issues_for_formula'
from /usr/local/Library/Homebrew/exceptions.rb:145:in `fetch_issues'
from /usr/local/Library/Homebrew/exceptions.rb:141:in `issues'
from /usr/local/Library/Homebrew/exceptions.rb:184:in `dump'
from /usr/local/Library/brew.rb:162:in `rescue in <main>'
from /usr/local/Library/brew.rb:65:in `<main>'
brew が古くなったのかと思い、brew の update を試みました。
$ brew update
warning: unable to unlink .yardopts: Permission denied
warning: unable to unlink CONTRIBUTING.md: Permission denied
warning: unable to unlink SUPPORTERS.md: Permission denied
fatal: cannot create directory at '.github': Permission denied
Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master
$ sudo brew update
Password:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mach (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/Library/Homebrew/extend/pathname.rb:2:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/Library/Homebrew/global.rb:3:in `<top (required)>'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/Library/brew.rb:15:in `<main>'
解決するためにやったこと
困り果てていたところ、同じ問題に遭遇している人が解決策を書いていくれていました。
/usr/local のパーミッション の修正
$ ls -la /usr/local
total 96
drwxr-xr-x 23 root wheel 782 11 4 2017 .
$ sudo chown $(whoami):admin /usr/local
$ ls -la /usr/local
total 96
drwxr-xr-x 23 hogehoge admin 782 11 4 2017 .
Homebrew を更新
$ cd /usr/local/Library/Homebrew
$ git pull -q origin refs/heads/master:refs/remotes/origin/master
これで brew update が使えるようになりました。
$ brew update
参考
jekyll でシンタックスハイライトを導入する
2018-07-22目的
jekyll でシンタックスハイライトを導入します。
方法
Gem ファイルに rouge を記載する
gem "rouge"
_config.yml に highlighter を設定する
highlighter: rouge
syntax.scss を作成して、読み込む
$ rougify style monokai > syntax.css
自分は、 _includes/head.html
cssを読み込む行を追加しました。
php のコードをシンタックスハイライトしてみる
<?php
echo 'test';
参考
jekyll で検索を実装する方法- lunr の日本語対応
2018-07-21目的
jekyll で検索機能を実装するため、全文検索エンジンの lunr.js を導入する方法を調べました。
しかし、日本語での検索の対応に対応していなかったため、
下記を使い対応します。
導入
github のデモのファイルを参考にしながらセッティングしました。
ライブラリの設置
ライブラリの下記のファイルを配置します。
- lunr.stemmer.support.js
- tinyseg.js
- lunr.ja.js
言語設定
ライブラリを読み込んだら次は
どの言語を利用するかの設定をします。
下記のようにしました。
window.idx = lunr(function () {
this.field('id');
this.field('title', { boost: 10 });
this.field('author');
this.field('category');
// この行を追加しました
this.use(lunr.ja)
var that = this;
$.each(result, function(i, value) {
var row = $.extend({ "id": i }, value)
that.add(row);
});
});
日本語で検索できるようになりました。