Skip to content

Instantly share code, notes, and snippets.

@andrejcremoznik
Last active July 12, 2018 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrejcremoznik/07429341fff4f318c5dd to your computer and use it in GitHub Desktop.
Save andrejcremoznik/07429341fff4f318c5dd to your computer and use it in GitHub Desktop.
Instructions on how to set up various webdev tools in a bash shell

Make custom executables available as global bash commands

If you want custom Bash, NodeJS, PHP or any other script available as global bash commands you need to add them to the $PATH environment variable.

Global access to composer and WP-CLI

  1. Create a new directory in your user's home directory that will contain global apps
  2. Install those apps to that directory and make them executable by the current user
  3. Add the directory to system's $PATH
$[~/] mkdir ~/bin
$[~/] cd ~/bin
$[~/.bin/] curl -o composer https://getcomposer.org/composer.phar
$[~/.bin/] curl -o wp https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$[~/.bin/] chmod u+x *

Then edit ~/.bashrc and add in or edit the following line:

# Add custom directories to $PATH
PATH=$HOME/bin:$PATH

And source the new file (or open a new shell): source ~/.bashrc

You can now use composer and wp commands from anywhere on the system.

$[~/] wp --info
PHP binary:     /usr/bin/php
PHP version:    7.2.7
...

Access in non-interactive shells

For non-interactive shells (scripts connectng to server) to be able to use these commands the $PATH needs to be set at the top of ~/.bashrc, before the line that exits non-interactive shells.

# Add custom directories to $PATH
PATH=$HOME/bin:$PATH

[[ $- != *i* ]] && return

# rest of the file ...

NPM global packages without root

  1. Install nodejs. Follow instructions on https://nodejs.org/en/download/package-manager/
  2. Create a new directory in your user's home directory that will contain global NPM packages
  3. Set NPM prefix to that directory
  4. Add the directory to $PATH
Install Node with your distro's packet manager then:

$[~/] mkdir ~/node
$[~/] npm config set prefix ~/node
$[~/] npm config get prefix
/home/<user>/node

Then edit ~/.bashrc and add the ~/node/bin directory to $PATH like so:

# Add custom directories to $PATH
PATH=$HOME/bin:$HOME/node/bin:$PATH

Source the new file (or open a new shell): source ~/.bashrc

Test

If you've followed the instructions above, you should be able to install global NPM packages:

$[~/] npm install -g create-react-app

If you get permission errors, you did something wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment