LoginSignup
3
0

More than 1 year has passed since last update.

Ruby(Sinatra)+Postgres の開発をローカルでやろう! Vol.3 ~無限 Windows でもネイティブで動かしたいだろ編~

Last updated at Posted at 2022-12-23

Life is Tech ! Tokai Advent Calendar 2022」24 日目の記事になります。

はじめに

メリークリスマス!
Life is Tech! で関東 Web サービスコースメンターをやっているいっそです!
僕は国道 1 号線 (東海道) 沿いに住んでるんですが、実質東海だろ?ってことで、今回は東海のカレンダーにお邪魔しました!

今回は、「Ruby(Sinatra)+Postgres の開発をローカルでやろう! Vol.3 ~無限 Windows でもネイティブで動かしたいだろ編~」ということで、いつもは Cloud9 で運用されている Web サービスコースの開発環境を Windows のローカル環境でも建ててみたいと思います!

この記事は昨日と 3 日間に分けて投稿されています!
興味あればぜひ読んでみてください。

1 日目の macOS, Linux 用記事は こちら
2 日目の Docker Compose 用記事は こちら

Web サービス開発に必要なソフトウェア

こちらは Vol.1 で解説しているのでそちらを参照してください。

インストール

それでは、実際にインストールをしていきます。

Scoop

本記事では簡単にするために Scoop を使用したインストール方法をご紹介します。

Scoop は Windows 用パッケージマネージャーで、コマンド 1 発でソフトウェア(および依存ソフトウェア)のインストールと PATH 通しを行ってくれる便利なものです。
インストールだけでなく、アンインストールやアップデートもめちゃくちゃ簡単にできるのも特徴です。

PowerShell を起動し、以下のコマンドでインストールすることができます。(本チュートリアルは全て PowerShell を想定しています)

PowerShell
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
irm get.scoop.sh | iex

Ruby 3.0.0 のインストール

Ruby 3.0.0 のインストール方法を紹介します。

Ruby 3.0.0

いよいよ Ruby 3.0.0 のインストールを行います。
以下のコマンドを実行するだけです。Ruby 3.0.0 が含まれるバケットはなかったので、僕が作成しました。(レポジトリは こちら )
GitHub にあるバケットを導入するために git もインストールしています。

PowerShell
scoop install git
scoop bucket add lit-webs https://github.com/isso-719/lit-webs-scoop

scoop install ruby300

以上で Ruby 3.0.0 のインストールは完了です!

PostgreSQL 15 のインストールと設定

次は PostgreSQL 15 のインストールと設定を行っていきます。

PostgreSQL のインストール

Scoop を用いてインストールします。
postgresql では vcredist に含まれる dll を使用している箇所があるため、それもインストールします。

また、管理者権限で実行したい部分があるので sudo をインストールしています。(管理者権限が必要です)

PowerShell
scoop install sudo
scoop bucket add extras
sudo scoop install vcredist

scoop install postgresql

PostgreSQL の起動

PostgreSQL を起動します。

PowerShell
pg_ctl start

PostgreSQL の自動起動を設定します。

PowerShell
sudo pg_ctl register -S a

PostgreSQL の設定

データベースを操作するユーザを作成します。

PowerShell
createuser -U postgres -d -l -r -s $env:UserName

次に現在のユーザでデータベースを作成します。

PowerShell
createdb $env:UserName

pg config の設定を行います。

PowerShell
bundle config build.pg -- --with-pg-dir=C:\Users\$env:UserName\scoop\apps\postgresql\*\data

これで PostgreSQL のインストールも完了です!

検証

Count 素材を使用して実際に使用できるか検証します。

PowerShell
> bundle
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Using rake 13.0.6
Fetching concurrent-ruby 1.1.10
Installing concurrent-ruby 1.1.10
Fetching i18n 1.12.0
Installing i18n 1.12.0
Fetching minitest 5.16.3
Installing minitest 5.16.3
Fetching tzinfo 2.0.5
Installing tzinfo 2.0.5
Fetching zeitwerk 2.6.6
Installing zeitwerk 2.6.6
Fetching activesupport 6.1.7
Installing activesupport 6.1.7
Fetching activemodel 6.1.7
Installing activemodel 6.1.7
Fetching activerecord 6.1.7
Installing activerecord 6.1.7
Using bundler 2.2.3
Fetching multi_json 1.15.0
Installing multi_json 1.15.0
Fetching ruby2_keywords 0.0.5
Installing ruby2_keywords 0.0.5
Fetching mustermann 2.0.2
Installing mustermann 2.0.2
Fetching pg 1.4.5 (x64-mingw32)
Installing pg 1.4.5 (x64-mingw32)
Fetching rack 2.2.4
Installing rack 2.2.4
Fetching rack-protection 2.2.4
Installing rack-protection 2.2.4
Fetching tilt 2.0.11
Installing tilt 2.0.11
Fetching sinatra 2.2.4
Installing sinatra 2.2.4
Fetching sinatra-activerecord 2.0.26
Installing sinatra-activerecord 2.0.26
Fetching sinatra-contrib 2.2.4
Installing sinatra-contrib 2.2.4
Fetching webrick 1.7.0
Installing webrick 1.7.0
Bundle complete! 7 Gemfile dependencies, 21 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

> rake db:create
Created database 's_count'

> rake db:migrate
== 20190423024537 CreateCounts: migrating =====================================
-- create_table(:counts)
   -> 0.0063s
== 20190423024537 CreateCounts: migrated (0.0074s) ============================

> ruby app.rb -o 0.0.0.0
[2022-12-24 01:49:08] INFO  WEBrick 1.7.0
[2022-12-24 01:49:08] INFO  ruby 3.0.0 (2020-12-25) [x64-mingw32]
== Sinatra (v2.2.3) has taken the stage on 4567 for development with backup from WEBrick
[2022-12-24 01:49:08] INFO  WEBrick::HTTPServer#start: pid=5920 port=4567

ファイアウォールが出てきますが、チェックつけて、アクセスを許可して…

Untitled.png

http://localhost:4567 にアクセスすると、正常に動いてることが確認できました!

Untitled 1.png

終わりに

今回は Windows で Ruby(Sinatra)+Postgres の開発環境を Scoop を用いて構築してみました。
Scoop めっちゃ良きですね!これからソフトインストールするときは一旦 Scoop でインストールできないか確認してからインストールしてみようと思いました!

以上、「Life is Tech ! Tokai Advent Calendar 2022」24 日目の記事でした。

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