When it comes to installing gems, the "gem install" command is the go-to for most developers. However, one issue that often arises is the placement of the installed executable files. By default, the "gem install" command installs executables in the /usr/bin directory, which can cause problems when trying to manage gems across different environments. In this article, we will explore how to configure "gem install" to install executables outside of the /usr/bin directory by default.
First, let's understand why the default installation directory can be problematic. The /usr/bin directory is a system directory, and any modifications to it require root access. This means that if you want to install a gem and its executable files in a non-system location, you will need to use sudo or have root privileges. This can be cumbersome and can cause issues when working with multiple users or environments.
To avoid this problem, we can configure "gem install" to install executables outside of the /usr/bin directory by default. To do this, we will need to modify the GEM_HOME and PATH variables.
GEM_HOME is the directory where gems are installed, and by default, it is set to /usr/bin. We will change this to a different directory where we have write permissions. For example, we can create a new directory called gems in our home directory and set GEM_HOME to be the path to this directory.
Next, we need to add this new directory to the PATH variable. PATH is an environment variable that specifies the directories in which executable files can be found. By adding our new gems directory to the PATH, we are telling the system to look for executable files in this directory as well.
To make these changes permanent, we can add the following lines to our .bashrc or .zshrc file, depending on the shell we are using:
export GEM_HOME=$HOME/gems
export PATH=$GEM_HOME/bin:$PATH
Once we have saved these changes and sourced the file, we can test our configuration by installing a gem without using sudo. For example, we can run the following command to install the popular bundler gem:
gem install bundler
If everything is set up correctly, the bundler executable should be installed in our new gems directory, and we should be able to run it without using sudo.
This configuration also comes in handy when working with multiple versions of a gem. By installing gems in a non-system directory, we can have different versions of the same gem installed simultaneously without conflicts.
In addition to configuring the default installation directory for executables, we can also specify a different directory for each gem. This can be done by using the --install-dir flag when installing a gem. For example, the following command will install the bundler gem in a new directory called my-gems in our home directory:
gem install bundler --install-dir=$HOME/my-gems
In conclusion, by configuring "gem install" to install executables outside of the /usr/bin directory by default, we can avoid the hassle of using sudo and manage our gems more efficiently. This also allows us to have multiple versions of a gem installed without conflicts. So, the next time you need to install a gem, consider setting up your configuration to install executables in a custom directory.