2018年4月29日日曜日

Springでlog4j2(Gradleでプロジェクト作成)

以前にSpringでlog4jをmavenの設定で使用するようにしたことがあったけど、今回はGraldeで行ってみる。

SpringでLog4j2

build.gradleファイルを開いて、log4j2を取り込む設定を記述する。

build.gradleの設定内容は次のとおり。
buildscript {
    ext {
        springBootVersion = '1.5.13.BUILD-SNAPSHOT'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

build.gradleを選択したまま、コンテキストメニューを表示して、「Gradle」-「Refresh Gradle Project」をクリック。

src/main/javaを選択したまま、コンテキストメニューを表示して、「New」-「File」をクリック。

File name: に log4j2.xml を入力して「Finish」をクリック。

log4j2.xmlにログ出力の設定内容を記述する。

log4j2.xmlの設定は次のとおり。
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
    <Properties>
        <Property name="app_name">hoge</Property>
        <Property name="date">%d{yyyy-MM-dd HH:mm:ss.SSS}</Property>
        <Property name="daily_log">logs/app_${app_name}_%d{yyyy-MM-dd}.log</Property>
        <Property name="monthly_log">logs/app_monthly_${app_name}_%d{yyyy-MM}.log</Property>
        <Property name="error_daily_log">logs/app_error_${app_name}_%d{yyyy-MM-dd}.log</Property>
    </Properties>
    <appenders>
        <Console
            name="Console"
            target="SYSTEM_OUT"
        >
            <PatternLayout pattern="${date}, [${app_name}], [ %-5level ], %logger{10}, %msg %n" />
        </Console>
        <RollingFile
            name="File"
            fileName="logs/app.log"
            filePattern="${daily_log}.gz"
    >
            <PatternLayout pattern="${date}, [${app_name}], [ %-5level ], %logger{10}, %msg %n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
        <RollingFile
            name="ErrorFile"
            fileName="logs/app_error.log"
            filePattern="${error_daily_log}.gz"
        >
            <RegexFilter regex="\[ ERROR \]" />
            <PatternLayout pattern="${date}, [${app_name}], [ %-5level ], %logger{10}, %msg %n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
        <RollingFile
            name="MonthlyFile"
            fileName="logs/app_monthly.log"
            filePattern="${monthly_log}.gz"
        >
            <PatternLayout pattern="${date}, [${app_name}], [ %-5level ], %logger{10}, %msg %n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </appenders>
    <loggers>
        <root level="info">
            <appender-ref ref="Console" />
            <appender-ref ref="File" />
            <appender-ref ref="MonthlyFile" />
            <appender-ref ref="ErrorFile" />
        </root>
    </loggers>
</configuration>

前回の記事で作成したHogeController.javaにログ出力のコードを追加する。

SpringでHello world(Gradleでプロジェクト作成)

private static・・・、Logger.info("#index START")とimport文が追加になる。

HogeController.javaのソースは次のとおり。
package com.example.hoge;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HogeController {

    private static final Logger logger = LoggerFactory.getLogger(HogeController.class);

    @RequestMapping("/")
    public String index() {
        logger.info("#index START");
        return "Hello World.";
    }
}

log4j2の設定が終わったので、Springを動かしてログ出力されているかを確認する。
プロジェクトを選択したまま、メニューバーの「Run」-「Run As」-「4 Spring Boot App」をクリック。

Hello World. の表示を前回同様に確認する。
プロジェクトを選択したまま、コンテキストメニューを表示して「Refresh」をクリック。

log4j2.xmlで設定したとおりに、logsフォルダが作成され、その下にログが出力されている。これでおわり。

2018年4月24日火曜日

SpringでHello world(Gradleでプロジェクト作成)

Spring Tool SuiteにBuildshipをインストールしたので、プロジェクトを作成し、HelloWorldをしてみる。

Package Explorer上でポップアップを表示させ、「New」-「Spring Starter Project」をクリック。

Name に hoge を入力。
Type: は Gradle (Buildship 2.x) を選択。
Package に com.example.hoge を入力。
「Next」をクリック。

Web をチェックして、「Next」をクリック。

「Finish」をクリック。

プロジェクトができているので、 com.exmple.hoge パッケージにHelloWorld出力用のクラスを作成する。
com.exmple.hoge パッケージを選択して、ポップアップを表示して、「New」-「Class」をクリック。

Name: に HogeController を入力して、「Finish」をクリック。

HogeControllerのプログラムを次のようにコーディングする。
package com.example.hoge;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HogeController {

    @RequestMapping("/")
    public String index() {
        return "Hello World.";
    }
}

メニューバーの「Run」-「Run As」-「4 Spring Boot App」をクリック。

Spring Boot app起動のログがコンソールに出力される。
Started HogeApplication・・・と出力されていたら、ブラウザで動作確認できる。


ブラウザで動作確認する。
アドレスバーに、 http://localhost:8080 を入力。
Hello World. が出力された。おわり。

2018年4月23日月曜日

Spring Tool SuiteにBuildshipをインストール

以前にSTS Gradleをインストールしたけど、"Migration from STS Gradle to Buildship" The STS Gradle plugin is under minimal maintenance and may be discontinued in the future. A document explaining the migration process from STS Gradle to Buildship can be found here ってメッセージが表示した。

Spring Tool SuiteにGradleのプラグインをインストール

なので、Buildshipをインストールすることにした。


hereのリンク(Migration guide from STS Gradle to Buildship)をクリックすると、STS GradleからBuildshipの移行方法が書いてあるサイトが表示される。
移行の手順は次のように書いてある。



まず、手順1にしたがって、最新のBuildshipをインストールすることにする。
(手順1 Install the latest Buildship release. Download locations and instructions are in the Buildship)


メニューバーの 「Help」-「Install New Software...」を選択。


Work with: に http://download.eclipse.org/buildship/updates/e47/releases/2.x を入力して、「Add...」をクリック。

Name: に Buildship 2.x
Location: に http://download.eclipse.org/buildship/updates/e47/releases/2.x を入力して、「OK」をクリック。

Buildship: Eclipse Plug-ins for Gradle がチェックの状態で「Next」をクリック。


「Next」をクリック。

I accept the terms of the license agreement を選択して、「Finish」をクリック。

Spring Tool Suiteの再起動が促されるので、「Restart Now」をクリック。

Spring Tool Suiteの再起動後に、またも、Migration from STS Gradle to Buildship のダイアログが表示されたが、「OK」をクリックして進める。


手順2は、移行したいプロジェクトのパスを控えておきなさいとのことだけど、その気はないので、この手順は実施しない。
(手順2 Note the root module location of the project you want to migrate.)


手順3は、ワークスペースからプロジェクトをファイルを残して削除する、とのことなので、指示通りにする。


(手順3 Delete the projects from the workspace without physically removing them from the file system.)


プロジェクトを選択して、ポップアップメニューを表示して、「Delete」をクリック。

Delete project contents on disk [cannot be undone] はチェックせず、「OK」をクリック。


手順4から8は、元々あったプロジェクトをBuildshipに移行する手順であるが、今回それは行わないので、その次の手順のSTS Gradleのアンインストールを行う。
(手順4 Open the Buildship import wizard and specify the root project location.
Optionally set the advanced options (Gradle version, JVM argument, etc.) and check the project import preview.
手順5 Click Finish to execute the import.
手順6 When prompted, choose Overwrite Eclipse descriptors to perform a clean import, or choose the Keep option to preserve the plugin configuration.
手順7 Disable the hidden file filter in the Project/Package Explorer view.
手順8 For all projects, delete the .settings/gradle folder (it contains configuration data from STS Gradle).)

STS Gradleのアンインストールの手順は次のとおり。
これも、Migration guide from STS Gradle to Buildship のサイトに書いてある。

メニューバーの「Spring Tool Suite」-「Spring Tool Suiteについて」をクリック。

「Installation Details」をクリック。

「Installed Software」タブの「Gradle IDE」をクリック。

「Finish」をクリック。

Gradle IDEのアンインストールを反映させるためにSpring Tool Suiteの再起動が促されるので「Restart Now」をクリック。これで終わり。

2018年4月10日火曜日

MacにTensorFlowの環境構築

最近、会社でAIを業務に活用できないかという観点で、AIのAPIやAIライブラリのTensorFlowを確認している。
会社のMacにはもちろん、TensorFlowをインストールしている。
そんで、自宅MacにもTensorFlowの環境を構築することにした。

参考にしたサイトは次の通り。
Python3 TensorFlow for Mac 環境構築

ちなみにボクのmacOSは、10.13.4(macOS High Sierra)。
実施した手順は次から。
全てターミナルでコマンド入力。

1. Xcode Command Line tools

xcode-select --install
xcode-select: note: install requested for command line developer tools


2. Homebrewは以前にインストールしているので、homebrewからpyenvをインストール。

brew install pyenv
Warning: You are using OS X 10.13.
We do not provide support for this pre-release version.
You may encounter build failures or other breakage.
==> Installing dependencies for pyenv: autoconf, pkg-config
==> Installing pyenv dependency: autoconf
==> Downloading http://ftpmirror.gnu.org/autoconf/autoconf-2.69.tar.gz
==> Downloading from http://ftp.jaist.ac.jp/pub/GNU/autoconf/autoconf-2.69.tar.g
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/autoconf/2.69 --with-lispdir=/usr/loc
==> make install
==> Caveats
Emacs Lisp files have been installed to:
/usr/local/share/emacs/site-lisp/

Add the following to your init file to have packages installed by
Homebrew added to your load-path:
(let ((default-directory "/usr/local/share/emacs/site-lisp/"))
  (normal-top-level-add-subdirs-to-load-path))
==> Summary
🍺  /usr/local/Cellar/autoconf/2.69: 70 files, 3.1M, built in 34 seconds
==> Installing pyenv dependency: pkg-config
==> Downloading http://pkgconfig.freedesktop.org/releases/pkg-config-0.28.tar.gz
==> Downloading from https://pkg-config.freedesktop.org/releases/pkg-config-0.28
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/pkg-config/0.28 --disable-host-tool -
checking for gcc... clang
checking whether the C compiler works... no
configure: error: in `/private/tmp/pkg-config20180325-3814-1yqgea/pkg-config-0.28':
configure: error: C compiler cannot create executables
See `config.log' for more details
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': no implicit conversion of nil into String (TypeError)
 from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
 from /usr/local/Library/ENV/4.3/gcc-4.2:4:in `
' READ THIS: https://git.io/brew-troubleshooting /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:359:in `open_http': 422 Unprocessable Entity (GitHub::Error) from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:737:in `buffer_open' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:212:in `block in open_loop' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:210:in `catch' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:210:in `open_loop' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:151:in `open_uri' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:717:in `open' from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:31:in `open' from /usr/local/Library/Homebrew/utils.rb:436:in `open' from /usr/local/Library/Homebrew/utils.rb:466:in `issues_matching' from /usr/local/Library/Homebrew/utils.rb:498:in `issues_for_formula' from /usr/local/Library/Homebrew/exceptions.rb:209:in `fetch_issues' from /usr/local/Library/Homebrew/exceptions.rb:205:in `issues' from /usr/local/Library/Homebrew/exceptions.rb:248:in `dump' from /usr/local/Library/brew.rb:160:in `rescue in
' from /usr/local/Library/brew.rb:67:in `
'


3. なんか、怒られた。
「/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/open-uri.rb:359:in `open_http': 422 Unprocessable Entity (GitHub::Error)」でググってみる。
すると、
次のサイトが見つかった。
Issue with brew installation #4

なんか、「After uninstall brew, and reinstall it, I solved this problem. Hope help.」って書いてある。
homebrewをもう一度インストールしてみよう。(アンインストールはせず、インストールのみした。)

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew
==> The following new directories will be created:
/usr/local/Homebrew
/usr/local/Frameworks
/usr/local/sbin
/usr/local/share/zsh
/usr/local/share/zsh/site-functions

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir -p /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/share/zsh /usr/local/share/zsh/site-functions
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /bin/chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /usr/sbin/chown xxxxx /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Homebrew /usr/local/Frameworks /usr/local/sbin /usr/local/share/zsh /usr/local/share/zsh/site-functions
==> /usr/bin/sudo /bin/mkdir -p /Users/xxxxx/Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Users/xxxxx/Library/Caches/Homebrew
==> /usr/bin/sudo /usr/sbin/chown xxxxx /Users/xxxxx/Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 99382, done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 99382 (delta 27), reused 38 (delta 17), pack-reused 99317
Receiving objects: 100% (99382/99382), 22.67 MiB | 1.04 MiB/s, done.
Resolving deltas: 100% (72304/72304), done.
From https://github.com/Homebrew/brew
 * [new branch]          master     -> origin/master
 * [new tag]             0.1        -> 0.1
 * [new tag]             0.2        -> 0.2
 * [new tag]             0.3        -> 0.3
 * [new tag]             0.4        -> 0.4
 * [new tag]             0.5        -> 0.5
 * [new tag]             0.6        -> 0.6
 * [new tag]             0.7        -> 0.7
 * [new tag]             0.7.1      -> 0.7.1
 * [new tag]             0.8        -> 0.8
 * [new tag]             0.8.1      -> 0.8.1
 * [new tag]             0.9        -> 0.9
 * [new tag]             0.9.1      -> 0.9.1
 * [new tag]             0.9.2      -> 0.9.2
 * [new tag]             0.9.3      -> 0.9.3
 * [new tag]             0.9.4      -> 0.9.4
 * [new tag]             0.9.5      -> 0.9.5
 * [new tag]             0.9.8      -> 0.9.8
 * [new tag]             0.9.9      -> 0.9.9
 * [new tag]             1.0.0      -> 1.0.0
 * [new tag]             1.0.1      -> 1.0.1
 * [new tag]             1.0.2      -> 1.0.2
 * [new tag]             1.0.3      -> 1.0.3
 * [new tag]             1.0.4      -> 1.0.4
 * [new tag]             1.0.5      -> 1.0.5
 * [new tag]             1.0.6      -> 1.0.6
 * [new tag]             1.0.7      -> 1.0.7
 * [new tag]             1.0.8      -> 1.0.8
 * [new tag]             1.0.9      -> 1.0.9
 * [new tag]             1.1.0      -> 1.1.0
 * [new tag]             1.1.1      -> 1.1.1
 * [new tag]             1.1.10     -> 1.1.10
 * [new tag]             1.1.11     -> 1.1.11
 * [new tag]             1.1.12     -> 1.1.12
 * [new tag]             1.1.13     -> 1.1.13
 * [new tag]             1.1.2      -> 1.1.2
 * [new tag]             1.1.3      -> 1.1.3
 * [new tag]             1.1.4      -> 1.1.4
 * [new tag]             1.1.5      -> 1.1.5
 * [new tag]             1.1.6      -> 1.1.6
 * [new tag]             1.1.7      -> 1.1.7
 * [new tag]             1.1.8      -> 1.1.8
 * [new tag]             1.1.9      -> 1.1.9
 * [new tag]             1.2.0      -> 1.2.0
 * [new tag]             1.2.1      -> 1.2.1
 * [new tag]             1.2.2      -> 1.2.2
 * [new tag]             1.2.3      -> 1.2.3
 * [new tag]             1.2.4      -> 1.2.4
 * [new tag]             1.2.5      -> 1.2.5
 * [new tag]             1.2.6      -> 1.2.6
 * [new tag]             1.3.0      -> 1.3.0
 * [new tag]             1.3.1      -> 1.3.1
 * [new tag]             1.3.2      -> 1.3.2
 * [new tag]             1.3.3      -> 1.3.3
 * [new tag]             1.3.4      -> 1.3.4
 * [new tag]             1.3.5      -> 1.3.5
 * [new tag]             1.3.6      -> 1.3.6
 * [new tag]             1.3.7      -> 1.3.7
 * [new tag]             1.3.8      -> 1.3.8
 * [new tag]             1.3.9      -> 1.3.9
 * [new tag]             1.4.0      -> 1.4.0
 * [new tag]             1.4.1      -> 1.4.1
 * [new tag]             1.4.2      -> 1.4.2
 * [new tag]             1.4.3      -> 1.4.3
 * [new tag]             1.5.0      -> 1.5.0
 * [new tag]             1.5.1      -> 1.5.1
 * [new tag]             1.5.10     -> 1.5.10
 * [new tag]             1.5.11     -> 1.5.11
 * [new tag]             1.5.12     -> 1.5.12
 * [new tag]             1.5.13     -> 1.5.13
 * [new tag]             1.5.14     -> 1.5.14
 * [new tag]             1.5.2      -> 1.5.2
 * [new tag]             1.5.3      -> 1.5.3
 * [new tag]             1.5.4      -> 1.5.4
 * [new tag]             1.5.5      -> 1.5.5
 * [new tag]             1.5.6      -> 1.5.6
 * [new tag]             1.5.7      -> 1.5.7
 * [new tag]             1.5.8      -> 1.5.8
 * [new tag]             1.5.9      -> 1.5.9
HEAD is now at 7ef1aa6dd Merge pull request #4034 from MikeMcQuaid/faq-installation
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Counting objects: 4720, done.
remote: Compressing objects: 100% (4517/4517), done.
remote: Total 4720 (delta 53), reused 641 (delta 16), pack-reused 0
Receiving objects: 100% (4720/4720), 3.84 MiB | 666.00 KiB/s, done.
Resolving deltas: 100% (53/53), done.
Tapped 4516 formulae (4,762 files, 12MB)
==> Cleaning up /Library/Caches/Homebrew...
Removing: /Library/Caches/Homebrew/gradle-2.7.zip... (42.7MB)
Removing: /Library/Caches/Homebrew/pkg-config-0.28.tar.gz... (1.8MB)
==> Migrating /Library/Caches/Homebrew to /Users/xxxxx/Library/Caches/Homebrew...
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
Error: Could not link:
/usr/local/share/man/man1/brew.1

Please delete these paths and run `brew update`.
Error: Could not link:
/usr/local/share/doc/homebrew

Please delete these paths and run `brew update`.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics.html

==> Next steps:
- Run `brew help` to get started
- Further documentation:
    https://docs.brew.sh


4. 再度、pyenvをインストール。

brew install pyenv
==> Installing dependencies for pyenv: pkg-config, openssl, readline
==> Installing pyenv dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.29.2.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.29.2: 11 files, 627.2KB
==> Installing pyenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB
==> Installing pyenv dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-7.0.3_1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-7.0.3_1.high_sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only..

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
🍺  /usr/local/Cellar/readline/7.0.3_1: 46 files, 1.5MB
==> Installing pyenv
==> Downloading https://homebrew.bintray.com/bottles/pyenv-1.2.3.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pyenv-1.2.3.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/pyenv/1.2.3: 597 files, 2.4MB


5. pyenv-virtualenvをインストール
(5.は不要な気がする。ただ、実際の作業ログとして残しておく。)

brew install pyenv-virtualenv
==> Downloading https://github.com/pyenv/pyenv-virtualenv/archive/v1.1.1.tar.gz
==> Downloading from https://codeload.github.com/pyenv/pyenv-virtualenv/tar.gz/v1.1.1
######################################################################## 100.0%
==> ./install.sh
==> Caveats
To enable auto-activation add to your profile:
  if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
==> Summary
🍺  /usr/local/Cellar/pyenv-virtualenv/1.1.1: 20 files, 60.6KB, built in 3 seconds


6.  TensorFlow環境構築のコマンドを叩く。

pyenv virtualenv 3.6.0 TensorFlow
Requirement already satisfied: setuptools in /Users/xxxxx/.pyenv/versions/3.6.0/envs/TensorFlow/lib/python3.6/site-packages
Requirement already satisfied: pip in /Users/xxxxx/.pyenv/versions/3.6.0/envs/TensorFlow/lib/python3.6/site-packages


7.  6. TensorFlow環境構築のコマンドで設定した内容を反映させるコマンドを叩く。

pyenv rehash


8.  6. と7.で設定した環境に切り替える。

pyenv global TensorFlow


9. Python3をインストール。

brew install python3
==> Installing dependencies for python: gdbm, sqlite, xz
==> Installing python dependency: gdbm
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.14.1_1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gdbm-1.14.1_1.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/gdbm/1.14.1_1: 20 files, 555.7KB
==> Installing python dependency: sqlite
==> Downloading https://homebrew.bintray.com/bottles/sqlite-3.23.0.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring sqlite-3.23.0.high_sierra.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local,
because macOS provides an older sqlite3.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/sqlite/lib
    CPPFLAGS: -I/usr/local/opt/sqlite/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/sqlite/lib/pkgconfig

==> Summary
🍺  /usr/local/Cellar/sqlite/3.23.0: 11 files, 3MB
==> Installing python dependency: xz
==> Downloading https://homebrew.bintray.com/bottles/xz-5.2.3.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring xz-5.2.3.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/xz/5.2.3: 92 files, 1.4MB
==> Installing python
==> Downloading https://homebrew.bintray.com/bottles/python-3.6.5.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring python-3.6.5.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/python/3.6.5/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cel
==> /usr/local/Cellar/python/3.6.5/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cel
==> /usr/local/Cellar/python/3.6.5/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/usr/local/Cel
==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

If you need Homebrew's Python 2.7 run
  brew install python@2

Pip, setuptools, and wheel have been installed. To update them run
  pip3 install --upgrade pip setuptools wheel

You can install Python packages with
  pip3 install 
They will install into the site-package directory
  /usr/local/lib/python3.6/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
🍺  /usr/local/Cellar/python/3.6.5: 4,705 files, 99.4MB


10. pip3でvirtualenvをインストール。
(前述した5.は不要な気がする。多分、10.があればOK.)

pip3 install virtualenv
Collecting virtualenv
  Downloading virtualenv-15.2.0-py2.py3-none-any.whl (2.6MB)
    100% |████████████████████████████████| 2.6MB 524kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.2.0


11. スーパーユーザになる。

su - root
Password:


12.  TensorFlowをインストール。

pip3 install --upgrade tensorflow
Collecting tensorflow
  Downloading tensorflow-1.7.0-cp36-cp36m-macosx_10_11_x86_64.whl (45.3MB)
    100% |################################| 45.3MB 36kB/s
Requirement already up-to-date: numpy>=1.13.3 in /usr/local/lib/python3.6/site-packages (from tensorflow)
Requirement already up-to-date: wheel>=0.26 in /usr/local/lib/python3.6/site-packages (from tensorflow)
Requirement already up-to-date: six>=1.10.0 in /usr/local/lib/python3.6/site-packages (from tensorflow)
Requirement already up-to-date: protobuf>=3.4.0 in /usr/local/lib/python3.6/site-packages (from tensorflow)
Collecting tensorboard<1 .8.0="">=1.7.0 (from tensorflow)
  Downloading tensorboard-1.7.0-py3-none-any.whl (3.1MB)
    100% |################################| 3.1MB 501kB/s
Requirement already up-to-date: grpcio>=1.8.6 in /usr/local/lib/python3.6/site-packages (from tensorflow)
Collecting termcolor>=1.1.0 (from tensorflow)
  Downloading termcolor-1.1.0.tar.gz
Collecting absl-py>=0.1.6 (from tensorflow)
  Downloading absl-py-0.1.13.tar.gz (80kB)
    100% |################################| 81kB 4.8MB/s
Collecting gast>=0.2.0 (from tensorflow)
  Downloading gast-0.2.0.tar.gz
Collecting astor>=0.6.0 (from tensorflow)
  Downloading astor-0.6.2-py2.py3-none-any.whl
Requirement already up-to-date: setuptools in /usr/local/lib/python3.6/site-packages (from protobuf>=3.4.0->tensorflow)
Requirement already up-to-date: markdown>=2.6.8 in /usr/local/lib/python3.6/site-packages (from tensorboard<1 .8.0="">=1.7.0->tensorflow)
Requirement already up-to-date: html5lib==0.9999999 in /usr/local/lib/python3.6/site-packages (from tensorboard<1 .8.0="">=1.7.0->tensorflow)
Collecting werkzeug>=0.11.10 (from tensorboard<1 .8.0="">=1.7.0->tensorflow)
  Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    100% |################################| 327kB 2.5MB/s
Requirement already up-to-date: bleach==1.5.0 in /usr/local/lib/python3.6/site-packages (from tensorboard<1 .8.0="">=1.7.0->tensorflow)
Building wheels for collected packages: termcolor, absl-py, gast
  Running setup.py bdist_wheel for termcolor ... done
  Stored in directory: /var/root/Library/Caches/pip/wheels/de/f7/bf/1bcac7bf30549e6a4957382e2ecab04c88e513117207067b03
  Running setup.py bdist_wheel for absl-py ... done
  Stored in directory: /var/root/Library/Caches/pip/wheels/76/f7/0c/88796d7212af59bb2f496b12267e0605f205170781e9b86479
  Running setup.py bdist_wheel for gast ... done
  Stored in directory: /var/root/Library/Caches/pip/wheels/8e/fa/d6/77dd17d18ea23fd7b860e02623d27c1be451521af40dd4a13e
Successfully built termcolor absl-py gast
Installing collected packages: werkzeug, tensorboard, termcolor, absl-py, gast, astor, tensorflow
Successfully installed absl-py-0.1.13 astor-0.6.2 gast-0.2.0 tensorboard-1.7.0 tensorflow-1.7.0 termcolor-1.1.0 werkzeug-0.14.1


13. TensorFlowを動かしてみる。

python3
Python 3.6.5 (default, Mar 30 2018, 06:41:53)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2018-04-08 23:23:36.689778: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>> quit()


TensorFlowが動いた。
これで終わり。