The error "sudo: bundle: command not found" occurs because the sudo command executes processes in a restricted environment that does not include the directory where the Bundler gem is installed. Even if bundle works for your regular user, the root user's PATH environment variable is typically different and does not point to the Ruby gem binary folder.
Understanding the PATH Environment Variable
To understand why this happens, it is necessary to understand the PATH variable. The PATH is a list of directories that the operating system searches through whenever you type a command into the terminal. If you type bundle, the shell looks through every directory in your PATH until it finds an executable file named "bundle".
When you use sudo, you are telling the system to run a command with administrative (root) privileges. For security reasons, most Linux distributions and macOS versions use a feature called "secure_path". This means that when sudo is invoked, it ignores the current user's PATH and replaces it with a hardcoded, minimal set of trusted directories (usually /sbin, /bin, /usr/sbin, and /usr/bin). Since Ruby gems are typically installed in a user's home directory or a version manager's specific folder (like .rbenv or .rvm), these locations are not in the root user's secure path, resulting in the "command not found" error.
Why You Should Avoid Using Sudo with Bundle
Before attempting to fix the path, it is critical to address why running sudo bundle install is generally considered a bad practice in professional software development. Installing gems as root can lead to several systemic issues:
- Permission Corruption: When you run Bundler as root, any files created (such as the
Gemfile.lockor folders invendor/bundle) are owned by the root user. Later, when you try to run the application as a normal user, the app will crash because it lacks the permissions to read or write to those root-owned files. - Security Risks: Gems are third-party code. Running a gem installation process with root privileges allows a potentially malicious or buggy gem to modify system-level files, install unauthorized software, or create security backdoors in the operating system.
- System Ruby Pollution: Many operating systems come with a "system Ruby" used for OS tasks. Using
sudooften forces gems into the system directory, which can break OS utilities that rely on specific versions of those gems.
How to Fix the Command Not Found Error
Depending on your goal, there are several ways to resolve this error. The best method depends on whether you actually need root privileges or if you are simply trying to overcome a permission error.
Method 1: Use the Full Path to the Executable
If you absolutely must run the command as root, you can bypass the PATH search by providing the absolute path to the Bundler executable. First, find where bundle is located by running which bundle as your normal user. It might return something like /home/user/.rbenv/shims/bundle. You can then run:
sudo /home/user/.rbenv/shims/bundle install
This tells sudo exactly where the file is, so it doesn't need to rely on the restricted secure_path.
Method 2: Pass the User PATH to Sudo
You can instruct sudo to preserve your current environment's PATH variable using the -E flag or by explicitly defining the PATH in the command string:
- Try
sudo -E bundle install. The-Eflag attempts to preserve your environment variables, though some system configurations still override the PATH for security. - Alternatively, use:
sudo "PATH=$PATH" bundle install. This explicitly passes your current user's PATH to the root session.
Method 3: Fix Permissions Instead of Using Sudo
If you are using sudo because you got a "Permission Denied" error when running bundle install, the correct fix is to change the ownership of the project folder, not to elevate the command privileges. Use the chown command to make your user the owner of the directory:
sudo chown -R $(whoami) /path/to/your/project
Once you own the directory, you can simply run bundle install without sudo, and the gems will be installed with the correct user permissions.
The Role of Ruby Version Managers
Most developers use version managers like rbenv, RVM, or asdf to avoid these issues entirely. These tools install Ruby and its gems into the user's home directory rather than system folders. This eliminates the need for sudo for almost all Ruby-related tasks.
When using a version manager, the bundle command is usually a "shim"—a small wrapper script that determines which version of Ruby should be used for the current project. Because these shims live in ~/.rbenv/shims or ~/.rvm/bin, they are completely invisible to sudo. If you find yourself needing sudo frequently with a version manager, it is a sign that the project folder permissions are incorrectly set or that the application is being deployed to a production server using an incorrect user account.
Common Misconceptions About Gem Installation
A common misconception is that sudo is required to install gems globally. While it is true that installing to the system Ruby directory requires root access, installing gems globally is rarely the desired outcome for a specific project. The modern standard is to use a Gemfile and bundle install, which manages dependencies locally for that project.
Another misconception is that sudo bundle install is the only way to fix "Permission Denied" errors on a server. In production environments (like Ubuntu or CentOS), the standard practice is to create a dedicated application user (e.g., a user named deploy). The deployment script then switches to that user via sudo -u deploy bundle install. This ensures the gems are installed as the user who will actually run the application, preventing the permission conflicts mentioned earlier.
Frequently Asked Questions
Why does bundle work without sudo but fail with it?
Your normal user account has a PATH variable that includes the Ruby gem folders. When you use sudo, the system switches to a "secure_path" for security reasons, which excludes those Ruby folders. Consequently, the system cannot locate the bundle executable when running as root.
Is it safe to add my gem path to the sudoers file?
It is generally not recommended. Modifying the /etc/sudoers file to relax PATH restrictions can introduce security vulnerabilities. It is much safer to use a Ruby version manager or fix the ownership of the project directory using chown.
What is the difference between gem install and bundle install?
gem install installs a single library into your Ruby environment globally or per-user. bundle install reads a Gemfile and installs all the specific versions of multiple libraries required for a particular project, ensuring consistency across different development machines.
How do I find the exact path to my bundle command?
You can find the absolute path by typing which bundle in your terminal. This will return the full directory string, such as /usr/local/bin/bundle or /home/username/.rbenv/shims/bundle, which you can then use with sudo if absolutely necessary.
Can I install gems to a local folder to avoid sudo?
Yes. You can use the command bundle config set path 'vendor/bundle'. This tells Bundler to install all gems into a vendor/bundle folder within your project directory instead of the system-wide Ruby directory, removing the need for administrative privileges.