Installing programs from non-system package managers without sudo

Published: ; Updated:
Tags: linux, shell

Sometimes programs that are distributed with programming language specific package managers (like pip, npm etc.) instruct users to install the program with sudo or as root. You can also install these programs without root, but that requires a little extra configuration.

With root permissions the package manager can place the program in /usr/local/bin, by default this directory is part of your shell’s PATH variable. If you run echo $PATH at the command-line, you can see the full PATH, for example:

/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

This is a colon-separated list of directories where your shell looks for commands when you enter a command. When you enter ls in your shell, it looks for the program called ls in the directories listed in your PATH, finds it at /bin/ls and then runs that program. When you install a program without root, the executable is placed in a folder in your home directory. You need to add this folder to your PATH in order for your shell to find the program.

In what follows we’ll add directories to your PATH in the ~/.profile configuration file. Create this file if it doesn’t exist yet. Different package managers use different locations to place executables, so each needs their own configuration. Follow the instructions for the package manager(s) you’d like to use, then skip to the activation section.

cargo

Cargo is Rust’s package manager. It places binaries in ~/.cargo/bin. Add the following to ~/.profile:

export PATH="$HOME/.cargo/bin:$PATH"

gem

Gem is used in the Ruby world. You need to pass the --user-install option to install a gem in your home directory, e.g. gem install --user-install fpm. It then places programs in ~/.gem/ruby/2.3.0/bin where the 2.3.0 part depends on which version of Ruby you have installed. First install a gem, and look at which version you have by running ls ~/.gem/ruby/, then add to your ~/.profile:

export PATH="$HOME/.gem/ruby/2.3.0/bin:$PATH"

Where you replace 2.3.0 with your version.

npm

Npm is what JavaScript developers use. Their documentation has a page explaining how to install packages globally without sudo.

pip

Pip is the package manager used in the Python ecosystem. Pip places programs in ~/.local/bin. Add this line to ~/.profile:

export PATH="$HOME/.local/bin:$PATH"

Activation

For the changes to the PATH variable to take effect, the ~/.profile file needs to be sourced. Sourcing a file means that your shell runs the commands in the file in the current shell context, so variables that change or are defined stay after the shell has finished sourcing the file. You source a file with the . command.

Ideally ~/.profile is sourced at the startup of your desktop environment. This ensures that all applications have the updated PATH, including your terminal and text editor. The method to do this differs per Linux distribution, follow the instructions for your distro below. If that doesn’t work, you can update your PATH for your shell only as a last resort.

The changes to your PATH should take effect the next time you login after this final configuration.

Debian

Debian users need to create a ~/.xsessionrc file, run:

echo '. ~/.profile' >> ~/.xsessionrc

Ubuntu

You are done! Ubuntu automatically sources ~/.profile when you login.

Others

For other distributions create a ~/.xinitrc file:

echo '. ~/.profile' >> ~/.xinitrc

If this doesn’t work after a re-login, try a ~/.xprofile file:

echo '. ~/.profile' >> ~/.xprofile

Shell

Your shell has a configuration file it executes on startup. For Bash it’s ~/.bashrc. Place this line in there:

. ~/.profile

For Zsh you put that line in ~/.zshrc.


RSS Contact