LoginSignup
1
1

初期化されたWindows11を爆速で環境構築するマニュアル(MinGW,Python,Node.js,Rust,Docker)

Last updated at Posted at 2024-06-23

前提条件

  • Windows11
  • 管理者権限は不要(社用PCで管理者権限を使うのはセキュリティホールになる)
  • 商用利用でも無料(例えばDocker Desktopは有料化されたので避けています)
  • 筆者と同等の環境を揃えられるように書いていますが読者に最適な環境とは限りません

目標開発環境の要件

  • ターミナルを起動するとgitbashが開く
  • ターミナルのgitbashで以下のコマンドが叩ける(コメントは解説)
$ git -v && ssh -V
# gitやsshが使える、基礎の基礎

$ gcc --version && gfortran --version && make --version
# MinGW(Minimalist GNU for Windows)が使える
# makeがそのまま使える.

$ pyenv version && python --version && pip -V && pipenv --version
# pyenv-winにより任意のPythonインタプリタバージョンを切り替えて使える
# pyenv-winによりPython 3系がインストールされている
# pipenvとpyenv-winの連携により任意のPythonバージョンのPipfileを展開できる

$ node -v && npm -v && npx -v 
# NodeJSが使える

$ cargo -V
# rustが使える
# 組み込みでもない限りC/C++で新規プロジェクトを立てる理由がない.

$ docker ps && docker compose ls && kubectl version
# Docker Engineが起動している
# Docker Compose V2が使える
# Kubernetesが使える

前書き

本稿はWindows 11を工場出荷状態にクリーンインストールしてから開発環境を復旧するマニュアルです.これがあると嬉しい点としては以下があげられます.

  1. 未経験者が戦力になるまでの時間を短縮できる
  2. 長年の負債によりCドライブの空き容量が逼迫しても全消去する勇気が持てる

これらの利益を得るために開発環境の復旧は1時間以内に終わらせられるように訓練しておくと良いです.

[図1 終焉のCドライブ]
340063588-bc958a96-e63b-4e00-9226-cfebf30c22ff.png

WSL2は説明不要…カスタムディストリビューションを使いましょう

WSL2はUbuntuに開発環境を加えた自分専用のディストリビューションを作ることができるので今更言うことがありません.初期化初日から事前に準備したカスタムディストリビューションで快適に作業しましょう.組み込み開発者などは特におすすめです.

この辺の記事が参考になります.

手順1. GitとGitBashのインストールと設定

  • URLを開き、Download from Windowsボタンからダウンロード&インストール
  • Terminalを起動してGitBashを設定image.png

図左下の新しいプロファイルを追加しますボタンをクリックして図右側のように以下を設定します
名前はbash.
コマンドラインは(標準的なGitインストールならば)C:\Program Files\Git\bin\bash.exe

  • Terminalの既定のプロファイルをGitBashに設定image.png

図左上のスタートアップをクリックし、右上の既定のプロファイルから先ほど作ったbashを設定しましょう.最後に右下保存ボタンをクリックして閉じる

  • (オプション)このタイミングで有識者は~/.ssh/id_rsaを引き継ぐべきです。リモートレポジトリや外部サーバーへアクセスできるようになります

手順2. gcc, makeなどを含むMinGWのインストール.

  • URLを開き図中下側で青色に選択されているx86_64-posix-sehをクリックimage.png

圧縮ファイルのダウンロードが始まる
現時点(2024/06/16)ではx86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z

  • 圧縮ファイルを展開する.中のmingw-w64というディレクトリをホームディレクトリ直下に移動

ホームディレクトリはエクスプローラーのアドレス欄に%USERPROFILE%と打てば開きます。そのなかにドラッグ&ドロップしましょう

x86_64-posix-seh(Architecture)-(Threads)-(Exception)を意味します.
ArchitectureはCPUアーキテクチャ.x86_64(64bit)とi686(32bit)のいずれかですがWindows11は64bitしかないのでx86_64を選択する
Threadsはスレッドモデル.posix(標準)とwin32(Windowsネイティブ)が選択肢ですが、GNUでRustをビルドしたり, mutexを扱うコードを書く場合にwin32では問題になるのでposixを選択する
Exceptionは例外処理.sef,sijiが選択肢(dwarf-2は32bitのみ)、基本的にはsehが良い

環境変数を設定しPATHを通す

  • スタートメニューの🔍にenvと入力しEnterを押すと環境設定画面が開く

image.png

  • オレンジ矢印に沿ってユーザー環境変数のPath%USERPROFILE%\mingw-w64を追加しOKをクリック

image.png

Makeを使えるように設定

  • GitBashを開いてcd $(dirname $(which mingw32-make)) && ln -s mingw32-make make.exe と入力

下記引用の事情でmingw-w64をインストールしただけの状態ではmingw32-makeと打たないとmakeが起動しません.そこでmingw32-makeからmake.exeへのシンボリックリンクを貼ります.なお.exe拡張子が付いたシンボリックリンクでないとコマンドプロンプト等からmakeを起動できません(GitBashは拡張子が無くとも実行可能ファイルと認識してくれます).

最後に make コマンドについてちょっと書いておきます *4。 MinGW で提供されている make コマンドには "mingw32-make.exe" という名前が付いており、コマンドプロンプトから使用する場合は "mingw32-make" とコマンドを打たなければいけません。 なぜこのような名前が付いているのかは以下に書いてあります。
(中略)
つまり、MSYS にも make が含まれており、名前の衝突を防ぐために MinGW の make の名前を変更した、ということのようです。 MSYS をインストールして make を使うのであればこのままでいいと思いますが、MSYS をインストールしないのであれば mingw32-make.exe をコピーして make.exe にリネームしてもいいかと思います。

手順3. 最強のPython環境(pyenv + pipenv).

Pythonには様々なインストール手段があります.Hello Worldレベルでいいなら手段はどうでもいいですが,仕事としてPythonを書くならばインタプリタ切替、パッケージ切替、パッケージインストールの三点が揃ったインストール手段を使いましょう.その点で最強なのがpyenvpipenvの組み合わせです.あらゆるPipfileに対してインタプリタやパッケージのバージョンを気にせず同期できます.

参考記事

Pyenvは公式にはWindowsに対応していないのでPyenv-winというWindows版をインストールします.公式ドキュメントを読むと以下のようなインストール手段があります.

  1. PowerShell - easiest way
  2. Git Commands - default way + adding manual settings
  3. Pyenv-win zip - manual installation
  4. Python pip - for existing users

本稿では2番目のGit Commandを用いる方法でインストールします.1は外部スクリプトを実行するのが色々リスクなので避けました.

GitBashを開いて~/.pyenvにレポジトリをコピーします

# GitBash
git clone https://github.com/pyenv-win/pyenv-win.git ~/.pyenv

次に以下のユーザー環境変数を設定すればPyenvのインストールは完了です.

Variable Value
PYENV %USERPROFILE%\.pyenv\pyenv-win\
PYENV_HOME %USERPROFILE%\.pyenv\pyenv-win\
PYENV_ROOT %USERPROFILE%\.pyenv\pyenv-win\
Pathに追加 %USERPROFILE%.pyenv\pyenv-win\bin

この操作は上記記事の以下を参考にしています

If for some reason you cannot execute PowerShell command(likely on an organization managed device), type "environment variables for you account" in Windows search bar and open Environment Variables dialog.
You will need create those 3 new variables in User Variables section(top half). Let's assume username is my_pc.
|Variable|Value|
|---|---|
|PYENV|C:\Users\my_pc\.pyenv\pyenv-win
|PYENV_HOME|C:\Users\my_pc\.pyenv\pyenv-win
|PYENV_ROOT|C:\Users\my_pc\.pyenv\pyenv-win
And add two more lines to user variable Path.

C:\Users\my_pc\.pyenv\pyenv-win\bin
C:\Users\my_pc\.pyenv\pyenv-win\shims

pyenvがインストールできたら最新版のPythonおよびpipenvをインストールします。

$ pyenv install 3 # インタプリタVer3.**の最新をインストール
:: [Info] ::  Mirror: https://www.python.org/ftp/python
:: [Installing] ::  3.12.3 ...
:: [Info] :: completed! 3.12.3

$ pyenv global 3 # インタプリタVer3.**の最新をグローバルに設定
$ python -V # Pythonインストールを確認
Python 3.12.3 
$ pip install pipenv
(中略)
Successfully installed certifi-2024.6.2 distlib-0.3.8 filelock-3.15.1 pipenv-2024.0.1 platformdirs-4.2.2 setuptools-70.0.0 virtualenv-20.26.2

手順4. Node.jsのインストール

ビルド済みバイナリにパスを通すのが早いです.

  • URLを開いてPrebuilt Binariesからnode-v20.14.0-win-x64.zipなど(2024/06/16時点のLTS)をダウンロード
  • そして~/node-v20.14.0-win-x64にディレクトリを展開
  • ユーザー環境変数PAthに%USERPROFILE%\node-v20.14.0-win-x64を追記してPATHを通します

gitbashを開いて以下のようにコマンドが実行出来たら完了です.

$ node -v && npm -v && npx -v
v20.14.0
10.7.0
10.7.0

手順5. Rustのインストール

  • URLからRUSTUP-INIT.exe(64bit)をダウンロードをクリック

  • 以下の手順でGNU版をインストール

ここでデフォルトのx86_64-pc-windows-msvcではなくx86_64-pc-windows-gnuをインストールしています.
これはさっきMinGWをインストーしたいのでそれを使いたい、またJetBrainsユーザーなのでVisual Studio系をインストールしたくないからに過ぎません.お好みで良いです.

Rust Visual C++ prerequisites

Rust requires a linker and Windows API libraries but they don't seem to be
available.

These components can be acquired through a Visual Studio installer.

1) Quick install via the Visual Studio Community installer
   (free for individuals, academic uses, and open source).

2) Manually install the prerequisites
   (for enterprise and advanced users).

3) Don't install the prerequisites
   (if you're targeting the GNU ABI).

>3
# 3 + Enter
# さっきMinGWをインストールしたのでVisual Studioは不要、GNU版をインストールして連携させる.

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  C:\Users\smith\.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory is located at:

  C:\Users\smith\.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  C:\Users\smith\.cargo\bin

This path will then be added to your PATH environment variable by
modifying the HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-pc-windows-msvc
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
>2
# 2 + Enter
# msvcのToolChainではなくgnuのToolChainを使いたいためCustomizeを選択.

I'm going to ask you the value of each of these installation options.
You may simply press the Enter key to leave unchanged.

Default host triple? [x86_64-pc-windows-msvc]
x86_64-pc-windows-gnu

Default toolchain? (stable/beta/nightly/none) [stable]
# Enter

Profile (which tools and data to install)? (minimal/default/complete) [default]
# Enter

Modify PATH variable? (Y/n)
> Y 
# Y + Enter
# 環境変数PATHを設定してほしい

Current installation options:


   default host triple: x86_64-pc-windows-gnu
     default toolchain: stable
               profile: default
  modify PATH variable: yes

1) Proceed with selected options (default - just press enter)
2) Customize installation
3) Cancel installation
>
# Enter
info: profile set to 'default'
info: setting default host triple to x86_64-pc-windows-gnu
warning: Updating existing toolchain, profile choice will be ignored
info: syncing channel updates for 'stable-x86_64-pc-windows-gnu'
info: default toolchain set to 'stable-x86_64-pc-windows-gnu'

  stable-x86_64-pc-windows-gnu unchanged - rustc 1.79.0 (129f3b996 2024-06-10)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload its PATH environment variable to include
Cargo's bin directory (%USERPROFILE%\.cargo\bin).

Press the Enter key to continue.

これでRustのインストールは完了です.

手順6 Dockerのインストール

Docker Desktop for Windowsは商用利用において有料化されました.フリーランスでもなく、会社がライセンス料を払うわけでもないなら使えません.またインストールには管理者権限を必要とするため情シスの目に留まります.

従業員数250名を超える企業、または年間売上高1000万ドルを超える企業でDocker Desktopを商用利用するには、有料サブスクリプション(Pro、Team、または Business) が必要です。

そこでRancher Desktopをインストールします.DockerやDocker Composeのコマンドがそのまま使えるオープンソースプロジェクトです

  • 上記URLのDownload for Windows (x64)からRancher.Desktop.Setup.**.exeをダウンロードしインストールします

インストール完了後、以下のようにdockerの基本的なコマンドが使えるようになります.

$ docker ps && docker compose ls && kubectl version
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
NAME                STATUS              CONFIG FILES
Client Version: v1.29.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Unable to connect to the server: dial tcp [::1]:8080: connectex: No connection could be made because the target machine actively refused it.

環境変数の確認

手順通り進めばこのように設定されているはず

環境変数
Path C:\Users\ユーザー名\.cargo\bin;
%USERPROFILE%\.pyenv\pyenv-win\bin;
%USERPROFILE%\.pyenv\pyenv-win\shims;
%USERPROFILE%\mingw64\bin;
%USERPROFILE%\node-v20.14.0-win-x64;
C:\Users\ユーザー名\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin;
C:\Users\ユーザー名\AppData\Local\Programs\Rancher Desktop\resources\resources\linux\bin\
PYENV C:\Users\ユーザー名\.pyenv\pyenv-win\
PYENV_HOME C:\Users\ユーザー名\.pyenv\pyenv-win\
PYENV_ROOT C:\Users\ユーザー名\.pyenv\pyenv-win\

最後に

貴方の御用のためにお使いください

1
1
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
1
1