Closed2

What does /opt mean in Linux?

Daichi TamakiDaichi Tamaki

Let’s take an internal application developed in a company, CompanyApplication, as an example.

If we don’t package it using standard tools, such as dpkg or rpm, we’ll most likely put all of the files related to CompanyApplication in a single directory. So we’ll have the binaries, libraries, and configuration together. They won’t be separated into different locations as a traditional UNIX system would have them.

Let’s say we want to deploy our application on a server. In this case, we can just copy the directory of our application in the /opt/CompanyApplication directory.

When needed, we can execute it directly from there. Therefore when using /opt, installing our application is as simple as copying, extracting a TAR, or unzipping. And when we don’t need the application anymore, we can remove it simply by removing the directory under /opt.

As a side note, we can also prefer using a /opt/Company/CompanyApplication directory structure. In this case, we should use a LANANA registered company/provider name.

Deploying software by copying a directory is unconventional for a traditional UNIX system. Normally a UNIX application would have its libraries, binaries, and other files in separate directories, such as /usr/local/bin, and /usr/local/lib.

https://www.baeldung.com/linux/opt-directory

ある企業で開発された社内アプリケーション、CompanyApplication を例にとって考えてみましょう。

dpkg や rpm などの標準的なツールを使ってパッケージ化しない場合、CompanyApplication に関連するすべてのファイルを1つのディレクトリに置くことがほとんどでしょう。つまり、バイナリ、ライブラリ、コンフィギュレーションを一緒に置くことになります。伝統的な UNIX システムのように、異なる場所に分けられることはありません。

例えば、アプリケーションをサーバーにデプロイしたいとします。この場合、アプリケーションのディレクトリを/opt/CompanyApplicationディレクトリにコピーすればいいのです。

必要なときに、そこから直接実行することができます。したがって、/optを使用する場合、アプリケーションのインストールは、コピー、TARの展開、または解凍と同じくらい簡単です。そして、アプリケーションが不要になったら、/opt以下のディレクトリを削除するだけで、アプリケーションを削除することができます。

余談ですが、/opt/Company/CompanyApplicationのディレクトリ構造を使用することも可能です。この場合、LANANAに登録されている会社名/プロバイダー名を使用する必要があります。

ディレクトリをコピーしてソフトウェアを配備することは、伝統的なUNIXシステムにはない型破りな方法です。通常、UNIXアプリケーションは、ライブラリ、バイナリ、その他のファイルを、/usr/local/bin、/usr/local/libといった別々のディレクトリに配置します。

Daichi TamakiDaichi Tamaki

4. /usr/local vs /opt

The FHS defines /usr/local as “For use by the system administrator when installing software locally”. This may be confusing, as this description is very similar to /opt. On the other hand, there’s an important difference.

The hierarchy under /usr/local should mimic the hierarchy under /usr. This means we should place all the application binaries in /usr/local/bin, and all the libraries in /usr/local/lib, and so on. As a result, we’ll place them along with the files of the other applications.

Therefore, we can’t have a single directory for each application when deploying into /usr/local. Instead, we organize them in the more conventional UNIX style that divides application files to separate directories.

Some of the reasons for this traditional approach are:

  • When we have all the binaries in /usr/local/bin, we can just add this single directory to our $PATH, so we can execute all of the binaries we installed without additional configuration.
  • When all installed libraries are in /usr/local/lib, multiple binaries can share the same library, so we avoid having multiple copies of the same library on our system.

On the other hand, /opt doesn’t have these directory structure restrictions. As long as the applications in /opt are in separate directories, they can have custom directory structures inside these directories. They can have duplicate copies of libraries already installed in the system, and they may require additional $PATH configuration to execute from the terminal.

https://www.baeldung.com/linux/opt-directory

4. /usr/local と /opt の比較

FHSでは、/usr/localを「システム管理者がソフトウェアをローカルにインストールする際に使用するもの」と定義しています。この記述は/optと非常に似ているため、混乱するかもしれません。一方、重要な違いもあります。

usr/localの下の階層は、/usrの下の階層を模倣する必要があります。つまり、アプリケーションのバイナリはすべて/usr/local/binに、ライブラリはすべて/usr/local/libに、というように配置する必要があるのです。その結果、他のアプリケーションのファイルと一緒に配置することになります。

したがって、/usr/local にデプロイする際に、アプリケーションごとに単一のディレクトリを用意するわけにはいきません。代わりに、アプリケーションのファイルを別々のディレクトリに分ける、より伝統的なUNIXスタイルで整理します。

この伝統的なアプローチの理由には、次のようなものがあります。

  • usr/local/bin にすべてのバイナリがある場合、この単一のディレクトリを $PATH に追加するだけで、追加の設定なしにインストールしたすべてのバイナリを実行できる。
  • インストールしたすべてのライブラリが/usr/local/libにある場合、複数のバイナリが同じライブラリを共有できるため、システム上に同じライブラリのコピーを複数持つことを避けられます。

一方,/optにはこのようなディレクトリ構造の制約がない.optにあるアプリケーションは、別々のディレクトリにある限り、そのディレクトリの中で独自のディレクトリ構造を持つことができます。また、すでにシステムにインストールされているライブラリーの複製を持つこともできますし、ターミナルから実行するために追加の$PATH設定を必要とする場合もあります。

このスクラップは2023/01/04にクローズされました