LoginSignup
5
0

More than 1 year has passed since last update.

Windows + WSL2 + Ubuntu 20.4 + Phoenix16.6 with Sqlite3環境構築時にエラーが起こったのでその対応メモ 小ネタ

Last updated at Posted at 2022-02-15

概要

表題の通り。
ぶっちゃけて言うと問題はあんまりElixir関係ない。

経緯

Windows10の環境に直接ElixirとPhoenixをインストールしていたがバージョンアップしたら
なんか動かなくなってしまった。

OTP24が原因?よー分からん。まいったなぁと思っていたところ、
思い出したのが@piacerex さんの記事。

Windows10の環境に元々、WSL2にUbuntu入れてたんで、せっかくだからUbuntu上にelixirとphoenixを
入れてみよう。自分の環境のバージョンは20.4で、件の記事の環境は18.04と違いがあるが、まぁ多分なんとかなるだろう。
(根拠なし)

とにかく使えればいいのだ。

でも、postgreSQLまで入れるのはちょっと大袈裟だし、とりあえずSQLite3でいいか
と思ってやってみました。

elixirのインストール

@piacerexさんの記事の丸パクリですが、以下を実行

wget https://packages.erlang-solutions.com/erlang-solutions_2.0_all.deb && sudo dpkg -i erlang-solutions_2.0_all.deb
sudo apt update
sudo apt install esl-erlang -y
sudo apt install elixir -y
sudo apt install erlang-dev -y
sudo apt install erlang-dev -y
sudo apt install erlang-xmerl -y
sudo apt install erlang-parsetools -y
sudo apt install inotify-tools -y

ここまでは問題なくいきました。

Phoenixのプロジェクトを作ってみる

<マシン名>:~/app$ mix phx.new slite_app --database sqlite3

さぁecto.create

サクッと終わらせるぞ
と思ったら、、、

<マシン名>:~/app/slite_app$ mix ecto.create
==> exqlite
 CC sqlite3_nif.o
/bin/sh: 1: cc: not found
make: *** [Makefile:128: /home/mandl/app/ex_learn/slite_app/_build/dev/lib/exqlite/obj/sqlite3_nif.o] Error 127
could not compile dependency :exqlite, "mix compile" failed. Errors may have been logged above. You can recompile this dependency with "mix deps.compile exqlite", update it with "mix deps.update exqlite" or clean it with "mix deps.clean exqlite"
==> slite_app
** (Mix) Could not compile with "make" (exit status: 2).
You need to have gcc and make installed. If you are using
Ubuntu or any other Debian-based system, install the packages
"build-essential". Also install "erlang-dev" package if not
included in your Erlang/OTP version. If you're on Fedora, run
"dnf group install 'Development Tools'".

コケました。

対応方法

なになに? 要はmakebuild-essential入れろってこと?
追加してみた

$ sudo apt install make
$ sudo apt install build-essential

今度はうまくいきました

<マシン名>:~/app/slite_app$ mix ecto.create
==> exqlite
 CC sqlite3_nif.o
 CC sqlite3.o
 LD sqlite3_nif.so
Compiling 10 files (.ex)

####   省略   ####

==> slite_app
Compiling 14 files (.ex)
Generated slite_app app
The database for SliteApp.Repo has been created

以上です。

追記(2022年2月18日)

元記事執筆者の@piacerexさんよりコメントをいただきました。
ふむ。どうやら20.4はいろいろなんか問題があるらしい。

そういうことなら、せっかくなので元記事のPostgreSQLのインストールとNxのインストールもやってみて、
人柱となってみようと思います。

PostgreSQLのインストール

一旦、元記事をそのままコピペ。問題があったら都度対応していきます。

$ sudo apt install curl ca-certificates gnupg -y
Reading package lists... Done
Building dependency tree
<省略>

$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
<省略>

$ sudo apt update
Hit:1 https://download.docker.com/linux/ubuntu focal InRelease
Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Ign:3 http://binaries.erlang-solutions.com/debian focal InRelease
Hit:4 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:5 http://binaries.erlang-solutions.com/debian focal Release
Get:7 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
<省略>

$ sudo apt install postgresql -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
<省略>

$ sudo service postgresql start
 * Starting PostgreSQL 12 database server

sudo su - postgres
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 4.19.128-microsoft-standard x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
<省略>

~$ psql

postgres=# alter role postgres with password 'postgres';
ALTER ROLE

postgres=# \q

~$ exit
logout

何事もなくインストールが終了してしまいました。
まだ、油断は禁物。画面が動かなければ意味がない

Phoenixのプロジェクトを作成。今度はDBをPostgreSQLに

--databaseオプションを省略してmix phx.new=デフォルト設定のPostgreSQLを使用

$ mix phx.new basic
<省略>
Fetch and install dependencies? [Yn] Y
* running mix deps.get
* running mix deps.compile

We are almost there! The following steps are missing:

    $ cd basic

Then configure your database in config/dev.exs and run:

    $ mix ecto.create

Start your Phoenix app with:

    $ mix phx.server

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phx.server

ここで問題ないがでないのは想定内。
いよいよ緊張の瞬間その①ecto.createでDBを作ってみる

$ cd basic/
$ mix ecto.create
The database for Basic.Repo has been created

行けた!

まだだ画面を見るまでは安心できない。

画面を作ってみる。

$ mix phx.gen.html Accounts User users name:string

出力がうまくいったらmigrate!
なんかあるとしたらここか?

$ mix ecto.migrate
Compiling 5 files (.ex)
Generated basic app

17:42:30.521 [info]  == Running 20220218084039 Basic.Repo.Migrations.CreateUsers.change/0 forward

17:42:30.524 [info]  create table users

17:42:30.538 [info]  == Migrated 20220218084039 in 0.0s

お?行けた。

lib/basic_web/router.exを編集だ

  scope "/", BasicWeb do
    pipe_through :browser

    get "/", PageController, :index
    resources "/users", UserController #←追記
  end

Phoenixを立ち上げる

なんかこのままうまくいきそうな気がしてきました。
iex -S mix.phx.serverでサーバー立ち上げ

$ iex -S mix phx.server
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

[info] Running BasicWeb.Endpoint with cowboy 2.9.0 at 127.0.0.1:4000 (http)
[info] Access BasicWeb.Endpoint at http://localhost:4000
Interactive Elixir (1.13.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> [watch] build finished, watching for changes

立ち上がった。

http://localhost:4000/users にアクセス
アクセスできた。

なんか追加してみよう。。。
できた。証拠↓

image.png

何事もなく画面が動きました。

このままの勢いで次に行ってみます。

Nxのインストールと確認

Ctrl + Cでサーバーをストップ。
元記事と同じようにmix.exsを編集します。

  defp deps do
    [
      {:nx, "~> 0.1.0"},  #←追記
      {:phoenix, "~> 1.6.6"},
<省略>

mix deps.get実行!

$ mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
Unchanged:
  castore 0.1.15
  connection 1.1.0
  cowboy 2.9.0
  cowboy_telemetry 0.4.0
<省略>

成功!

再度、サーバー立ち上げ

iex -S mix phx.server

いよいよ最後のフェーズNxの動作確認

iex(2)> t = Nx.tensor([[1, 2], [3, 4]])
#Nx.Tensor<
  s64[2][2]
  [
    [1, 2],
    [3, 4]
  ]
>
iex(3)> Nx.divide(Nx.exp(t), Nx.sum(Nx.exp(t)))
#Nx.Tensor<
  f32[2][2]
  [
    [0.032058604061603546, 0.08714432269334793],
    [0.23688282072544098, 0.6439142227172852]
  ]
>

行けました!

現場からは以上です。

5
0
3

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
5
0