LoginSignup
0

More than 1 year has passed since last update.

-bash: php: command not found 解決方法

Last updated at Posted at 2022-01-23

初めに

作成中のポートフォリオでPHPのバージョンを確認したいと思い、php -vをターミナルで叩いたところ、-bash: php: command not foundと表示されてバージョン確認できなかったので、その解決方法についてご紹介していきます。

エラー原因

アプリディレクトリ $ php -v
-bash: php: command not found

-bash: php: command not foundで「そんなコマンド無いよ!」と指摘された時は、パスが通ってないことが原因です。
なので、.bash_profileファイルにパスを通す設定をしてあげることで解決できます!

1.パスを通す

.bash_profileの確認

先ずそもそも自身のホームディレクトリに.bash_profileが存在しているのか確認してみます。

アプリディレクトリ $ cd #ホームディレクトリに戻る

ホームディレクトリ $ ls #ホームディレクトリ下に配置されているファイルを確認
Applications        OneDrive        getting-started
Creative Cloud Files    Pictures        index.html
Desktop         Public          mysite

上記コマンドで確認してみると、Applications等はありますが、.bash_profileは存在していないことがわかります。

.bash_profile作成

.bash_profileがないので新規で作成します。
ついでに、~/.bashrcも作成しておきす。

$ touch ~/.bash_profile
$ touch ~/.bashrc

編集

.bash_profileにパスを通す設定を書き込みます。

$ vi ~/.bash_profile
# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/php5/bin

export PATH

反映

sourceコマンドで変更を即時反映させます。

$ source ~/.bash_profile

確認

では、もう一度phpのバージョン確認をしてみます。

アプリディレクトリ $ php -v
-bash: php: command not found

??? 何故かまだパスが通ってないようです。

2.Homebrewをアップデート

いくつか参考記事を見て、HomebrewでPHPをインストールしてみることに。
その前に一様Homebrewをアップデートしておきます。

ホームディレクトリ $ brew upgrade

これで再度バージョン確認をしてみます。
おっと、アップデートのみでバージョン確認できるようになりました!

$ php -v
PHP 8.1.2 (cli) (built: Jan 21 2022 04:47:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

他の参考記事ではアップデートのみで解決した事象はなかったので、今回の記事が少しでも参考になればと思います!

参考記事

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
0