How to Permanently Add /usr/lib/ruby-enterprise/bin to $PATH in CentOS for All Users


3 views

The $PATH environment variable is a colon-separated list of directories that tells the shell where to look for executable files. When you type a command in the terminal, the system searches through these directories in order until it finds the executable.

The command you tried:

export PATH=/usr/lib/ruby-enterprise/bin:$PATH

only affects the current shell session. When you log out or open a new terminal, this change will be lost.

For CentOS/RHEL systems, the standard approach is to create a new file in /etc/profile.d/:

echo 'export PATH=/usr/lib/ruby-enterprise/bin:$PATH' > /etc/profile.d/ruby-enterprise.sh
chmod +x /etc/profile.d/ruby-enterprise.sh

This ensures the path is set for all users on system login.

You could also modify these files for different scopes:

  • /etc/environment - System-wide environment variables (no export needed)
  • /etc/profile - System-wide login shell initialization
  • ~/.bashrc - User-specific shell initialization

To confirm the change took effect:

source /etc/profile
echo $PATH
which ruby

When modifying system paths:

  1. Always back up original files before editing
  2. Test changes in a non-production environment first
  3. Document all system modifications
  4. Consider using alternatives like update-alternatives for managing multiple versions

If the changes don't appear:

# Check file permissions
ls -l /etc/profile.d/ruby-enterprise.sh

# Verify file is being sourced
grep 'ruby-enterprise' /etc/profile /etc/profile.d/*

# Check shell type
echo $SHELL

The $PATH environment variable is a colon-separated list of directories that tells the shell where to look for executable files. When you type a command in the terminal, the shell searches through these directories in order until it finds the executable.

echo $PATH
# Typical output: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The command export PATH=/new/path:$PATH only modifies the PATH for the current shell session. When you open a new terminal or log in again, these changes are lost because they're not written to any configuration file.

For CentOS/RHEL systems, there are several files where you can permanently set environment variables:

  • /etc/environment - System-wide environment variables
  • /etc/profile.d/ - Scripts executed during login shell initialization
  • /etc/bashrc - System-wide functions and aliases

The cleanest method is to create a new file in /etc/profile.d/:

sudo nano /etc/profile.d/ruby-enterprise.sh

Add this content:

# Set PATH for Ruby Enterprise Edition
export PATH=/usr/lib/ruby-enterprise/bin:$PATH

Make the file executable:

sudo chmod +x /etc/profile.d/ruby-enterprise.sh

After making changes, either log out and log back in or run:

source /etc/profile

Then verify Ruby is using the correct path:

which ruby
# Should return: /usr/lib/ruby-enterprise/bin/ruby

For a more fundamental approach (affects all users and services):

sudo nano /etc/environment

Edit the PATH line (note: don't use $PATH variable here):

PATH="/usr/lib/ruby-enterprise/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

If you need the PATH to be available when using sudo, add this to /etc/sudoers:

Defaults secure_path = /usr/lib/ruby-enterprise/bin:/sbin:/bin:/usr/sbin:/usr/bin

Use visudo to edit this file safely.

If changes don't appear:

  • Check for syntax errors in your scripts
  • Verify file permissions (should be readable by all users)
  • Confirm you're using a login shell (bash -l forces a login shell)
  • Check for conflicting PATH settings in user's ~/.bashrc or ~/.bash_profile