I wrote a post yesterday about setting up the Perforce server P4D on Mac OS X once you’ve done that all that is left is to configure P4V the visual client and P4 the command line client. Fortunately P4V has a helpful set-up wizard to help you configure it for the first time: connect to the server, create your new user account, new client spec and sync up for the first time.

The command line utility P4 takes a little bit more configuration via environment variables which is not quite as simple as setting up P4V, so I thought I would document it. The first thing I did was run P4V and configured it, this has the side effect of working out the perforce port, creating a user account and associated client spec.  Take a note of the username, clientspec and perforce port as you setup P4V, you’ll use those values to configure P4.

Note for this to work you need to have P4 already set-up in your system’s path variable. If you don’t have P4 in your path when you type ‘p4‘ and hit enter in a terminal window you will get a response along the lines of ‘-bash: p4: command not found‘. If you don’t have this already set-up and you are using the default bash terminal on Mac OS X, then you can add P4 to your path by doing the following:

  1. Open a Terminal window, (its in the utilities folder in the application folder).
  2. Type vi .bash_profile this will open the hidden file that defines your bash profile file in the vi text editor.
  3. Edit the line (press i to start editing and esc to stop) that starts with path=, insert the path to where you put p4 (in red) followed by a : right after path= e.g. PATH=”/Developer/Perforce/:…:${PATH}”.
  4. Save the file (with :s then enter) and then exit vi (with :q then enter).
  5. Quit Terminal.
  6. Reopen Terminal.
  7. Type p4 and hit enter, you should now get a response from the p4 application, although this will most likely get an error response unless you’ve already set-up your Perforce environment variables.

Now we can configure the three environment variables that the P4 application requires to work, these are $P4PORT, $P4USER and $P4CLIENT. These variables tell the P4 application how to connect to the server and the user to connect as and clientspec to connect with for that user. As I mentioned above the values of these three variables are the same as the perforce port, username and clientspec you used in P4V. To setup these environment variables we are going to add them to a hidden system file which is run every time you login to set-up the environment variables for your user profile (from this guide).

  1. Open a Terminal window.
  2. Type mkdir .MacOSX and hit enter to create the hidden directory the file resides in, the dot at the start means it is a hidden file.
  3. Type echo “” > .MacOSX/environment.plist and hit enter to create an empty file called environment.plist in your user directory directory, which is hidden.
  4. Type open .MacOSX/environment.plist and hit enter to open the file we just created in the plist editor.
  5. Add a child value with name P4PORT and the value from P4V e.g. localhost:1666.
  6. Add a sibling value with name P4USER with the value of your user from P4V e.g. daniel.
  7. Add a sibling value with name P4CLIENT with the value of the clientspec for your user in P4V e.g. daniels_clientspec.
  8. Save the file and exit the editor.
  9. Quit terminal.
  10. Logout and log back in.
  11. Open terminal and type P4 and hit enter, you should now get a non-error response from P4.

It is worth configuring the P4 command line application as it is the main way to administer a perforce server, so if you want add, delete or manage users, depots etc then you need to have P4 configured.